:root {
  color-scheme: light;
  --page: #efe7d9;
  --surface: #f7f2e8;
  --surface-raised: #fcf9f2;
  --surface-soft: #ece7d9;
  --surface-hover: #e5ddcc;
  --line: #ddd1bc;
  --line-strong: #9b8e77;
  --text: #2f291f;
  --text-secondary: #625b4e;
  --text-muted: #6d6456;
  --accent: #747858;
  --accent-deep: #434c37;
  --accent-soft: #e4e5d6;
  --success: #65744c;
  --danger: #915448;
  --brass: #c3af82;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --shadow-composer: 0 12px 36px rgb(53 44 30 / 8%), 0 2px 8px rgb(53 44 30 / 4%);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", "PingFang SC", sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-weight: 400;
  color: var(--text);
  background: var(--page);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.auth-locked .app-shell, .auth-locked .mobile-drawer, .auth-locked .drawer-backdrop { display: none !important; }
.login-screen { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(100%, 380px); padding: 32px; border: 1px solid var(--line); border-radius: 24px; background: var(--surface); }
.login-card h1 { font-size: 25px; font-weight: 500; margin: 20px 0 8px; }
.login-card p { color: var(--text-secondary); line-height: 1.6; }
.login-card label { display: block; margin: 24px 0 8px; font-size: 14px; }
.login-card input { width: 100%; padding: 12px; border: 1px solid var(--line-strong); border-radius: 10px; background: var(--surface-raised); color: var(--text); }
.login-card button { width: 100%; margin-top: 16px; padding: 12px; border: 0; border-radius: 10px; background: var(--accent-deep); color: var(--surface-raised); cursor: pointer; }
.login-card .login-error { min-height: 24px; font-size: 13px; color: var(--danger); }
.tool-audit { margin-top: 12px; font-size: 12px; color: var(--text-secondary); overflow-wrap: anywhere; }
.tool-audit summary { cursor: pointer; }
.tool-audit pre { white-space: pre-wrap; font: inherit; margin: 8px 0; padding: 10px; border-radius: 8px; background: var(--surface-soft); }
html, body { width: 100%; min-width: 320px; min-height: 100%; margin: 0; }
body { min-height: 100dvh; overflow: hidden; background: var(--page); font-size: 16px; }
button, input, textarea { font: inherit; }
button { color: inherit; touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button:disabled { cursor: not-allowed; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
kbd { color: var(--text-muted); font-family: inherit; font-size: 12px; line-height: 1.2; }

.app-shell { display: grid; width: 100%; height: 100dvh; grid-template-columns: 1fr; }
.main-panel { position: relative; display: grid; grid-template-rows: 60px minmax(0, 1fr) auto; min-width: 0; height: 100dvh; background: var(--surface); }
.topbar { z-index: 2; display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: var(--space-2); min-height: 60px; padding: 0 max(var(--space-4), env(safe-area-inset-right)) 0 max(var(--space-4), env(safe-area-inset-left)); border-bottom: 1px solid rgb(221 209 188 / 72%); background: rgb(247 242 232 / 95%); }
.icon-button { display: inline-grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: var(--radius-md); background: transparent; color: var(--text-secondary); cursor: pointer; transition: background-color 140ms ease-out, color 140ms ease-out, transform 140ms ease-out; }
.icon-button:hover:not(:disabled) { background: var(--surface-hover); color: var(--text); }
.icon-button:active:not(:disabled), .new-chat-button:active { transform: scale(.97); }
.icon-button:disabled { opacity: .36; cursor: not-allowed; }
.install-button[hidden] { display: none; }
.brand-mark { display: inline-grid; place-items: center; flex: 0 0 28px; width: 28px; height: 28px; border: 1px solid var(--brass); border-radius: 50%; background: var(--accent-soft); color: var(--accent-deep); font-family: Georgia, serif; font-size: 19px; font-style: italic; font-weight: 600; line-height: 1; }
.topbar-title { display: flex; align-items: center; justify-content: center; gap: var(--space-3); min-width: 0; }
.desktop-title { display: inline; max-width: min(56vw, 360px); overflow: hidden; color: var(--text); font-size: 14px; font-weight: 550; text-overflow: ellipsis; white-space: nowrap; }
.mobile-only { display: inline-grid; }
.conversation-view { position: relative; min-height: 0; overflow: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: #c5b9a4 transparent; }
.messages { display: flex; flex-direction: column; gap: var(--space-6); width: 100%; max-width: 780px; min-height: 100%; margin-inline: auto; padding: 48px 22px 36px; }
.empty-state { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: center; min-height: 330px; padding: 24px 0 62px; text-align: center; animation: appear 220ms ease-out both; }
.empty-mark { display: grid; place-items: center; width: 52px; height: 52px; margin-bottom: 22px; border-radius: 50%; background: var(--accent-soft); color: var(--accent-deep); }
.empty-mark .brand-mark { width: 100%; height: 100%; border: 1px solid rgb(128 91 66 / 10%); font-size: 31px; }
.empty-state h1 { margin: 0; color: var(--text); font-family: Georgia, "Noto Serif SC", serif; font-size: clamp(27px, 6vw, 33px); font-weight: 400; letter-spacing: -.035em; line-height: 1.3; text-wrap: balance; }
.empty-state p { max-width: 300px; margin: 10px 0 0; color: var(--text-secondary); font-size: 15px; line-height: 1.65; }
.empty-caption { margin-top: 27px; color: var(--text-muted); font-size: 12px; letter-spacing: .015em; }
.message { display: flex; flex-direction: column; width: 100%; animation: appear 180ms ease-out both; }
.message.user { align-items: flex-end; }
.message.assistant { align-items: flex-start; }
.message-label { display: flex; align-items: center; gap: 8px; margin: 0 0 10px 2px; color: var(--text-secondary); font-size: 13px; font-weight: 550; }
.message-label .brand-mark { width: 22px; height: 22px; flex-basis: 22px; font-size: 15px; }
.message-content { max-width: min(100%, 680px); color: var(--text); font-size: 16px; line-height: 1.76; overflow-wrap: anywhere; white-space: pre-wrap; }
.message.user .message-content { width: fit-content; max-width: min(88%, 620px); padding: 12px 16px; border-radius: 18px 18px 5px 18px; background: #e9e9dc; color: var(--text); line-height: 1.65; }
.message.assistant .message-content { padding: 0 2px; }
.message-time { margin-top: 8px; color: var(--text-muted); font-size: 11px; }
.message.assistant + .message.user { margin-top: var(--space-2); }
.typing { display: inline-flex; align-items: center; gap: 5px; min-height: 28px; color: var(--text-muted); }
.typing span { width: 5px; height: 5px; border-radius: 50%; background: #928a76; animation: pulse 1s ease-in-out infinite; }
.typing span:nth-child(2) { animation-delay: 120ms; }
.typing span:nth-child(3) { animation-delay: 240ms; }

.composer-zone { z-index: 2; width: 100%; padding: 12px 16px max(14px, env(safe-area-inset-bottom)); background: linear-gradient(180deg, rgb(252 251 249 / 0%) 0, rgb(252 251 249 / 92%) 16px, var(--surface) 42px); }
.composer { width: 100%; max-width: 760px; margin: 0 auto; padding: 14px 14px 10px; border: 1px solid var(--line-strong); border-radius: 22px; background: var(--surface-raised); box-shadow: var(--shadow-composer); transition: border-color 140ms ease-out, box-shadow 140ms ease-out; }
.composer:focus-within { border-color: var(--accent); box-shadow: 0 12px 36px rgb(53 44 30 / 9%), 0 0 0 3px rgb(116 120 88 / 12%); }
.composer textarea { display: block; width: 100%; min-height: 30px; max-height: 176px; resize: none; padding: 2px 2px 8px; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 16px; line-height: 1.55; }
.composer textarea::placeholder { color: var(--text-muted); opacity: 1; }
.composer textarea:focus-visible { outline: none; }
.composer-actions { display: flex; align-items: center; gap: var(--space-2); }
.composer-spacer { flex: 1; }
.send-button { display: inline-grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 14px; background: var(--accent-deep); color: #fff; cursor: pointer; transition: background-color 140ms ease-out, transform 140ms ease-out, opacity 140ms ease-out; }
.send-button:hover:not(:disabled) { background: #353e2e; }
.send-button:active:not(:disabled) { transform: scale(.96); }
.send-button:disabled { background: #e4dfd2; color: var(--text-muted); cursor: not-allowed; }
.send-button svg { width: 19px; height: 19px; stroke-width: 2; }
.composer-caption { max-width: 760px; margin: 10px auto 0; color: var(--text-muted); font-size: 11px; line-height: 1.4; text-align: center; }
.composer-caption.error { color: #8a3e32; }

.desktop-sidebar { display: none; }
.drawer-backdrop { position: fixed; z-index: 8; inset: 0; background: rgb(36 32 27 / 34%); opacity: 0; transition: opacity 180ms ease-out; }
.drawer-backdrop.visible { opacity: 1; }
.mobile-drawer { position: fixed; z-index: 9; inset: 0 auto 0 0; display: flex; flex-direction: column; width: min(88vw, 350px); padding: max(18px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom)); border-right: 1px solid var(--line); background: var(--surface); transform: translateX(-102%); transition: transform 220ms ease-out; overscroll-behavior: contain; }
.mobile-drawer.open { transform: translateX(0); }
.drawer-top { display: flex; align-items: center; justify-content: space-between; min-height: 46px; margin-bottom: 20px; padding-inline: 4px; }
.sidebar-brand { display: flex; align-items: center; gap: 9px; color: var(--text); font-family: Georgia, "Noto Serif SC", serif; font-size: 19px; font-weight: 600; letter-spacing: -.04em; }
.new-chat-button { display: flex; align-items: center; gap: 11px; width: 100%; min-height: 46px; padding: 0 12px; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--surface-raised); color: var(--text); text-align: left; cursor: pointer; transition: background-color 140ms ease-out, transform 140ms ease-out; }
.new-chat-button:hover { background: var(--surface-hover); }
.new-chat-button svg { width: 18px; height: 18px; color: var(--accent); }
.new-chat-button span { flex: 1; font-size: 14px; font-weight: 550; }
.search-box { display: flex; align-items: center; gap: 9px; min-height: 44px; margin-top: 12px; padding: 0 11px; border: 1px solid transparent; border-radius: var(--radius-md); background: var(--surface-soft); color: var(--text-muted); }
.search-box:focus-within { border-color: var(--accent); background: var(--surface-raised); }
.search-box svg { flex: 0 0 18px; width: 18px; height: 18px; }
.search-box input { min-width: 0; flex: 1; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 14px; }
.search-box input::placeholder { color: var(--text-muted); }
.search-box kbd { display: none; }
.history-heading { margin: 22px 8px 10px; color: var(--text-muted); font-size: 12px; font-weight: 550; }
.conversation-list { display: flex; flex-direction: column; gap: 3px; flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain; }
.conversation-item { display: flex; align-items: center; gap: 8px; width: 100%; min-height: 42px; padding: 0 10px; border: 0; border-radius: 10px; background: transparent; color: var(--text-secondary); text-align: left; cursor: pointer; }
.conversation-item:hover { background: var(--surface-hover); color: var(--text); }
.conversation-item.active { background: var(--accent-soft); color: var(--text); }
.conversation-item span { flex: 1; min-width: 0; overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.conversation-item small { color: var(--text-muted); font-size: 11px; }
.history-empty { padding: 14px 10px; color: var(--text-muted); font-size: 13px; line-height: 1.5; }
.sidebar-bottom { padding-top: 14px; border-top: 1px solid var(--line); }
.local-note { display: flex; align-items: center; gap: 9px; padding: 8px 9px 14px; color: var(--text-muted); font-size: 12px; }
.local-note-icon { display: grid; place-items: center; width: 18px; height: 18px; border: 1px solid var(--line-strong); border-radius: 50%; font-size: 12px; }
.data-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 7px; }
.data-action { display: flex; align-items: center; justify-content: center; gap: 6px; min-width: 0; min-height: 44px; padding: 0 6px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-raised); color: var(--text-secondary); font-size: 12px; cursor: pointer; transition: background-color 140ms ease-out, border-color 140ms ease-out, color 140ms ease-out; }
.data-action:hover:not(:disabled) { border-color: var(--line-strong); background: var(--surface-hover); color: var(--text); }
.data-action:active:not(:disabled) { transform: scale(.98); }
.data-action:disabled { opacity: .55; }
.data-action svg { flex: 0 0 16px; width: 16px; height: 16px; color: var(--accent); }
.drawer-data-actions { margin-top: 12px; }
.sidebar-user { display: flex; align-items: center; gap: 10px; min-height: 46px; padding: 4px 2px 0; }
.user-avatar { display: grid; place-items: center; flex: 0 0 32px; width: 32px; height: 32px; border: 1px solid #cbd0b7; border-radius: 50%; background: #e5e7d7; color: var(--accent-deep); font-family: Georgia, serif; font-size: 13px; }
.user-name { flex: 1; color: var(--text-secondary); font-size: 13px; }
.drawer-bottom { display: flex; align-items: center; gap: 10px; min-height: 54px; margin-top: 16px; padding: 12px 6px 0; border-top: 1px solid var(--line); }
.drawer-bottom div { display: flex; flex-direction: column; gap: 3px; }
.drawer-bottom strong { font-size: 13px; font-weight: 550; }
.drawer-bottom small { color: var(--text-muted); font-size: 11px; }

@keyframes appear { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulse { 0%, 60%, 100% { opacity: .4; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-2px); } }

@media (min-width: 768px) {
  .app-shell { grid-template-columns: 252px minmax(0, 1fr); }
  .desktop-sidebar { display: flex; flex-direction: column; min-height: 0; padding: 22px 14px 14px; border-right: 1px solid var(--line); background: #eee8d9; }
  .desktop-sidebar > .sidebar-brand { margin: 0 8px 24px; }
  .desktop-sidebar .search-box { margin-top: 10px; }
  .desktop-sidebar .search-box kbd { display: inline; }
  .mobile-only, .mobile-drawer, .drawer-backdrop { display: none !important; }
  .topbar { grid-template-columns: 1fr 44px; min-height: 64px; padding-inline: 28px; }
  .topbar-title { justify-content: center; }
  .desktop-title { display: inline; }
  .main-panel { grid-template-rows: 64px minmax(0, 1fr) auto; }
  .messages { padding: 56px 32px 38px; }
  .composer-zone { padding: 16px 30px max(18px, env(safe-area-inset-bottom)); }
}

@media (min-width: 1120px) {
  .app-shell { grid-template-columns: 276px minmax(0, 1fr); }
  .desktop-sidebar { padding-inline: 18px; }
  .topbar { padding-inline: 40px; }
  .composer-zone { padding-inline: 40px; }
}

@media (max-width: 380px) {
  .topbar { padding-inline: 10px; }
  .messages { padding-inline: 16px; }
  .composer-zone { padding-inline: 10px; }
  .composer { padding-inline: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
