/*
 * Mobile styling. Dark by default, with a light-scheme override; sized for
 * one-handed use with generous tap targets and a composer pinned to the
 * bottom above the iOS home indicator.
 *
 * The palette is named after the desktop panel's theme tokens so the two
 * surfaces describe colour the same way: `--bg` is "layer 2", `--border`
 * "border l2", `--accent` the business-primary state. That keeps a change
 * made on one side readable on the other instead of drifting.
 */

:root {
  /* desktop: --dsw-alias-bg-layer-1 / -2 / -3 */
  --bg: #0f1115;
  --bg-raised: #171a21;
  --bg-input: #1e222b;
  /* desktop: --dsw-alias-border-l2 / -l1 */
  --border: #262b36;
  /* desktop: --dsw-alias-label-primary / -secondary / -tertiary */
  --text: #e8eaef;
  --text-muted: #8b93a3;
  /* desktop: --dsw-alias-state-business-primary */
  --accent: #4f7cff;
  --accent-weak: #223054;
  /* desktop: --dsw-alias-state-error-primary / -success-primary */
  --danger: #ff5f57;
  --ok: #34c759;
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f8fa;
    --bg-raised: #ffffff;
    --bg-input: #eef0f4;
    --border: #e2e5ea;
    --text: #11151c;
    --text-muted: #667085;
    --accent: #2f5bea;
    --accent-weak: #e5ebfd;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overscroll-behavior-y: contain;
  /* A long unbroken string (a path, a base64 token) must not widen the page:
     on a phone that turns every screen into a horizontally scrolling one. */
  overflow-x: hidden;
  overflow-wrap: anywhere;
}

/* The chat owns the viewport height and scrolls inside itself. A document that
   grows to ten thousand pixels scrolls differently on every mobile browser —
   and on iOS the rubber-band drag fights the composer — so the shell stays
   exactly one screen tall and only `.chat` moves. `min-height: 0` is what lets
   a flex child actually shrink and scroll; without it the pane silently
   overflowed and the page refused to drag. */
body.in-chat {
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

#app { min-height: 100dvh; max-width: 100vw; display: flex; flex-direction: column; overflow-x: hidden; }
body.in-chat #app { height: 100dvh; min-height: 0; }

h1 { font-size: 18px; font-weight: 650; margin: 0; }
h2 { font-size: 17px; margin: 0 0 8px; }
.muted { color: var(--text-muted); }
.error { color: var(--danger); font-size: 14px; margin: 8px 0 0; }
.pad { padding: 20px; }

.center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
  text-align: center;
  min-width: 0;
}

.pane {
  justify-content: flex-start;
  padding-top: calc(48px + var(--safe-top));
  align-items: stretch;
  text-align: left;
  /* One column on a phone, a readable column on a tablet. */
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}
.pane h1 { text-align: center; margin-bottom: 4px; }
.pane .muted { text-align: center; font-size: 14px; margin: 0 0 20px; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; min-width: 0; }
.field span { font-size: 13px; color: var(--text-muted); }

.input, textarea {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: 17px; /* keeps iOS from zooming on focus */
  font-family: inherit;
}
.input { text-transform: uppercase; letter-spacing: 2px; }
textarea { text-transform: none; letter-spacing: normal; resize: none; max-height: 40dvh; }

.btn {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  font-family: inherit;
}
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; }
.btn:disabled { opacity: 0.5; }

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.bar h1 { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border: none; border-radius: 10px;
  background: transparent; color: var(--text);
  font-size: 20px; font-family: inherit;
}
.icon.danger { color: var(--danger); }
.icon.ghost { opacity: 0; pointer-events: none; }

.list { flex: 1; overflow-y: auto; padding: 6px 0; }

.row {
  display: flex; align-items: center; gap: 10px;
  width: 100%;
  padding: 14px 16px;
  border: none; border-bottom: 1px solid var(--border);
  background: transparent; color: var(--text);
  text-align: left; font-family: inherit;
}
.row-main { flex: 1; display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.row-title { font-size: 16px; font-weight: 550; display: flex; align-items: center; gap: 7px; }
.row-sub { font-size: 12.5px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { color: var(--text-muted); font-size: 20px; }

.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); display: inline-block; }
.dot.running { animation: pulse 1.6s ease-in-out infinite; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.foot {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 16px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  font-size: 13px;
}
.link { border: none; background: none; color: var(--accent); font-size: 13px; font-family: inherit; padding: 8px; }

.chat {
  flex: 1 1 0;
  min-height: 0; /* lets the pane shrink so its own scrollbar can exist */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 12px 12px;
}

.msg {
  max-width: 88%;
  padding: 10px 13px;
  border-radius: var(--radius);
  font-size: 15.5px;
  word-break: break-word;
  white-space: normal;
}
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 5px; }
.msg.user.pending { opacity: 0.72; }
.pending-note { display: block; margin-top: 4px; font-size: 11.5px; opacity: 0.85; }
.msg.assistant { align-self: flex-start; background: var(--bg-raised); border: 1px solid var(--border); border-bottom-left-radius: 5px; }

/* A folded answer: the summary is the collapsed bubble, the body the full one. */
.msg.assistant.long { padding: 0; overflow: hidden; }
.msg.assistant.long summary {
  list-style: none;
  padding: 10px 13px;
  font-size: 15.5px;
  color: var(--text-muted);
  cursor: pointer;
}
.msg.assistant.long summary::-webkit-details-marker { display: none; }
.msg.assistant.long[open] summary { color: var(--text-muted); border-bottom: 1px solid var(--border); }
.msg.assistant.long .long-body { padding: 10px 13px; }

/* One folded run of tool calls and results. */
.activity-run {
  align-self: flex-start;
  max-width: 92%;
  font-size: 12.5px;
  border-left: 2px solid var(--border);
}
.activity-run summary {
  list-style: none;
  padding: 5px 10px;
  color: var(--text-muted);
  cursor: pointer;
}
.activity-run summary::-webkit-details-marker { display: none; }
.activity-run.failed summary { color: var(--danger); }
.activity-run .activity-body { padding-top: 2px; }

/* The first page's "load older" affordance. */
.older {
  align-self: center;
  margin: 4px 0 10px;
  padding: 9px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-raised);
  color: var(--text-muted);
  font-size: 13px;
  font-family: inherit;
}

.turn-error {
  align-self: stretch;
  margin: 2px 0;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--border));
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  color: var(--danger);
  font-size: 12.5px;
  word-break: break-word;
}

.tool {
  align-self: flex-start;
  max-width: 92%;
  font-size: 12.5px;
  color: var(--text-muted);
  padding: 6px 10px;
  border-left: 2px solid var(--border);
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.tool.failed { border-left-color: var(--danger); color: var(--danger); }
.tool-name { color: var(--accent); margin-right: 6px; }

.rule { display: flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: 11.5px; padding: 4px 0; }
.rule::before, .rule::after { content: ""; flex: 1; height: 1px; background: var(--border); }

.chip {
  display: inline-block; padding: 1px 8px; border-radius: 6px;
  background: var(--accent-weak); color: var(--accent); font-size: 12.5px;
}

/* Pending interactions sit between the transcript and the composer: the
   agent is blocked on them, but they must not cover the conversation the
   reader may still be scrolling. When present they take as much room as they
   need — a question card with two questions and a submit button does not fit
   in a fixed slice, and clipping its action row would make it unanswerable. */
.interactions {
  flex: 0 1 auto;
  min-height: 0;
  max-height: 50dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px 10px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

/* The transcript yields room while a card is up: both want the limited
   height of a phone, and the card is the thing the agent is waiting on. */
body.in-chat:has(.interactions:not([hidden])) .chat { flex: 1 1 0; }

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  padding: 12px;
  /* A card taller than the interaction pane scrolls its body, not its action
     row: the buttons are the whole point of the card, so they stay reachable
     however many questions the batch carries. */
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.card.approval { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; flex: 0 0 auto; }
.card-kicker {
  font-size: 11.5px;
  font-weight: 650;
  letter-spacing: 0.04em;
  color: var(--accent);
  text-transform: uppercase;
}
.card-tool {
  margin-left: auto;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  font-size: 12px;
  color: var(--text-muted);
}
.card-body { margin: 0 0 8px; font-size: 14.5px; }
.card-hint { margin: 0 0 8px; font-size: 12px; color: var(--text-muted); }

/* Device page: the permission list. The `held` / `missing` split is the whole
   point of the page, so the two states differ by more than a checkmark. */
.cap-list { list-style: none; margin: 0 0 8px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.cap {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  padding: 7px 10px;
  border-radius: 9px;
  background: var(--bg-input);
}
.cap.held { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.cap.missing { opacity: 0.62; }
.cap-mark {
  flex: none;
  width: 16px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text-muted);
  text-align: center;
}
.cap.held .cap-mark { color: var(--accent); font-weight: 700; }
.cap-main { display: flex; flex-direction: column; gap: 1px; min-width: 0; }
.cap-title { font-size: 13.5px; font-weight: 600; }
.cap-detail { font-size: 11.5px; color: var(--text-muted); line-height: 1.45; }
.card-hint.mono { font-family: ui-monospace, "SFMono-Regular", Consolas, monospace; word-break: break-all; }

.card-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
  position: sticky;
  bottom: 0;
  padding-top: 10px;
  margin-top: auto;
  background: var(--bg-raised);
}
.btn.small { width: auto; flex: 1; padding: 10px 14px; font-size: 14px; }

/* The question rows are the scrolling part of a tall card. */
.question-card .card-body-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

.question { padding: 8px 0; border-top: 1px dashed var(--border); }
.question:first-of-type { border-top: none; padding-top: 0; }
.options { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.option {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-input);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  padding: 8px 14px;
}
.option.active { border-color: transparent; background: var(--accent); color: #fff; }
.input.answer { font-size: 15px; padding: 10px 12px; }

/* Model sheet. A bottom sheet reads better than a centred dialog on a phone
   and leaves the conversation visible behind it. */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: color-mix(in srgb, #000 55%, transparent);
  display: flex;
  align-items: flex-end;
}
.sheet {
  width: 100%;
  max-height: 82dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg-raised);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
}
.sheet-head h2 { flex: 1; font-size: 16px; }
.sheet-body { overflow-y: auto; padding: 8px 12px 16px; -webkit-overflow-scrolling: touch; }
.sheet-search { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.sheet-search .input { font-size: 16px; padding: 10px 12px; text-transform: none; letter-spacing: normal; }
.model-group { padding: 4px 0; }
.model-group-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 10px 4px;
  border: none;
  background: none;
  color: var(--text);
  font-family: inherit;
  text-align: left;
}
.model-group-head .card-kicker { display: inline; }
.group-count {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}
.model-list[hidden] { display: none; }
.model-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  margin-bottom: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
}
.model-row.active { border-color: transparent; background: var(--accent-weak); }
.model-row:disabled { opacity: 0.5; }
.model-name { display: flex; align-items: center; gap: 8px; font-weight: 550; }
.model-effort {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
}
.model-sub { font-size: 12px; color: var(--text-muted); }

.composer {
  position: sticky; bottom: 0; z-index: 10;
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
}
.composer textarea { flex: 1; font-size: 16px; padding: 11px 12px; }
.composer .btn { width: auto; padding: 11px 18px; }

/* The "desktop is running" note is informational, so it sits inside the
   layout flow instead of floating over the cards the reader has to answer. */
.notice {
  flex: 0 0 auto;
  margin: 0 12px 6px;
  padding: 9px 12px; border-radius: 10px;
  background: var(--bg-raised); border: 1px solid var(--border);
  color: var(--text-muted); font-size: 12.5px; text-align: center;
}

.spinner {
  width: 26px; height: 26px; border-radius: 50%;
  border: 2.5px solid var(--border); border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
