:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #d8dee8;
  --text: #172033;
  --muted: #667085;
  --accent: #0f766e;
  --accent-strong: #0d5f59;
  --accent-soft: #e6f4f1;
  --bubble: #eef2f7;
  --danger: #cf222e;
  --shadow: 0 18px 50px rgba(17, 24, 39, 0.10);
  --app-height: 100vh;
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #071411;
  --surface: #0d211d;
  --surface-2: #102a25;
  --border: rgba(73, 224, 178, 0.2);
  --text: #e8fff7;
  --muted: #9db8ad;
  --accent: #35d7b7;
  --accent-strong: #78ffe1;
  --accent-soft: rgba(53, 215, 183, 0.12);
  --bubble: #163a33;
  --danger: #ff8a8a;
  --shadow: 0 18px 58px rgba(0, 0, 0, 0.34);
}

@supports (height: 100dvh) {
  :root {
    --app-height: 100dvh;
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow-x: hidden;
}

main {
  width: min(960px, calc(100% - 32px));
  height: min(820px, calc(var(--app-height) - 32px));
  margin: 16px auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

header {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
}

[data-theme="dark"] header {
  background: rgba(13, 33, 29, 0.92);
}

.history-toggle,
.history-close {
  display: none;
}

h1 {
  flex: 1 1 auto;
  margin: 0;
  font-size: 18px;
  line-height: 1.25;
}

.status {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.status::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #94a3b8;
}

.status.ok::before {
  background: #16a34a;
}

.status.error::before {
  background: var(--danger);
}

.token-field {
  min-width: 0;
}

.token-help {
  margin-top: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

label {
  display: block;
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 11px 12px;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  outline: none;
}

[data-theme="dark"] input,
[data-theme="dark"] textarea {
  background: rgba(13, 33, 29, 0.8);
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.workspace {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
}

.drawer-scrim {
  display: none;
}

.history-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--surface-2);
  padding: 14px 12px;
}

.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.history-title {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.drawer-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.drawer-section-title {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.quota-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

[data-theme="dark"] .quota-panel {
  background: var(--surface);
}

.quota-panel.ok {
  border-color: rgba(15, 118, 110, 0.24);
}

.quota-panel.warning {
  border-color: #facc15;
  background: #fefce8;
}

[data-theme="dark"] .quota-panel.warning {
  background: rgba(202, 138, 4, 0.14);
}

.quota-panel.error {
  border-color: #fecdd3;
  background: #fff1f2;
}

[data-theme="dark"] .quota-panel.error {
  border-color: rgba(255, 138, 138, 0.48);
  background: rgba(207, 34, 46, 0.14);
}

.quota-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.quota-head strong {
  min-width: 0;
  color: var(--text);
  font-size: 13px;
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.quota-panel.error .quota-head strong {
  color: var(--danger);
}

.quota-panel.ok .quota-head strong {
  color: var(--accent-strong);
}

.quota-meter {
  height: 8px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--bubble);
}

.quota-meter span {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 160ms ease;
}

.quota-panel.warning .quota-meter span {
  background: #ca8a04;
}

.quota-panel.error .quota-meter span {
  background: var(--danger);
}

.quota-detail {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.history-section {
  min-height: 0;
  flex: 1;
  border-bottom: 0;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  min-height: 0;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 3px;
}

.history-item:hover,
.history-item.active {
  border-color: rgba(15, 118, 110, 0.24);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.history-title-button {
  min-width: 0;
  min-height: 34px;
  flex: 1;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 6px 7px;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-title-button:hover:not(:disabled) {
  background: transparent;
}

.history-delete {
  display: grid;
  width: 30px;
  min-width: 30px;
  min-height: 30px;
  place-items: center;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  padding: 0;
  font-size: 18px;
  font-weight: 700;
  opacity: 0;
}

.history-item:hover .history-delete,
.history-delete.confirm {
  opacity: 1;
}

.history-delete:hover:not(:disabled) {
  background: #fff1f2;
  color: var(--danger);
}

.history-delete.confirm {
  width: auto;
  min-width: 72px;
  border-radius: 8px;
  background: #fff1f2;
  color: var(--danger);
  padding: 0 8px;
  font-size: 12px;
}

.history-empty {
  color: var(--muted);
  font-size: 13px;
}

.chat {
  min-height: 0;
  overflow-y: auto;
  padding: 18px;
}

.chat-tip {
  margin-bottom: 14px;
  border: 1px solid rgba(15, 118, 110, 0.18);
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  padding: 9px 12px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.5;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  max-width: 82%;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-content {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
}

.message.user .message-content {
  align-items: flex-end;
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 999px;
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  background: var(--bubble);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.message.user .avatar {
  background: var(--accent);
  color: #fff;
}

.bubble {
  min-width: 0;
  padding: 11px 13px;
  border-radius: 8px;
  background: var(--bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.7;
  font-size: 15px;
}

.message.user .bubble {
  background: var(--accent);
  color: #fff;
}

.message.error .bubble {
  background: #fff1f2;
  color: var(--danger);
  border: 1px solid #fecdd3;
}

.message.streaming .bubble::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 1em;
  margin-left: 3px;
  vertical-align: -2px;
  background: currentColor;
  border-radius: 2px;
  animation: blink 1s steps(2, start) infinite;
}

.message-actions {
  display: flex;
  gap: 6px;
}

.copy-button {
  display: grid;
  width: 32px;
  min-width: 0;
  height: 32px;
  min-height: 0;
  place-items: center;
  border: 1px solid rgba(15, 118, 110, 0.18);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  padding: 0;
  opacity: 0.72;
  transition: opacity 120ms ease, color 120ms ease, background 120ms ease;
}

.copy-button svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.9;
}

.message:hover .copy-button,
.copy-button:focus-visible {
  opacity: 1;
}

.copy-button:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--accent-strong);
}

.message.streaming .message-actions {
  display: none;
}

.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 50;
  max-width: min(360px, calc(100vw - 32px));
  transform: translate(-50%, calc(-50% + 6px));
  border-radius: 999px;
  background: rgba(23, 32, 51, 0.76);
  box-shadow: 0 8px 18px rgba(17, 24, 39, 0.12);
  color: #fff;
  padding: 9px 13px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.4;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.show {
  transform: translate(-50%, -50%);
  opacity: 1;
}

.toast.error {
  background: rgba(207, 34, 46, 0.82);
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.composer {
  padding: 12px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

.composer-box {
  display: flex;
  gap: 10px;
  align-items: center;
}

.composer-box textarea {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid var(--border);
  background: #fff;
}

[data-theme="dark"] .composer-box textarea {
  background: var(--surface);
}

.composer-box button {
  flex: 0 0 auto;
}

.send-button {
  display: grid;
  align-self: center;
  width: 44px;
  min-width: 44px;
  height: 44px;
  min-height: 44px;
  place-items: center;
  border-radius: 999px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

.send-button.stop-mode {
  background: var(--danger);
  font-size: 18px;
}

.send-button.stop-mode:hover:not(:disabled) {
  background: #a40e26;
}

textarea {
  height: 46px;
  min-height: 46px;
  max-height: 160px;
  overflow-y: auto;
  resize: none;
  border: 0;
  padding: 10px;
}

textarea:focus {
  box-shadow: none;
}

button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  min-width: 78px;
  min-height: 44px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

button:hover:not(:disabled) {
  background: var(--accent-strong);
}

button.secondary {
  border: 1px solid rgba(15, 118, 110, 0.24);
  background: var(--accent-soft);
  color: var(--accent-strong);
}

button.secondary:hover:not(:disabled) {
  background: #d7eee9;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.hint {
  margin-top: 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

@media (max-width: 640px) {
  body {
    background: var(--surface);
    height: var(--app-height);
    min-height: var(--app-height);
    width: 100vw;
    overflow: hidden;
  }

  main {
    width: 100%;
    max-width: 100vw;
    height: var(--app-height);
    min-height: 0;
    margin: 0;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  header {
    min-width: 0;
    padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
    align-items: center;
    flex-direction: row;
    gap: 10px;
    justify-content: flex-start;
  }

  .history-toggle {
    display: grid;
    width: 40px;
    min-width: 40px;
    height: 40px;
    place-items: center;
    border-radius: 999px;
    background: transparent;
    color: var(--text);
    padding: 0;
    font-size: 22px;
    line-height: 1;
  }

  .history-toggle:hover:not(:disabled) {
    background: var(--surface-2);
  }

  h1 {
    min-width: 0;
    font-size: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .status {
    flex: 0 0 12px;
    width: 12px;
    height: 12px;
    border: 0;
    background: transparent;
    padding: 0;
    overflow: hidden;
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: transparent;
  }

  .status::before {
    flex: 0 0 10px;
    width: 10px;
    height: 10px;
  }

  .workspace {
    position: relative;
    min-width: 0;
    display: block;
    overflow: hidden;
  }

  .drawer-scrim {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: block;
    background: rgba(15, 23, 42, 0.38);
    opacity: 0;
    pointer-events: none;
    transition: opacity 160ms ease;
  }

  body.history-open .drawer-scrim {
    opacity: 1;
    pointer-events: auto;
  }

  .history-panel {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 11;
    display: flex;
    width: min(320px, 84vw);
    min-height: var(--app-height);
    max-height: none;
    flex-direction: column;
    overflow: hidden;
    border-right: 1px solid var(--border);
    border-bottom: 0;
    background: var(--surface);
    box-shadow: 24px 0 48px rgba(15, 23, 42, 0.18);
    padding: calc(16px + env(safe-area-inset-top)) 14px 16px;
    transform: translateX(-100%);
    transition: transform 180ms ease;
  }

  body.history-open .history-panel {
    transform: translateX(0);
  }

  .history-header {
    margin-bottom: 12px;
  }

  .history-title {
    margin: 0;
    color: var(--text);
    font-size: 16px;
    white-space: nowrap;
  }

  .drawer-section {
    padding: 14px 0;
  }

  .history-section {
    min-height: 0;
    flex: 1;
  }

  .history-close {
    display: grid;
    width: 36px;
    min-width: 36px;
    height: 36px;
    place-items: center;
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--text);
    padding: 0;
    font-size: 22px;
    line-height: 1;
  }

  .history-list {
    min-height: 0;
    flex-direction: column;
    overflow-y: auto;
    gap: 8px;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .history-item {
    width: 100%;
    min-height: 44px;
    border-color: var(--border);
    border-radius: 8px;
    background: #fff;
    padding: 4px;
    font-size: 14px;
  }

  .history-title-button {
    min-height: 36px;
    padding: 8px;
    font-size: 14px;
  }

  .history-delete {
    opacity: 1;
  }

  .history-empty {
    white-space: nowrap;
  }

  .chat {
    height: 100%;
    padding: 12px;
  }

  .chat-tip {
    margin-bottom: 12px;
    border-radius: 8px;
    padding: 8px 10px;
  }

  .messages {
    min-width: 0;
  }

  .message {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .message-content {
    max-width: calc(100vw - 74px);
  }

  .avatar {
    width: 28px;
    height: 28px;
  }

  .bubble {
    width: calc(100vw - 74px);
    max-width: calc(100vw - 74px);
    font-size: 15px;
    white-space: normal;
    word-break: break-all;
  }

  .copy-button {
    opacity: 1;
  }

  .composer {
    width: 100vw;
    max-width: 100vw;
    overflow: hidden;
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  .composer-box {
    position: relative;
    display: flex;
    flex-direction: row;
    align-items: center;
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
    gap: 8px;
  }

  button {
    min-width: 68px;
    padding-inline: 12px;
  }

  .send-button {
    position: static;
    flex: 0 0 44px;
    width: 44px;
    min-width: 44px;
    padding: 0;
  }

  button.secondary {
    min-width: 100%;
  }
}
