:root {
  /* One look with the HQ dashboard: Coach navy, cyan accent (same tokens as yt-channel's dashboard page). */
  --bg: #0b2a4f;
  --bg2: #0e3766;
  --panel: #123f73;
  --panel2: #184b86;
  --ink: #f2f7ff;
  --muted: #a9c3e6;
  --line: rgba(255, 255, 255, .14);
  --accent: #7fd4ff;
  --accent-ink: #06213f;
  --bubble-in: #123f73;
  --bubble-out: #7fd4ff;
  --bubble-out-ink: #06213f;
  --ok: #6fe3a1;
  --warn: #ffc857;
  --danger: #ff8a8a;
  --shadow: none;
  --radius: 18px;
  --tabbar-h: 58px;
  /* bottom inset; app.js sets 0 when the web view already ends above the home indicator (iOS standalone, see fitShell) */
  --sab: env(safe-area-inset-bottom);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  background-image: radial-gradient(120% 80% at 50% 0, #1c5aa0 0, var(--bg2) 45%, var(--bg) 100%);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
/* The shell is 100dvh; .short-view (app.js) drops the bottom inset when the web view stops above the home indicator; while the phone keyboard is up app.js sets --app-h/--app-top from the visual viewport. */
#app { position: fixed; left: 0; right: 0; top: var(--app-top, 0px); height: 100dvh; height: var(--app-h, 100dvh); display: flex; flex-direction: column; overflow: hidden; }
.screen { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.muted { color: var(--muted); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .5; }
.link { background: none; border: 0; color: var(--accent); padding: 10px; cursor: pointer; font-size: 15px; }
.link.center { display: block; margin: 8px auto; }
.icon-btn {
  width: 40px; height: 40px; flex: none; border-radius: 50%; border: 0; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--muted);
}
.icon-btn:active { background: var(--line); }
.icon-btn.small { width: 30px; height: 30px; }
.icon-btn svg, .send-btn svg { width: 24px; height: 24px; }
.icon-btn.small svg { width: 18px; height: 18px; }

/* ---------- login ---------- */
#login { align-items: center; justify-content: center; padding: 24px 16px calc(24px + var(--sab)); }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login-avatar { margin: 0 auto 4px; border-radius: 22px; box-shadow: 0 6px 24px rgba(0,0,0,.35); }
.login-card h1 { margin: 0; font-size: 28px; letter-spacing: -.01em; }
.login-card p { margin: 0 0 8px; }
.code-form { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; text-align: left; }
.label { font-size: 13px; color: var(--muted); font-weight: 600; }
#code-input {
  min-height: 50px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel);
  color: var(--ink); font-size: 22px; letter-spacing: .25em; text-align: center; font-variant-numeric: tabular-nums;
}
.msg { min-height: 1.4em; font-size: 14px; color: var(--muted); margin: 0; }
.msg.error { color: var(--danger); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px 16px;
  background: rgba(11, 42, 79, .92);
  border-bottom: 1px solid var(--line);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.avatar { border-radius: 50%; flex: none; }
.who { flex: 1; min-width: 0; }
.name { font-weight: 700; font-size: 17px; }
.status { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.online .dot { background: var(--ok); }
.status.offline .dot { background: var(--warn); }

.banner {
  display: flex; align-items: center; gap: 8px; padding: 10px 8px 10px 16px; font-size: 14px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.banner span { flex: 1; }

/* ---------- messages ---------- */
#messages { flex: 1; overflow-y: auto; padding: 12px 12px 8px; -webkit-overflow-scrolling: touch; }
.day { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 8px; font-weight: 600; }
.row { display: flex; gap: 8px; margin: 2px 0; align-items: flex-end; }
.row.user { justify-content: flex-end; }
.row.gap { margin-top: 10px; }
.row .mini { width: 28px; height: 28px; border-radius: 50%; flex: none; }
.row .mini.spacer { visibility: hidden; }
.stack { display: flex; flex-direction: column; max-width: min(78%, 520px); min-width: 0; gap: 4px; }
.row.assistant .stack:has(.code) { width: min(78%, 520px); } /* messages with code use the full column so blocks get room */
.row.user .stack { align-items: flex-end; }
.bubble {
  padding: 9px 13px; border-radius: var(--radius); background: var(--bubble-in); box-shadow: var(--shadow);
  overflow-wrap: anywhere; white-space: normal;
}
.row.assistant .bubble { border-bottom-left-radius: 6px; }
.row.user .bubble { background: var(--bubble-out); color: var(--bubble-out-ink); border-bottom-right-radius: 6px; }
.bubble p { margin: 0 0 6px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0; padding-left: 22px; }
.bubble code { font: 13.5px ui-monospace, SFMono-Regular, Menlo, monospace; background: color-mix(in srgb, currentColor 10%, transparent); padding: 1px 5px; border-radius: 5px; }
/* fenced code blocks: header (language, iTerm, Copy) + a block that scrolls sideways instead of wrapping commands */
.bubble .code {
  --code-bg: #071d38; --code-head: #0a2747; --code-ink: #e6eefb;
  --tk-c: #7f97b8; --tk-s: #a8e6a1; --tk-k: #ff9ecf; --tk-n: #ffc98a; --tk-f: #7fd4ff; --tk-v: #c9b6ff; --tk-o: #9fe0d6;
  margin: 8px 0; border-radius: 10px; overflow: hidden; background: var(--code-bg); border: 1px solid var(--line); min-width: 0;
}
.bubble .code:first-child { margin-top: 2px; } .bubble .code:last-child { margin-bottom: 2px; }
.code-head { display: flex; align-items: center; gap: 2px; padding: 2px 4px 2px 10px; background: var(--code-head); font-size: 12px; color: var(--muted); }
.code-lang { margin-right: auto; font: 12px ui-monospace, SFMono-Regular, Menlo, monospace; }
.code-btn {
  background: none; border: 0; color: var(--muted); font: 600 12px/1 -apple-system, system-ui, sans-serif; padding: 7px 8px; min-height: 28px;
  border-radius: 6px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; -webkit-user-select: none; user-select: none;
}
.bubble a.code-btn { color: var(--muted); text-decoration: none; }
.code-btn:hover, .code-btn:active { background: var(--line); color: var(--ink); }
.code-btn.done { color: var(--ok); }
.code-iterm { display: none; }
@media (min-width: 900px) { .code-iterm { display: inline-flex; } }
.bubble pre {
  margin: 0; padding: 10px 12px; overflow-x: auto; -webkit-overflow-scrolling: touch; color: var(--code-ink, inherit);
  white-space: pre; overflow-wrap: normal; word-break: normal; tab-size: 4;
}
.bubble pre code { background: none; padding: 0; font-size: 13px; line-height: 1.5; white-space: inherit; }
.tk-c { color: var(--tk-c); font-style: italic; } .tk-s { color: var(--tk-s); } .tk-k { color: var(--tk-k); } .tk-n { color: var(--tk-n); }
.tk-f { color: var(--tk-f); } .tk-v { color: var(--tk-v); } .tk-o { color: var(--tk-o); }
.bubble a { color: inherit; text-decoration: underline; }
.meta { font-size: 11px; color: var(--muted); padding: 0 6px; display: flex; gap: 4px; align-items: center; }
.meta svg { width: 14px; height: 14px; }
.meta .ok { color: var(--accent); }
.reactions { font-size: 15px; margin-top: -10px; padding: 0 6px; z-index: 1; }
.reactions span { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 0 6px; }

.media { display: grid; gap: 4px; grid-template-columns: repeat(2, 1fr); }
.media.one { grid-template-columns: 1fr; }
.media img {
  display: block; width: 100%; max-width: 260px; max-height: 320px; object-fit: cover; border-radius: 14px;
  background: var(--line); cursor: zoom-in; aspect-ratio: auto;
}
.media:not(.one) img { height: 130px; max-width: 130px; }
.file {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 14px; text-decoration: none;
  background: var(--bubble-in); color: var(--ink); border: 1px solid var(--line); min-width: 200px; max-width: 280px;
}
.file .ficon { width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; flex: none; }
.file .fname { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file .fsize { font-size: 12px; color: var(--muted); }
.file .ftext { min-width: 0; flex: 1; }
.file svg { width: 20px; height: 20px; color: var(--muted); flex: none; }

.perm { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 12px; margin: 10px auto; max-width: 360px; }
.perm h3 { margin: 0 0 4px; font-size: 15px; }
.perm p { margin: 0 0 8px; font-size: 14px; color: var(--muted); }
.perm pre { font-size: 12px; max-height: 140px; overflow: auto; background: var(--bg); padding: 8px; border-radius: 8px; margin: 0 0 10px; white-space: pre-wrap; }
.perm .actions { display: flex; gap: 8px; } .perm .actions .btn { flex: 1; }
.perm .result { font-weight: 600; font-size: 14px; }

.typing-bubble { display: flex; gap: 4px; padding: 13px 14px; margin-left: 36px; }
.typing-bubble span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing-bubble span:nth-child(2) { animation-delay: .2s; } .typing-bubble span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.pill {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(86px + var(--tabbar-h) + var(--sab));
  background: var(--accent); color: var(--accent-ink); border: 0; border-radius: 20px; padding: 8px 16px; font-weight: 600; box-shadow: 0 4px 14px rgba(0,0,0,.18);
}

/* ---------- composer ---------- */
.composer { border-top: 1px solid var(--line); background: color-mix(in srgb, var(--bg) 92%, transparent); padding: 8px; }
#app:not(.tabs) .composer { padding-bottom: calc(8px + var(--sab)); }
.composer-row { display: flex; align-items: flex-end; gap: 2px; }
#input {
  flex: 1; resize: none; max-height: 140px; min-height: 40px; padding: 9px 14px; margin: 0 4px;
  border-radius: 20px; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  font: inherit; font-size: 16px; line-height: 1.35;
}
.send-btn { width: 40px; height: 40px; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); cursor: pointer; }
.send-btn:disabled { opacity: .35; }
.pending { display: flex; gap: 8px; overflow-x: auto; padding: 4px 4px 8px; }
.chip { position: relative; flex: none; }
.chip img, .chip .chip-file { width: 64px; height: 64px; border-radius: 12px; object-fit: cover; display: grid; place-items: center; background: var(--panel); border: 1px solid var(--line); font-size: 11px; padding: 4px; text-align: center; overflow: hidden; word-break: break-all; }
.chip button { position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: var(--ink); color: var(--bg); font-size: 14px; line-height: 22px; padding: 0; cursor: pointer; }

/* ---------- sheet, viewer, toast ---------- */
.sheet { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: flex; align-items: flex-end; z-index: 20; }
.sheet-panel { width: 100%; background: var(--panel); border-radius: 18px 18px 0 0; padding: 8px 16px calc(16px + var(--sab)); }
.sheet-item { display: block; width: 100%; text-align: left; padding: 15px 4px; border: 0; border-bottom: 1px solid var(--line); background: none; font-size: 16px; cursor: pointer; }
.sheet-item.danger { color: var(--danger); }
.sheet-item.cancel { border: 0; text-align: center; font-weight: 600; color: var(--muted); }
.viewer { position: fixed; inset: 0; z-index: 30; background: #000; touch-action: none; overscroll-behavior: contain; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.viewer-stage { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.viewer-stage img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; transform-origin: 50% 50%; will-change: transform; -webkit-user-drag: none; }
.viewer-top, .viewer-bottom { position: absolute; left: 0; right: 0; display: flex; gap: 10px; transition: opacity .2s;
  padding-left: calc(12px + env(safe-area-inset-left)); padding-right: calc(12px + env(safe-area-inset-right)); pointer-events: none; }
.viewer-top { top: 0; justify-content: flex-end; padding-top: calc(10px + env(safe-area-inset-top)); }
.viewer-bottom { bottom: 0; justify-content: center; padding-bottom: calc(16px + var(--sab)); }
.viewer-btn { pointer-events: auto; display: inline-flex; align-items: center; justify-content: center; gap: 8px; width: 44px; height: 44px; border-radius: 22px;
  border: 0; background: rgba(40, 40, 40, .72); color: #fff; font-family: inherit; font-size: 16px; font-weight: 600; line-height: 1; cursor: pointer; -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); }
.viewer-btn.wide { width: auto; padding: 0 20px 0 16px; }
.viewer-btn svg { width: 22px; height: 22px; }
.viewer-btn:disabled { opacity: .6; }
.viewer.chrome-off .viewer-top, .viewer.chrome-off .viewer-bottom { opacity: 0; }
.viewer.chrome-off .viewer-btn { pointer-events: none; }
html.viewer-open, html.viewer-open body { overflow: hidden; overscroll-behavior: none; }
html.viewer-open #messages { overflow: hidden; }
.toast { position: fixed; top: calc(64px + env(safe-area-inset-top)); left: 16px; right: 16px; margin: 0 auto; max-width: 360px; background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 12px; font-size: 14px; text-align: center; z-index: 40; }
#code-input::placeholder { letter-spacing: normal; font-size: 17px; }
.pending { padding-top: 10px; }

/* ---------- app shell: bottom tabs + HQ dashboard ---------- */
.tabbar {
  flex: none; display: flex; gap: 4px; padding: 6px 6px calc(6px + var(--sab));
  background: rgba(11, 42, 79, .97); border-top: 1px solid var(--line);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.tabbar button {
  position: relative; flex: 1 1 auto; min-width: 0; min-height: 52px; padding: 4px 6px; border-radius: 14px;
  border: 1px solid transparent; background: transparent; color: var(--muted); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.tabbar button[aria-selected="true"] { background: var(--panel2); color: var(--ink); border-color: var(--accent); }
.tabbar .ti { width: 24px; height: 24px; display: grid; place-items: center; }
.tabbar .ti svg { width: 24px; height: 24px; }
.tabbar .tl { font-size: 11.5px; letter-spacing: -.01em; font-weight: 600; line-height: 1.15; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tabbar .badge {
  position: absolute; top: 3px; left: calc(50% + 6px); min-width: 19px; height: 19px; padding: 0 5px; border-radius: 10px;
  font-size: 12px; font-weight: 700; line-height: 19px; text-align: center; background: var(--warn); color: #1b1300;
}
.tabbar .badge.unread { background: var(--accent); color: var(--accent-ink); }
#app.kb .tabbar { display: none; }
#dash { background: var(--bg); padding-top: env(safe-area-inset-top); }
#dash-frame { flex: 1; width: 100%; border: 0; display: block; background: var(--bg); }
#app.short-view { --sab: 0px; }

/* ---------- local service messages (e.g. Tick & Torque) ---------- */
.row .svc-av { display: grid; place-items: center; background: #f3b54a; color: #06213f; font: 700 10px/1 system-ui, sans-serif; letter-spacing: -.02em; }
.row.service .bubble { background: color-mix(in srgb, var(--bubble-in) 80%, #f3b54a 20%); }
.sender { font-size: 12px; font-weight: 700; color: #f3b54a; padding: 0 6px; }
/* Meta-row buttons (Reply, Retry, Edit): 40px tap target, the negative margin keeps the row visually compact. */
.reply-btn { background: none; border: 0; color: var(--accent); font: inherit; font-weight: 600; padding: 0 10px; min-height: 40px; min-width: 44px; margin: -8px 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.reply-btn:active { opacity: .6; }
.meta .route { color: #f3b54a; }
.replying { display: flex; align-items: center; gap: 8px; margin: 2px 4px 6px; padding: 6px 6px 6px 12px; border-left: 3px solid #f3b54a; background: var(--panel); border-radius: 8px; font-size: 13px; color: var(--muted); }
.replying-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#replying-who { font-weight: 700; color: var(--ink); }
#replying-text { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#replying-text:empty { display: none; }
.replying button { background: none; border: 0; color: var(--muted); font-size: 22px; line-height: 1; min-width: 40px; min-height: 40px; padding: 0; cursor: pointer; flex: none; }
/* Quoted context above a reply bubble; tap jumps to the original. */
.quote { display: flex; flex-direction: column; align-items: flex-start; max-width: 100%; min-width: 0; min-height: 40px; justify-content: center; text-align: left;
  font: inherit; font-size: 12px; color: var(--muted); background: var(--panel); border: 0; border-left: 3px solid var(--accent); border-radius: 8px; padding: 4px 10px; cursor: pointer; }
.quote-who { font-weight: 700; color: var(--accent); }
.quote-text { max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.quote-text:empty { display: none; }
.row.flash .bubble { animation: flash 1.2s ease-out; }
@keyframes flash { 0%, 30% { box-shadow: 0 0 0 3px var(--accent); } 100% { box-shadow: 0 0 0 0 transparent; } }

/* ---------- failed sends ---------- */
.row.failed .bubble { background: color-mix(in srgb, var(--danger) 22%, var(--bubble-in)); color: var(--ink); border: 1px solid var(--danger); }
.row.failed .meta .err { color: var(--danger); font-weight: 600; }
.row.failed .small-note { font-size: 12px; color: var(--muted); padding: 0 6px; text-align: right; }

/* ---------- wide screens (Mac, iPad landscape): tabs as a left sidebar, chat column ~720px centred ---------- */
@media (min-width: 900px) {
  #app.tabs { flex-direction: row; }
  #app.tabs .tabbar {
    order: -1; flex: none; width: 196px; flex-direction: column; gap: 6px;
    padding: calc(14px + env(safe-area-inset-top)) 10px calc(14px + var(--sab));
    border-top: 0; border-right: 1px solid var(--line);
  }
  #app.tabs .tabbar button { flex: none; flex-direction: row; justify-content: flex-start; gap: 10px; min-height: 46px; padding: 6px 12px; }
  #app.tabs .tabbar .tl { font-size: 14.5px; }
  #app.tabs .tabbar .badge { position: static; margin-left: auto; }
  #app.kb .tabbar { display: flex; }
  #app.tabs .screen { min-width: 0; }
  #chat .topbar, #chat .banner { padding-left: max(16px, calc((100% - 720px) / 2)); padding-right: max(8px, calc((100% - 720px) / 2)); }
  #messages { padding-left: max(12px, calc((100% - 720px) / 2)); padding-right: max(12px, calc((100% - 720px) / 2)); }
  .composer { padding-left: max(8px, calc((100% - 720px) / 2)); padding-right: max(8px, calc((100% - 720px) / 2)); }
  .pill { left: calc(50% + 98px); bottom: 86px; }
  .stack { max-width: min(78%, 560px); }
  .row.assistant .stack:has(.code) { width: min(78%, 560px); }
}
