:root {
  color-scheme: light;
  --paper: #fff;
  --canvas: #f5f5f3;
  --soft: #f0f0ed;
  --ink: #242424;
  --muted: #73736e;
  --line: #e2e2de;
  --accent: #2f5e8d;
  --accent-soft: #eaf1f7;
  --danger: #a6443e;
  --shadow: 0 8px 24px rgba(25, 25, 20, .06);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; background: var(--canvas); color: var(--ink); font: 14px/1.5 ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }
:focus-visible { outline: 3px solid rgba(47, 94, 141, .28); outline-offset: 2px; }
[hidden] { display: none !important; }

.topbar { display: flex; align-items: center; gap: 12px; height: 58px; padding: 0 22px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,.94); }
.wordmark { color: var(--ink); font-weight: 740; letter-spacing: -.025em; text-decoration: none; }
.connection-status { margin-left: auto; color: var(--muted); font-size: 12px; }
.connection-status.online { color: #29704d; }
.quiet-button, .icon-button { border: 0; background: transparent; color: var(--muted); padding: 7px 9px; }
.quiet-button:hover, .icon-button:hover { color: var(--ink); background: var(--soft); }
.mobile-only { display: none; }

.chat-layout { display: grid; grid-template-columns: 264px minmax(0, 1fr); height: calc(100vh - 58px); }
.sidebar { display: flex; flex-direction: column; min-width: 0; border-right: 1px solid var(--line); background: #f8f8f6; overflow: auto; }
.sidebar-head { padding: 18px 15px 10px; }
.sidebar-actions { display: flex; gap: 7px; margin-top: 12px; }
.section-label { margin: 17px 15px 6px; color: #65655f; font-size: 10px; font-weight: 760; letter-spacing: .09em; text-transform: uppercase; }
.roster, .conversation-list { display: grid; gap: 2px; padding: 0 7px; }
.assistant-row, .conversation-row { display: flex; align-items: center; gap: 9px; width: 100%; min-width: 0; padding: 8px; border: 0; border-left: 2px solid transparent; background: transparent; color: var(--ink); text-align: left; }
.assistant-row:hover, .conversation-row:hover { background: #eeeeeb; }
.assistant-row.selected, .conversation-row.selected { border-left-color: var(--accent); background: var(--paper); }
.avatar { display: grid; place-items: center; flex: none; width: 27px; height: 27px; border: 1px solid #c9d4de; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 800; }
.row-copy { display: grid; min-width: 0; gap: 1px; }
.row-copy strong, .row-copy span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-copy strong { font-size: 12px; }
.row-copy span, .conversation-row small { color: var(--muted); font-size: 11px; }
.conversation-row small { margin-left: auto; font-size: 10px; }
.sidebar-spacer { flex: 1; }
.sidebar-foot { padding: 14px 15px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }

.button { display: inline-flex; align-items: center; justify-content: center; min-height: 34px; padding: 0 11px; border: 1px solid #c9ccc9; background: var(--paper); color: var(--ink); font-size: 12px; font-weight: 650; }
.button:hover { border-color: var(--accent); color: var(--accent); }
.button.primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.button.danger { border-color: #d8aaa5; color: var(--danger); }
.button.small { min-height: 29px; padding: 0 9px; font-size: 11px; }
.panel-toggle { border: 0; background: transparent; color: var(--accent); font-size: 11px; }

.conversation-root { position: relative; min-width: 0; background: var(--paper); }
.empty-state { display: grid; justify-items: center; align-content: center; gap: 11px; min-height: 100%; padding: 36px; text-align: center; }
.empty-state h1 { margin: 0; font-size: 25px; letter-spacing: -.04em; }
.empty-state p { max-width: 38ch; margin: 0 0 8px; color: var(--muted); }
.empty-mark { display: grid; place-items: center; width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 50%; color: var(--accent); font-size: 17px; font-weight: 750; }
.conversation { display: grid; min-width: 0; grid-template-columns: minmax(0,1fr); grid-template-rows: auto minmax(0, 1fr) auto; height: 100%; }
.conversation-head { display: flex; align-items: center; gap: 12px; min-height: 70px; padding: 14px clamp(18px,4vw,44px); border-bottom: 1px solid var(--line); }
.conversation-title { min-width: 0; }
.conversation-title h1 { overflow: hidden; margin: 0; font-size: 18px; letter-spacing: -.025em; text-overflow: ellipsis; white-space: nowrap; }
.conversation-title p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.privacy { display: inline-flex; align-items: center; gap: 5px; margin-left: auto; padding: 4px 7px; border: 1px solid var(--line); color: var(--muted); font-size: 11px; white-space: nowrap; }
.privacy.shared { border-color: #b6c9d9; background: var(--accent-soft); color: var(--accent); }
.messages { overflow: auto; padding: 28px max(18px, calc((100% - 760px) / 2)); scroll-behavior: smooth; }
.message { display: grid; grid-template-columns: 30px minmax(0,1fr); gap: 10px; margin: 0 0 22px; }
.message.user { grid-template-columns: minmax(0,1fr); padding-left: 52px; }
.message.user .message-body { justify-self: end; padding: 10px 13px; border-radius: 14px 14px 3px 14px; background: var(--soft); }
.message-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.message-head strong { font-size: 12px; }
.message-head time, .message-meta { color: var(--muted); font-size: 10px; }
.message-body { max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere; }
.message.error .message-body { color: var(--danger); }
.message-meta { margin-top: 5px; font-size: 11px; }
.composer-wrap { padding: 13px max(18px, calc((100% - 760px) / 2)) 18px; border-top: 1px solid var(--line); background: linear-gradient(to bottom, rgba(255,255,255,.88), #fff); }
.composer { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 9px; align-items: end; }
.composer textarea { width: 100%; min-height: 42px; max-height: 150px; resize: vertical; padding: 10px 12px; border: 1px solid #c9ccc9; border-radius: 10px; background: #fff; color: var(--ink); }
.composer-controls { display: flex; align-items: end; gap: 7px; }
.provider-chooser { min-height: 34px; max-width: 112px; padding: 0 7px; border: 1px solid #c9ccc9; border-radius: 7px; background: var(--paper); color: var(--ink); font-size: 11px; }
.composer-note { display: flex; justify-content: space-between; gap: 12px; margin-top: 7px; color: var(--muted); font-size: 10px; }
.context-panel { position: absolute; z-index: 2; top: 70px; right: 0; bottom: 0; width: min(340px,100%); overflow: auto; padding: 20px; border-left: 1px solid var(--line); background: #fafaf8; box-shadow: var(--shadow); }
.context-panel h2 { margin: 0 0 4px; font-size: 15px; }
.context-panel p { margin: 0 0 14px; color: var(--muted); font-size: 12px; }
.memory { padding: 11px 0; border-top: 1px solid var(--line); font-size: 12px; }
.memory small { display: block; margin-top: 4px; color: var(--muted); }

#notices { position: fixed; z-index: 20; top: 12px; left: 50%; display: grid; gap: 7px; width: min(470px,calc(100% - 30px)); transform: translateX(-50%); pointer-events: none; }
.notice { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); background: #fff; box-shadow: var(--shadow); font-size: 12px; pointer-events: auto; }
.notice.error, .field-error { color: var(--danger); }
.auth-card { width: min(420px,calc(100% - 32px)); margin: 11vh auto; padding: 28px; border: 1px solid var(--line); background: var(--paper); box-shadow: var(--shadow); }
.auth-card h1 { margin: 0 0 8px; font-size: 25px; letter-spacing: -.04em; }
.auth-card p { margin: 0 0 18px; color: var(--muted); }
.stack { display: grid; gap: 12px; }
.stack label { display: grid; gap: 5px; color: var(--muted); font-size: 12px; font-weight: 650; }
.stack input, .stack textarea, .stack select { width: 100%; padding: 9px; border: 1px solid #c9ccc9; background: #fff; }
.field-error { min-height: 17px; margin: 0; font-size: 12px; }
dialog { width: min(480px,calc(100% - 32px)); max-height: calc(100dvh - 32px); overflow: auto; padding: 0; border: 1px solid var(--line); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(30,30,26,.25); }
.dialog-body { padding: 22px; }
.dialog-body h2 { margin: 0 0 7px; font-size: 19px; letter-spacing: -.025em; }
.dialog-body > p { margin: 0 0 17px; color: var(--muted); font-size: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.settings-block { padding: 14px 0; border-top: 1px solid var(--line); }
.settings-block h3 { margin: 0 0 5px; font-size: 13px; }
.settings-block p { margin: 0 0 9px; color: var(--muted); font-size: 12px; }
.connection-list { display: grid; gap: 10px; }
.connection-card { padding: 14px; border: 1px solid var(--line); background: #fafaf8; }
.connection-card.selected { border-color: #b6c9d9; background: var(--accent-soft); }
.connection-card h3 { margin: 0 0 4px; font-size: 14px; }
.connection-card > p { margin: 0; overflow-wrap: anywhere; color: var(--muted); font-size: 12px; }
.connection-login { margin-top: 8px !important; }
.connection-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 11px; }
.device-code { width: fit-content; max-width: 100%; overflow-wrap: anywhere; padding: 5px 8px; border: 1px dashed #aeb5b0; background: var(--paper); color: var(--ink) !important; font: 700 15px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace !important; letter-spacing: .08em; }

.routine-heading { margin: 24px 0 9px; font-size: 13px; }
.routine-heading:first-child { margin-top: 0; }
.routine-list { display: grid; gap: 9px; }
.routine-empty { margin: 0; padding: 13px; border: 1px dashed #c9ccc9; color: var(--muted); font-size: 12px; }
.routine-card { padding: 13px; border: 1px solid var(--line); background: #fafaf8; }
.routine-card.active { border-color: #b6c9d9; background: var(--accent-soft); }
.routine-title { display: flex; align-items: center; gap: 8px; }
.routine-title h3 { min-width: 0; margin: 0; overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.routine-state { margin-left: auto; padding: 2px 6px; border: 1px solid var(--line); color: var(--muted); font-size: 10px; font-weight: 700; text-transform: capitalize; }
.routine-state.active { border-color: #a9cdb9; color: #29704d; }
.routine-state.proposed { border-color: #b6c9d9; color: var(--accent); }
.routine-meta, .routine-last-run { margin: 5px 0 0; color: var(--muted); font-size: 11px; }
.routine-review { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 8px; }
.routine-review summary { color: var(--accent); cursor: pointer; font-size: 11px; }
.routine-review h4 { margin: 11px 0 3px; font-size: 11px; }
.routine-copy { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; color: var(--ink); font-size: 12px; }
.routine-actions { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 11px; }
.routine-form { margin-top: 0; }
.routine-schedule { display: grid; grid-template-columns: minmax(0,1fr) minmax(90px,.7fr) minmax(0,1fr); gap: 9px; }
.routine-schedule label { min-width: 0; }

@media (max-width:720px) {
  .topbar { padding: 0 12px; }
  .mobile-only { display: block; }
  .connection-status { display: none; }
  .chat-layout { display: block; }
  .sidebar { position: fixed; z-index: 10; inset: 58px auto 0 0; width: min(292px,86vw); transform: translateX(-102%); transition: transform .18s ease; box-shadow: var(--shadow); }
  .sidebar.open { transform: translateX(0); }
  .conversation-root { height: calc(100vh - 58px); }
  .conversation-head, .messages { padding-left: 16px; padding-right: 16px; }
  .composer-wrap { padding: 11px 16px 16px; }
  .composer { grid-template-columns: minmax(0, 1fr); }
  .composer-controls { justify-content: space-between; }
  .dialog-body { padding: 18px; }
  .dialog-actions { flex-wrap: wrap; }
  .message.user { padding-left: 28px; }
  .conversation-head { flex-wrap: wrap; gap: 8px; }
  .conversation-title { flex-basis: 100%; }
  .privacy { margin-left: 0; max-width: calc(100% - 62px); overflow: hidden; text-overflow: ellipsis; font-size: 10px; }
  .panel-toggle { margin-left: auto; }
  .sidebar:not(.open) { visibility: hidden; }
  .context-panel { top: 110px; }
  .topbar { gap: 6px; }
  .wordmark { margin-right: auto; }
  #routines-button { padding-left: 6px; padding-right: 6px; }
  .routine-schedule { grid-template-columns: 1fr; gap: 10px; }
}
@media (max-width:420px) {
  .topbar { gap: 2px; padding: 0 8px; }
  .wordmark { font-size: 13px; }
  .quiet-button, .icon-button { padding-left: 5px; padding-right: 5px; font-size: 12px; }
  #logout-button { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .messages { scroll-behavior: auto; }
  .sidebar { transition: none; }
}

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0; }
