:root {
  --bg: #18181b;
  --panel: #232327;
  --panel-2: #2c2c31;
  --line: #3a3a40;
  --text: #ececee;
  --muted: #a1a1aa;
  --accent: #d97757;
  --accent-ink: #1b0f0a;
  --danger: #f87171;
  --ok: #4ade80;
  --user: #3b3b44;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
#app { height: 100dvh; display: flex; flex-direction: column; }
.hidden { display: none !important; }

/* lock / enrol */
.lock { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; padding: 24px; text-align: center; }
.lock img { width: 96px; height: 96px; }
.lock h1 { margin: 0; font-size: 24px; }
.lock p { margin: 0; color: var(--muted); max-width: 320px; }
.code-input { width: 200px; text-align: center; letter-spacing: 8px; font-size: 28px; padding: 10px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); }
.btn { background: var(--accent); color: var(--accent-ink); font-weight: 600; padding: 14px 22px; border-radius: 999px; min-width: 200px; }
.btn:disabled { opacity: .5; }
.btn.ghost { background: var(--panel-2); color: var(--text); font-weight: 500; }
.err { color: var(--danger); min-height: 1.4em; }

/* top bar */
.top { padding: calc(var(--safe-top) + 8px) 12px 8px; display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); background: var(--bg); }
.top h2 { margin: 0; font-size: 18px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 20px; flex: none; }
.icon-btn:active { background: var(--panel-2); }
.tabs { display: flex; gap: 4px; padding: 8px 12px; }
.tab { flex: 1; padding: 9px; border-radius: 999px; background: var(--panel); color: var(--muted); font-weight: 600; position: relative; }
.tab.on { background: var(--panel-2); color: var(--text); }
.tab .dot { position: absolute; top: 8px; right: 18px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }

/* lists */
.scroll { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 4px 12px calc(var(--safe-bottom) + 96px); }
.empty { color: var(--muted); text-align: center; padding: 48px 16px; }
.card { background: var(--panel); border-radius: var(--radius); padding: 12px 14px; margin: 8px 0; display: block; width: 100%; text-align: left; }
.card .row { display: flex; gap: 8px; align-items: baseline; }
.card .t { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .when { color: var(--muted); font-size: 12px; flex: none; }
.card .snip { color: var(--muted); font-size: 14px; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .where { color: var(--muted); font-size: 12px; margin-top: 4px; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.alert-text { white-space: pre-wrap; word-break: break-word; font-size: 14px; margin-top: 6px; color: var(--text); }
.alert-text.clamp { display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.card .actions { display: flex; gap: 8px; margin-top: 10px; }
.small-btn { padding: 8px 14px; border-radius: 999px; background: var(--panel-2); font-size: 14px; font-weight: 600; }
.small-btn.primary { background: var(--accent); color: var(--accent-ink); }
.fab { position: fixed; right: 18px; bottom: calc(var(--safe-bottom) + 20px); background: var(--accent); color: var(--accent-ink); font-weight: 700; padding: 15px 22px; border-radius: 999px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
.banner { background: var(--panel-2); border-radius: var(--radius); padding: 12px 14px; margin: 8px 0; display: flex; gap: 10px; align-items: center; font-size: 14px; }
.banner span { flex: 1; }

/* chat */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.msgs { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; padding: 10px 14px; border-radius: 18px; word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); white-space: pre-wrap; border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--panel); border-bottom-left-radius: 6px; }
.msg.assistant.live { white-space: pre-wrap; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant p:last-child, .msg.assistant ul:last-child, .msg.assistant ol:last-child { margin-bottom: 0; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 8px; padding-left: 20px; }
.msg.assistant pre { background: #111113; padding: 10px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.msg.assistant code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13.5px; background: #111113; padding: 1px 5px; border-radius: 5px; }
.msg.assistant pre code { padding: 0; background: none; }
.msg.assistant a { color: var(--accent); }
.msg.assistant table { border-collapse: collapse; font-size: 13px; display: block; overflow-x: auto; }
.msg.assistant td, .msg.assistant th { border: 1px solid var(--line); padding: 4px 6px; }
.msg.error { align-self: stretch; background: rgba(248,113,113,.12); border: 1px solid rgba(248,113,113,.4); color: #fecaca; white-space: pre-wrap; font-size: 14px; }
.msg.info { align-self: center; color: var(--muted); font-size: 13px; font-style: italic; padding: 2px 8px; }
.meta { align-self: flex-start; color: var(--muted); font-size: 12px; padding: 0 6px; }
.alert-ctx { align-self: stretch; background: var(--panel-2); border-left: 3px solid var(--accent); border-radius: 10px; padding: 10px 12px; font-size: 14px; }
.alert-ctx b { display: block; margin-bottom: 4px; }

.steps { align-self: flex-start; max-width: 92%; background: transparent; border: 1px solid var(--line); border-radius: 14px; font-size: 13px; color: var(--muted); }
.steps > button { width: 100%; text-align: left; padding: 8px 12px; display: flex; gap: 8px; align-items: center; }
.steps > button .lbl { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.steps .list { border-top: 1px solid var(--line); padding: 6px 0; }
.step { padding: 4px 12px; }
.step .head { display: flex; gap: 6px; width: 100%; text-align: left; color: var(--text); font-size: 13px; }
.step .head .nm { font-weight: 600; flex: none; }
.step .head .sm { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step .head.err .nm { color: var(--danger); }
.step pre { margin: 4px 0 6px; background: #111113; color: #d4d4d8; padding: 8px; border-radius: 8px; font-size: 12px; max-height: 300px; overflow: auto; white-space: pre-wrap; word-break: break-word; }
.spin { width: 12px; height: 12px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.working { align-self: flex-start; display: flex; gap: 8px; align-items: center; color: var(--muted); font-size: 13px; padding: 0 6px; }

.composer { display: flex; gap: 8px; align-items: flex-end; padding: 8px 10px calc(var(--safe-bottom) + 8px); border-top: 1px solid var(--line); background: var(--bg); }
.composer textarea { flex: 1; resize: none; max-height: 40vh; min-height: 44px; padding: 11px 14px; border-radius: 22px; border: 1px solid var(--line); background: var(--panel); outline: none; }
.composer textarea:focus { border-color: var(--accent); }
.send { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 20px; font-weight: 700; flex: none; }
.send.stop { background: var(--danger); color: #1b0a0a; }
.send:disabled { opacity: .4; }

/* sheets */
.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 10; display: flex; align-items: flex-end; }
.sheet { background: var(--panel); width: 100%; max-height: 85dvh; border-radius: 20px 20px 0 0; padding: 14px 14px calc(var(--safe-bottom) + 14px); display: flex; flex-direction: column; gap: 10px; }
.sheet h3 { margin: 4px 4px 0; font-size: 17px; }
.sheet input[type=search] { width: 100%; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); outline: none; }
.sheet .opts { overflow-y: auto; display: flex; flex-direction: column; gap: 2px; min-height: 120px; }
.opt { text-align: left; padding: 11px 12px; border-radius: 10px; }
.opt:active, .opt.on { background: var(--panel-2); }
.opt small { color: var(--muted); display: block; font-size: 12px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { padding: 7px 12px; border-radius: 999px; background: var(--bg); font-size: 14px; }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.menu-item { text-align: left; padding: 14px 12px; border-radius: 10px; font-size: 16px; }
.menu-item:active { background: var(--panel-2); }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-bottom) + 90px); background: var(--panel-2); padding: 10px 16px; border-radius: 999px; font-size: 14px; z-index: 20; box-shadow: 0 4px 14px rgba(0,0,0,.4); max-width: 90vw; }
.conn { position: fixed; top: calc(var(--safe-top) + 6px); left: 50%; transform: translateX(-50%); background: var(--panel-2); color: var(--muted); font-size: 12px; padding: 3px 10px; border-radius: 999px; z-index: 5; }
