:root {
  color-scheme: light;
  --canvas: #eef1f0;
  --surface: #ffffff;
  --surface-soft: #f7f8f7;
  --sidebar: #182321;
  --sidebar-soft: #24312f;
  --line: #d8dedc;
  --line-strong: #bbc7c3;
  --text: #18211f;
  --muted: #66716e;
  --accent: #0d766e;
  --accent-soft: #e2f3f0;
  --success: #1f7a4e;
  --warning: #b8642b;
  --danger: #a43d3d;
  --shadow: 0 14px 36px rgba(20, 35, 31, .12);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
html { background: var(--canvas); }
body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  color: var(--text);
  background: var(--canvas);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled, textarea:disabled { cursor: not-allowed; opacity: .52; }

.app-shell {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  overflow: hidden;
}

.project-sidebar {
  position: relative;
  z-index: 20;
  min-width: 0;
  display: flex;
  flex-direction: column;
  color: #f5f8f7;
  background: var(--sidebar);
  border-right: 1px solid #31413e;
}

.brand-row {
  min-height: 72px;
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 36px;
  align-items: center;
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid #31413e;
}
.brand-row strong, .brand-row small { display: block; }
.brand-row strong { font-size: 15px; }
.brand-row small { margin-top: 3px; color: #a9b7b3; font-size: 12px; }
.brand-mark {
  position: relative;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid #4aa49b;
  border-radius: 8px;
  background: #0f6b64;
}
.brand-mark::before, .brand-mark span {
  content: "";
  display: block;
  border: 2px solid #e5fffa;
  border-radius: 50%;
}
.brand-mark::before { width: 15px; height: 15px; }
.brand-mark span { position: absolute; width: 7px; height: 7px; background: #e5fffa; }

.project-list { flex: 1; min-height: 0; padding: 12px 10px; overflow: auto; }
.project-item {
  width: 100%;
  min-height: 58px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) 8px;
  align-items: center;
  gap: 9px;
  padding: 8px 10px;
  color: #e9f0ee;
  text-align: left;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
}
.project-item:hover, .project-item.is-active {
  background: var(--sidebar-soft);
  border-color: #3b4d49;
}
.project-item strong, .project-item small { display: block; overflow: hidden; text-overflow: ellipsis; }
.project-item strong { font-size: 14px; white-space: nowrap; }
.project-item small { margin-top: 3px; color: #9fb0ac; font-size: 11px; white-space: nowrap; }
.project-glyph {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  color: #c6fff6;
  background: #0d5e58;
  font: 700 12px/1 Consolas, monospace;
}
.project-active { width: 7px; height: 7px; border-radius: 50%; background: #5ad497; }

.sidebar-links { display: flex; gap: 14px; padding: 10px 14px; border-top: 1px solid #31413e; }
.sidebar-links a { display: inline-flex; color: #c5d4d0; font-size: 13px; text-decoration: none; }
.sidebar-links a:hover { color: #ffffff; text-decoration: underline; }
.sidebar-footer {
  min-height: 64px;
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  padding: 10px 14px;
  border-top: 1px solid #31413e;
}
.user-avatar {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #15332f;
  background: #bde6df;
  font-size: 13px;
  font-weight: 700;
}
.user-name { overflow: hidden; color: #dce6e3; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-footer form { margin: 0; }
.logout-button {
  min-height: 30px;
  padding: 0 8px;
  color: #becbc8;
  border: 0;
  border-radius: 5px;
  background: transparent;
  font-size: 12px;
}
.logout-button:hover { color: #ffffff; background: #2b3b38; }

.chat-shell {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 72px minmax(0, 1fr) auto auto;
  background: var(--surface-soft);
}
.chat-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  align-items: center;
  gap: 12px;
  padding: 11px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, .96);
}
.chat-title { min-width: 0; }
.chat-title h1 { margin: 0; font-size: 17px; line-height: 1.3; }
.connection-line { min-width: 0; display: flex; align-items: center; gap: 7px; margin-top: 4px; color: var(--muted); font-size: 12px; }
.connection-line span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.connection-dot { width: 8px; height: 8px; flex: 0 0 8px; border-radius: 50%; background: #a0aaa7; }
.connection-dot[data-state="ready"] { background: var(--success); }
.connection-dot[data-state="unavailable"] { background: var(--danger); }
.connection-dot[data-state="checking"] { background: var(--warning); }

.icon-button {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  padding: 0;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  font-size: 20px;
  line-height: 1;
}
.icon-button:hover { border-color: var(--line-strong); background: #f3f6f5; }
.sidebar-open, .sidebar-close { display: none; }

.chat-history {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  padding: 26px max(22px, calc((100% - 880px) / 2));
}
.chat-empty {
  min-height: 100%;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  color: var(--muted);
}
.empty-mark {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  color: #f0fffc;
  border-radius: 8px;
  background: var(--accent);
  font: 700 16px/1 Consolas, monospace;
}
.chat-empty strong { font-size: 14px; font-weight: 600; }
.message-row { width: 100%; display: flex; margin: 0 0 18px; }
.message-row[data-role="user"] { justify-content: flex-end; }
.message-block { max-width: min(78%, 720px); min-width: 0; }
.message-meta { margin: 0 0 6px; color: var(--muted); font-size: 11px; }
.message-row[data-role="user"] .message-meta { text-align: right; }
.message-content {
  margin: 0;
  padding: 12px 14px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 2px 5px rgba(20, 35, 31, .04);
  font: 14px/1.55 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
}
.message-row[data-role="user"] .message-content { border-color: #afd8d2; background: var(--accent-soft); }
.message-row[data-state="running"] .message-content { color: var(--muted); border-style: dashed; }
.message-row[data-state="failed"] .message-content { color: var(--danger); border-color: #e0b7b7; background: #fff7f7; }

.chat-notice {
  margin: 0 max(18px, calc((100% - 880px) / 2)) 8px;
  padding: 9px 12px;
  color: #733a16;
  border: 1px solid #e4bc9f;
  border-radius: 7px;
  background: #fff7ef;
  font-size: 13px;
}
.chat-composer {
  margin: 0 max(18px, calc((100% - 880px) / 2));
  padding: 10px 0 max(14px, env(safe-area-inset-bottom));
}
.chat-composer textarea {
  width: 100%;
  min-height: 54px;
  max-height: 180px;
  display: block;
  resize: none;
  padding: 15px 15px 8px;
  color: var(--text);
  border: 1px solid var(--line-strong);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  outline: 0;
  background: var(--surface);
  line-height: 1.45;
}
.chat-composer textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(13, 118, 110, .12); }
.composer-actions {
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--line-strong);
  border-top: 0;
  border-radius: 0 0 8px 8px;
  background: var(--surface);
}
.composer-actions > span { margin-right: auto; color: var(--muted); font-size: 11px; }
.composer-actions button {
  min-height: 34px;
  padding: 0 14px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  color: #ffffff;
  background: var(--accent);
  font-weight: 650;
}
.composer-actions .secondary-button { color: var(--text); border-color: var(--line); background: #f2f5f4; }

.sidebar-backdrop { position: fixed; inset: 0; z-index: 15; background: rgba(15, 25, 22, .42); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.auth-page { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; overflow: auto; padding: 24px; background: var(--canvas); }
.auth-card { width: min(440px, 100%); padding: 28px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); box-shadow: var(--shadow); }
.auth-card h1 { margin: 4px 0 3px; font-size: 24px; }
.auth-card h2 { margin: 26px 0 10px; font-size: 15px; }
.auth-card p { color: var(--muted); line-height: 1.5; }
.auth-brand { position: relative; width: 42px; height: 42px; display: grid; place-items: center; margin-bottom: 18px; border: 1px solid #4aa49b; border-radius: 8px; background: #0f6b64; }
.auth-brand::before, .auth-brand span { content: ""; display: block; border: 2px solid #e5fffa; border-radius: 50%; }
.auth-brand::before { width: 17px; height: 17px; }
.auth-brand span { position: absolute; width: 8px; height: 8px; background: #e5fffa; }
.auth-card .auth-eyebrow { margin: 0; color: var(--accent); font-size: 12px; font-weight: 700; }
.auth-card .auth-subtitle { margin: 0 0 22px; }
.auth-message { min-height: 38px; margin: 0 0 12px; padding: 9px 10px; color: var(--muted); border-left: 3px solid var(--line-strong); background: #f5f7f6; font-size: 13px; line-height: 1.45; }
.auth-message:empty { visibility: hidden; }
.auth-message[data-state="ready"] { color: var(--success); border-color: var(--success); }
.auth-message[data-state="error"] { color: var(--danger); border-color: var(--danger); }
.auth-login-actions { display: grid; gap: 8px; }
.primary-auth-button, .secondary-auth-button, .phone-auth-button { min-height: 42px; padding: 0 16px; border-radius: 6px; font-weight: 700; }
.primary-auth-button { width: 100%; color: #fff; border: 1px solid var(--accent); background: var(--accent); }
.primary-auth-button:hover { background: #095f59; }
.phone-auth-button { width: 100%; color: var(--accent); border: 1px solid var(--accent); background: #fff; }
.phone-auth-button:hover { color: #fff; background: var(--accent); }
.secondary-auth-button { color: var(--text); border: 1px solid var(--line-strong); background: #f1f4f3; }
.secondary-auth-button:hover { border-color: var(--accent); }
.device-flow { margin-top: 18px; border-top: 1px solid var(--line); }
.device-flow h2 { margin-bottom: 12px; }
.device-qr-frame { width: min(220px, 100%); aspect-ratio: 1; margin: 0 auto 12px; border: 1px solid var(--line); background: #fff; }
.device-qr-frame img { width: 100%; height: 100%; display: block; object-fit: contain; }
.device-code-row { display: grid; justify-items: center; gap: 5px; padding: 10px; border: 1px solid var(--line); background: var(--surface-soft); }
.device-code-row span { color: var(--muted); font-size: 12px; font-weight: 650; }
.device-code-row output { min-height: 30px; color: var(--text); font: 700 22px/1.35 ui-monospace, SFMono-Regular, Consolas, monospace; }
.device-flow-footer { min-height: 42px; display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 10px; }
.device-flow-footer > span { min-width: 48px; color: var(--muted); font: 600 13px/1 ui-monospace, SFMono-Regular, Consolas, monospace; }
.device-cancel-button { min-height: 38px; padding: 0 14px; color: var(--danger); border: 1px solid #e2bcbc; border-radius: 6px; background: #fff7f7; font-weight: 650; }
.device-cancel-button:hover { border-color: var(--danger); }
.device-approval { display: grid; gap: 14px; }
.auth-setup { margin-top: 18px; border-top: 1px solid var(--line); }
.auth-setup summary { padding: 16px 0 4px; color: var(--muted); cursor: pointer; font-size: 13px; font-weight: 650; }
.auth-setup form, .passkey-add-form { display: grid; gap: 8px; margin-top: 12px; }
.auth-card label { color: var(--muted); font-size: 12px; font-weight: 650; }
.auth-card input { width: 100%; min-height: 42px; padding: 8px 10px; color: var(--text); border: 1px solid var(--line-strong); border-radius: 6px; background: #fff; }
.auth-card input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(13, 118, 110, .12); }
.auth-setup .secondary-auth-button { margin-top: 5px; }
.auth-back { color: var(--accent); font-size: 13px; text-decoration: none; }
.auth-back:hover { text-decoration: underline; }
.passkey-manager { width: min(560px, 100%); }
.passkey-add-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; }
.passkey-add-row .primary-auth-button { width: auto; }
.passkey-list { display: grid; gap: 8px; }
.passkey-row { min-height: 62px; display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 12px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 7px; }
.passkey-row strong, .passkey-row small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.passkey-row strong { font-size: 14px; }
.passkey-row small { margin-top: 4px; color: var(--muted); font-size: 11px; }
.passkey-remove { min-height: 34px; padding: 0 10px; color: var(--danger); border: 1px solid #e2bcbc; border-radius: 5px; background: #fff7f7; }

@media (max-width: 760px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); }
  .project-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(286px, calc(100vw - 42px));
    transform: translateX(-100%);
    transition: transform .18s ease;
  }
  .project-sidebar[data-open="true"] { transform: translateX(0); }
  .sidebar-open, .sidebar-close { display: grid; }
  .sidebar-close { color: #e6efec; border-color: #3c4d49; background: #263532; }
  .chat-toolbar { grid-template-columns: 38px minmax(0, 1fr) 38px; padding: 9px 12px; }
  .chat-history { padding: 18px 12px; }
  .message-block { max-width: 90%; }
  .chat-composer { margin: 0 10px; }
  .chat-notice { margin-right: 10px; margin-left: 10px; }
  .auth-page { padding: 14px; }
  .auth-card { padding: 22px 18px; }
  .passkey-add-row { grid-template-columns: minmax(0, 1fr); }
  .passkey-add-row .primary-auth-button { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0s !important; }
}
