:root {
  color-scheme: light;
  --bg: #f5f3ee;
  --panel: #fffdfa;
  --panel-muted: #ece7dd;
  --ink: #171717;
  --muted: #6d665c;
  --line: #ded6ca;
  --accent: #1f7a69;
  --accent-strong: #0d5c50;
  --user: #e4f2ee;
  --assistant: #ffffff;
  --danger: #9f2d20;
  --sidebar-width: 300px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(31, 122, 105, 0.24);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  min-height: 620px;
}

.sidebar {
  position: relative;
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 18px;
  border-right: 1px solid var(--line);
  background: #eee8dc;
  padding: 20px;
}

.brand {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
}

.mobile-sidebar-button,
.mobile-sidebar-close,
.sidebar-scrim {
  display: none;
}

.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 8px;
  background: var(--ink);
  color: white;
  font-weight: 800;
}

.brand h1,
.brand p,
.topbar h2,
.topbar p {
  margin: 0;
}

.brand h1 {
  font-size: 18px;
}

.brand p {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.new-chat,
.send-button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 700;
}

.new-chat {
  min-height: 42px;
}

.conversation-list {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}

.sidebar-width-control,
.speech-voice-control {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.sidebar-width-control input {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.speech-voice-control select {
  min-height: 38px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  padding: 6px 8px;
}

.speech-voice-control small {
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}

.sidebar-resizer {
  position: absolute;
  top: 0;
  right: -5px;
  z-index: 2;
  width: 10px;
  height: 100%;
  cursor: col-resize;
  touch-action: none;
}

.sidebar-resizer::after {
  position: absolute;
  top: 20px;
  right: 4px;
  width: 2px;
  height: calc(100% - 40px);
  border-radius: 999px;
  background: transparent;
  content: "";
  transition: background 120ms ease;
}

.sidebar-resizer:hover::after,
.sidebar-resizer:focus-visible::after,
.sidebar-resizer.dragging::after {
  background: var(--accent);
}

.sidebar-resizer:focus-visible {
  outline: 0;
}

.conversation-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 4px;
}

.conversation-row.active,
.conversation-row:hover,
.conversation-row:focus-within {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.62);
}

.conversation-title {
  min-width: 0;
  min-height: 34px;
  border: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  overflow: hidden;
  padding: 6px;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 120ms ease;
}

.conversation-row:hover .conversation-actions,
.conversation-row:focus-within .conversation-actions {
  opacity: 1;
}

.conversation-action {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdfa;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
  padding: 4px 7px;
}

.conversation-action:hover,
.conversation-action.primary {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.conversation-action.danger:hover {
  border-color: rgba(159, 45, 32, 0.42);
  color: var(--danger);
}

.conversation-input {
  min-width: 0;
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  outline: none;
  padding: 6px 8px;
}

.conversation-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 105, 0.14);
}

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  background: var(--panel);
}

.topbar {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding: 18px 24px;
}

.eyebrow {
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.topbar h2 {
  margin-top: 2px;
  font-size: 20px;
}

.mode-control {
  display: grid;
  grid-template-columns: repeat(3, minmax(72px, 1fr));
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-muted);
}

.mode-button {
  min-height: 36px;
  border: 0;
  border-right: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
}

.mode-button:last-child {
  border-right: 0;
}

.mode-button.active {
  background: var(--ink);
  color: white;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: auto;
  padding: 24px;
}

.empty-state {
  display: grid;
  max-width: 720px;
  min-height: 100%;
  align-content: center;
  gap: 18px;
  margin: 0 auto;
  text-align: center;
}

.empty-state h3 {
  margin: 0;
  font-size: 32px;
  line-height: 1.1;
}

.empty-state p {
  margin: 0 auto;
  max-width: 560px;
  color: var(--muted);
  line-height: 1.6;
}

.starter-prompts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.starter-prompts button {
  min-height: 84px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  cursor: pointer;
  padding: 12px;
  text-align: left;
}

.message {
  display: grid;
  max-width: 860px;
  gap: 6px;
}

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

.message.assistant {
  align-self: flex-start;
}

.message-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.bubble {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 13px 15px;
  line-height: 1.58;
  white-space: pre-wrap;
}

.bubble.markdown {
  white-space: normal;
}

.message-content {
  display: grid;
  gap: 8px;
}

.message-attachments {
  display: flex;
  max-width: 680px;
  flex-wrap: wrap;
  gap: 8px;
}

.message-attachment {
  display: grid;
  min-width: 180px;
  max-width: 320px;
  grid-template-columns: 40px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 8px;
}

.message-attachment.image {
  width: min(320px, 100%);
  grid-template-columns: 1fr;
  padding: 0;
}

.message-attachment img {
  display: block;
  width: 100%;
  max-height: 280px;
  object-fit: contain;
  background: var(--panel-muted);
}

.message-attachment.image > div {
  padding: 0 8px 8px;
}

.file-badge {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border-radius: 6px;
  background: var(--panel-muted);
  color: var(--accent-strong);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.attachment-name,
.attachment-meta {
  overflow: hidden;
  margin: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-name {
  font-size: 13px;
  font-weight: 700;
}

.attachment-meta {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.markdown p {
  margin: 0 0 12px;
}

.markdown p:last-child,
.markdown ul:last-child,
.markdown pre:last-child {
  margin-bottom: 0;
}

.markdown ul {
  margin: 0 0 14px;
  padding-left: 22px;
}

.markdown li {
  margin: 4px 0;
}

.markdown code {
  border: 1px solid var(--line);
  border-radius: 5px;
  background: #f4f0e9;
  padding: 1px 5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}

.markdown pre {
  overflow: auto;
  margin: 0 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #161616;
  color: #f7f2e8;
  padding: 12px;
}

.markdown pre code {
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0;
}

.user .bubble {
  background: var(--user);
}

.assistant .bubble {
  background: var(--assistant);
}

.assistant.error .bubble {
  border-color: rgba(159, 45, 32, 0.35);
  color: var(--danger);
}

.speech-controls {
  display: flex;
  min-height: 32px;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.speech-action {
  min-height: 30px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
  padding: 5px 9px;
}

.speech-action:hover,
.speech-action:focus-visible {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.speech-action:disabled {
  cursor: wait;
  opacity: 0.65;
}

.speech-status {
  color: var(--muted);
  font-size: 11px;
}

.speech-status.error {
  color: var(--danger);
}

.composer {
  border-top: 1px solid var(--line);
  padding: 16px 24px 20px;
}

.composer.dragging {
  background: var(--user);
}

.composer-row {
  display: grid;
  grid-template-columns: 48px 76px minmax(0, 1fr) 96px;
  gap: 10px;
}

.recording-status {
  display: flex;
  gap: 8px;
  align-items: center;
  min-height: 30px;
  padding: 0 0 10px 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.recording-status[hidden] {
  display: none;
}

.recording-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  animation: recording-pulse 1.4s ease-in-out infinite;
}

.recording-status.transcribing .recording-dot {
  background: var(--accent);
  animation: none;
}

.recording-status button {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
}

@keyframes recording-pulse {
  50% {
    opacity: 0.35;
  }
}

.attachment-preview {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 10px;
}

.attachment-preview[hidden] {
  display: none;
}

.attachment-chip {
  position: relative;
  display: grid;
  min-width: 190px;
  max-width: 260px;
  grid-template-columns: 40px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  padding: 8px 34px 8px 8px;
}

.attachment-chip img {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  object-fit: cover;
}

.remove-attachment {
  position: absolute;
  top: 5px;
  right: 5px;
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
}

.remove-attachment:hover,
.remove-attachment:focus-visible {
  background: var(--panel-muted);
  color: var(--danger);
}

.attach-button {
  width: 48px;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  cursor: pointer;
  font-size: 28px;
  line-height: 1;
}

.attach-button:hover,
.attach-button:focus-visible {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.dictate-button {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  cursor: pointer;
  font-size: 13px;
  font-weight: 800;
}

.dictate-button:hover,
.dictate-button:focus-visible {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.dictate-button.recording {
  border-color: var(--danger);
  background: var(--danger);
  color: white;
}

.attach-button:disabled,
.dictate-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.composer textarea {
  max-height: 180px;
  min-height: 48px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  outline: none;
  padding: 13px 14px;
  line-height: 1.45;
}

.composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31, 122, 105, 0.14);
}

.send-button {
  min-height: 48px;
}

.send-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.composer-error {
  margin: 8px 0 0 58px;
  color: var(--danger);
  font-size: 12px;
}

@media (max-width: 820px) {
  body.sidebar-open {
    overflow: hidden;
  }

  .app-shell {
    display: block;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(88vw, 340px);
    border-right: 1px solid var(--line);
    padding-top: max(20px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    box-shadow: 12px 0 30px rgba(23, 23, 23, 0.18);
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

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

  .sidebar-scrim {
    position: fixed;
    inset: 0;
    z-index: 15;
    display: block;
    border: 0;
    background: rgba(23, 23, 23, 0.38);
    cursor: pointer;
    opacity: 0;
    pointer-events: none;
    transition: opacity 180ms ease;
  }

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

  .mobile-sidebar-close,
  .mobile-sidebar-button {
    display: grid;
    width: 44px;
    height: 44px;
    flex: 0 0 44px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--ink);
    cursor: pointer;
  }

  .mobile-sidebar-close {
    font-size: 28px;
    line-height: 1;
  }

  .menu-icon,
  .menu-icon::before,
  .menu-icon::after {
    display: block;
    width: 19px;
    height: 2px;
    border-radius: 2px;
    background: currentColor;
    content: "";
  }

  .menu-icon::before {
    transform: translateY(-6px);
  }

  .menu-icon::after {
    transform: translateY(4px);
  }

  .conversation-list {
    max-height: none;
  }

  .sidebar-width-control,
  .sidebar-resizer {
    display: none;
  }

  .topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    gap: 10px 12px;
    align-items: center;
    padding: max(12px, env(safe-area-inset-top)) 14px 12px;
  }

  .topbar-title {
    min-width: 0;
  }

  .topbar-title h2 {
    overflow: hidden;
    font-size: 18px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .mode-control {
    width: 100%;
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .chat-panel {
    height: 100vh;
    height: 100dvh;
  }

  .messages {
    gap: 12px;
    min-height: 0;
    overscroll-behavior: contain;
    padding: 16px 14px;
  }

  .empty-state {
    width: 100%;
    align-content: center;
    gap: 14px;
  }

  .empty-state h3 {
    font-size: 26px;
  }

  .starter-prompts {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .starter-prompts button {
    min-height: 64px;
  }

  .message {
    max-width: 92%;
  }

  .bubble {
    overflow-wrap: anywhere;
    padding: 11px 12px;
  }

  .message-attachments,
  .message-attachment {
    max-width: 100%;
  }

  .conversation-actions {
    opacity: 1;
  }

  .composer {
    padding: 10px 12px max(12px, env(safe-area-inset-bottom));
  }

  .composer-row {
    grid-template-columns: 44px 68px minmax(0, 1fr) 56px;
    gap: 8px;
  }

  .attach-button {
    width: 44px;
    min-height: 44px;
  }

  .dictate-button,
  .send-button,
  .composer textarea {
    min-height: 44px;
  }

  .dictate-button {
    font-size: 12px;
  }

  .send-button {
    font-size: 13px;
  }

  .composer textarea {
    padding: 10px 11px;
  }

  .composer-error {
    margin-left: 0;
  }

  .attachment-chip {
    min-width: min(240px, 78vw);
  }
}

@media (max-width: 359px) {
  .composer-row {
    grid-template-columns: 44px 64px minmax(0, 1fr);
  }

  .send-button {
    grid-column: 1 / -1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar,
  .sidebar-scrim {
    transition: none;
  }

  .recording-dot {
    animation: none;
  }
}
