:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", Inter, system-ui, sans-serif;
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-raised: #f7f9fc;
  --surface-soft: #e8eef8;
  --line: rgba(31, 49, 75, .11);
  --line-strong: rgba(31, 49, 75, .2);
  --control-line: #75869b;
  --text: #182235;
  --muted: #5b6879;
  --muted-strong: #344256;
  --accent: #5574d8;
  --accent-bright: #405fbe;
  --accent-ink: #ffffff;
  --focus: #234cc4;
  --success: #15805a;
  --danger: #c94b55;
  --terminal: #183b2d;
  --shadow: 0 20px 60px rgba(38, 55, 83, .16);
}

* { box-sizing: border-box; }
html { height: 100%; background: var(--bg); }
body { margin: 0; color: var(--text); }
button, textarea, input, select { font: inherit; }
button, select { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:disabled, textarea:disabled, select:disabled { cursor: not-allowed; opacity: .6; }
button:focus-visible, textarea:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.9; }
.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; }
[hidden] { display: none !important; }

/* Two-PIN entry */
.gate-body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(22px, env(safe-area-inset-top)) 18px max(22px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 50% 0%, rgba(85, 116, 216, .18), transparent 44%),
    var(--bg);
}
.gate-main { width: min(100%, 390px); margin: 0; }
.gate-mark {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  margin: 0 auto 18px;
  border: 1px solid rgba(85, 116, 216, .28);
  border-radius: 18px;
  background: linear-gradient(145deg, #f4f7ff, #dfe7fb);
  box-shadow: var(--shadow);
  color: var(--accent);
  font-size: 24px;
  font-weight: 800;
}
.gate-card {
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 26px;
  background: rgba(255, 255, 255, .94);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
}
.gate-card h1 { margin: 0; font-size: 28px; letter-spacing: -.035em; }
.gate-intro { margin: 10px 0 24px; color: var(--muted); line-height: 1.5; }
.gate-progress { display: flex; gap: 7px; margin: 0 0 24px; }
.gate-progress span { height: 4px; flex: 1; border-radius: 999px; background: #dbe2ed; }
.gate-progress span.active { background: var(--accent); }
.gate-card label { display: block; margin-bottom: 9px; color: var(--muted-strong); font-size: 14px; font-weight: 700; }
.gate-card #pin-input {
  width: 100%;
  height: 62px;
  border: 1px solid var(--control-line);
  border-radius: 16px;
  outline: none;
  background: #f8fafd;
  color: var(--text);
  font: 750 25px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: .36em;
  text-align: center;
}
.gate-card #pin-input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(85, 116, 216, .13); }
.gate-card #pin-input:-webkit-autofill {
  -webkit-text-fill-color: var(--text);
  box-shadow: 0 0 0 1000px #f8fafd inset;
}
.gate-card #pin-submit {
  width: 100%;
  min-height: 54px;
  margin-top: 14px;
  border: 0;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 800;
}
.gate-alert { margin: 0 0 18px; padding: 13px 14px; border: 1px solid rgba(201, 75, 85, .32); border-radius: 14px; background: #fff0f1; color: #8c3038; font-size: 14px; line-height: 1.4; }
.gate-note { margin: 18px 0 0; color: #78869a; font-size: 12px; line-height: 1.45; text-align: center; }

/* Mobile controller */
.controller-body {
  height: 100dvh;
  min-height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% -12%, rgba(85, 116, 216, .18), transparent 36%),
    linear-gradient(180deg, #f9fbff, var(--bg));
}
.app-bar {
  z-index: 10;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, .8);
  backdrop-filter: blur(22px) saturate(130%);
  -webkit-backdrop-filter: blur(22px) saturate(130%);
}
.app-bar-inner {
  width: min(100%, 980px);
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 0 auto;
  padding: 10px 16px;
}
.brand-lockup { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  width: 42px;
  height: 42px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid rgba(85, 116, 216, .28);
  border-radius: 14px;
  background: linear-gradient(145deg, #f3f6ff, #dce6fb);
  color: var(--accent);
  font-size: 19px;
  font-weight: 850;
  box-shadow: 0 10px 30px rgba(45, 64, 96, .14);
}
.brand-lockup h1 { margin: 0; font-size: 22px; line-height: 1.05; letter-spacing: -.035em; }
.connection-line { display: flex; align-items: center; gap: 6px; margin-top: 5px; color: var(--muted); font-size: 12px; }
.connection-line span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.connection-dot, .console-live-dot { width: 7px; height: 7px; flex: none; border-radius: 50%; background: #e7ad4b; box-shadow: 0 0 0 3px rgba(231, 173, 75, .12); }
.connection-dot.connected, .console-live-dot.ready { background: var(--success); box-shadow: 0 0 0 3px rgba(21, 128, 90, .12); }
.icon-button {
  width: 48px;
  height: 48px;
  flex: none;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--surface);
  color: var(--muted-strong);
}
.icon-button:active { transform: scale(.96); background: var(--surface-soft); }
.icon-button.small { width: 44px; height: 44px; border-radius: 13px; }

.app-main {
  width: min(100%, 980px);
  min-width: 0;
  max-width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0 auto;
  padding: 12px 12px 10px;
}
.terminal-picker {
  flex: none;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, .9);
}
.picker-heading { min-width: 0; display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0 2px 8px; }
.picker-heading label { color: var(--muted-strong); font-size: 12px; font-weight: 750; }
.terminal-count { min-width: 0; overflow: hidden; color: var(--muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.select-shell { position: relative; display: flex; align-items: center; }
.terminal-glyph { position: absolute; left: 14px; z-index: 1; color: var(--success); font: 750 14px/1 ui-monospace, SFMono-Regular, Menlo, monospace; pointer-events: none; }
.select-shell select {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  height: 52px;
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--control-line);
  border-radius: 14px;
  padding: 0 44px 0 48px;
  outline: none;
  background: #f8fafd;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  text-overflow: ellipsis;
}
.select-shell select:focus { border-color: rgba(85, 116, 216, .72); }
.select-chevron { position: absolute; right: 14px; width: 18px; height: 18px; color: var(--muted); pointer-events: none; }

.console-card {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: rgba(249, 251, 253, .98);
  box-shadow: 0 18px 50px rgba(45, 64, 96, .12);
}
.console-header { min-height: 60px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 10px 9px 15px; border-bottom: 1px solid var(--line); background: var(--surface); }
.console-title-wrap { min-width: 0; display: flex; align-items: center; gap: 10px; }
.console-title-wrap > div { min-width: 0; }
.console-title-wrap h2 { margin: 0; overflow: hidden; color: var(--text); font-size: 14px; font-weight: 760; line-height: 1.25; text-overflow: ellipsis; white-space: nowrap; }
.console-title-wrap p { margin: 3px 0 0; overflow: hidden; color: var(--muted); font: 12px/1.25 ui-monospace, SFMono-Regular, Menlo, monospace; text-overflow: ellipsis; white-space: nowrap; }
.terminal-screen {
  min-height: 0;
  flex: 1;
  overflow: auto;
  margin: 0;
  padding: 15px;
  background:
    linear-gradient(rgba(31, 49, 75, .035) 1px, transparent 1px) 0 0 / 100% 24px,
    #f7fafc;
  color: var(--terminal);
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  overscroll-behavior: contain;
  scrollbar-color: #bcc7d6 transparent;
}

.composer-shell {
  z-index: 12;
  border-top: 1px solid var(--line);
  padding: 10px 12px max(10px, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, .9);
  backdrop-filter: blur(24px) saturate(130%);
  -webkit-backdrop-filter: blur(24px) saturate(130%);
}
.command-form, .voice-transcript { width: min(100%, 980px); min-width: 0; margin-inline: auto; }
.composer-row { display: grid; grid-template-columns: minmax(0, 1fr) 50px auto; gap: 8px; align-items: end; }
#command-input {
  width: 100%;
  min-width: 0;
  min-height: 52px;
  border: 1px solid var(--control-line);
  border-radius: 17px;
  padding: 15px 14px;
  outline: none;
  background: #ffffff;
  color: var(--text);
  line-height: 1.35;
}
#command-input::placeholder { color: #7d8a9d; }
#command-input:focus { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(85, 116, 216, .11); }
.voice-button, .send-button {
  min-height: 52px;
  border: 0;
  border-radius: 17px;
  font-weight: 800;
}
.voice-button { width: 50px; display: grid; place-items: center; background: var(--surface-soft); color: var(--accent-bright); }
.voice-button.listening { background: var(--danger); color: #ffffff; animation: listen-pulse 1.35s ease-in-out infinite; }
.send-button { min-width: 82px; display: flex; align-items: center; justify-content: center; gap: 7px; padding: 0 14px; background: var(--accent); color: var(--accent-ink); }
.send-button svg { width: 18px; height: 18px; }
.voice-button:active, .send-button:active { transform: scale(.97); }
.composer-options { min-height: 44px; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 5px 3px 0; color: var(--muted); font-size: 11px; }
.composer-options #send-mode-label { padding: 5px 8px; border-radius: 999px; white-space: nowrap; }
.composer-options #send-mode-label.quick { background: rgba(21, 128, 90, .1); color: #176d4f; }
.review-toggle { min-height: 44px; display: inline-flex; align-items: center; gap: 8px; color: var(--muted-strong); cursor: pointer; user-select: none; }
.review-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-track { width: 34px; height: 20px; flex: none; display: flex; align-items: center; padding: 2px; border: 1px solid var(--line-strong); border-radius: 999px; background: #d7dee9; transition: background .16s, border-color .16s; }
.toggle-track span { width: 14px; height: 14px; border-radius: 50%; background: #7e8ca0; transition: transform .16s, background .16s; }
.review-toggle input:checked + .toggle-track { border-color: rgba(85, 116, 216, .55); background: rgba(85, 116, 216, .28); }
.review-toggle input:checked + .toggle-track span { transform: translateX(14px); background: #fff; }
.review-toggle input:focus-visible + .toggle-track { outline: 3px solid var(--focus); outline-offset: 3px; }
.voice-transcript { display: flex; align-items: center; gap: 9px; margin-bottom: 8px; padding: 9px 12px; border: 1px solid rgba(85, 116, 216, .2); border-radius: 13px; background: #eef2ff; color: #304c9d; font-size: 13px; }
.voice-wave { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px rgba(85, 116, 216, .1); }

.review-dialog {
  width: min(100% - 16px, 520px);
  max-height: min(78dvh, 680px);
  overflow: auto;
  margin: auto auto max(8px, env(safe-area-inset-bottom));
  padding: 10px 18px 18px;
  border: 1px solid var(--line-strong);
  border-radius: 26px;
  background: #ffffff;
  color: var(--text);
  box-shadow: 0 30px 90px rgba(33, 49, 75, .3);
}
.review-dialog::backdrop { background: rgba(41, 53, 75, .3); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); }
.review-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.eyebrow { margin: 0 0 5px; color: var(--accent-bright); font-size: 10px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.review-heading h2 { margin: 0; font-size: 23px; letter-spacing: -.03em; }
.review-target { display: flex; flex-wrap: wrap; gap: 6px 10px; margin: 20px 0 10px; color: var(--muted); font-size: 12px; }
.review-command { display: block; overflow-wrap: anywhere; padding: 15px; border: 1px solid var(--line); border-radius: 15px; background: #f3f6fa; color: var(--terminal); font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
.review-note { margin: 12px 2px 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.review-actions { margin-top: 18px; }
.send-confirm { width: 100%; min-height: 52px; border: 0; border-radius: 16px; background: var(--accent); color: var(--accent-ink); font-weight: 850; }

.status-message {
  position: fixed;
  z-index: 30;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  width: max-content;
  max-width: min(92vw, 480px);
  transform: translate(-50%, -10px);
  padding: 11px 14px;
  border: 1px solid var(--line-strong);
  border-radius: 13px;
  background: rgba(255, 255, 255, .97);
  color: var(--text);
  box-shadow: 0 14px 40px rgba(38, 55, 83, .2);
  font-size: 13px;
  line-height: 1.35;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
}
.status-message.visible { opacity: 1; transform: translate(-50%, 0); }
.status-message.error { border-color: rgba(201, 75, 85, .38); background: #fff0f1; color: #8c3038; }

@keyframes listen-pulse { 50% { box-shadow: 0 0 0 7px rgba(201, 75, 85, .14); } }

@media (min-width: 760px) {
  .app-bar-inner { min-height: 76px; padding-inline: 22px; }
  .app-main { padding: 18px 22px 14px; gap: 14px; }
  .terminal-picker { display: grid; grid-template-columns: 170px minmax(0, 1fr); align-items: end; gap: 16px; padding: 15px; }
  .picker-heading { display: block; margin: 0 0 4px 2px; }
  .picker-heading label { display: block; margin-bottom: 5px; }
  .console-card { border-radius: 24px; }
  .terminal-screen { padding: 20px; font-size: 13px; }
  .composer-shell { padding: 13px 22px max(13px, env(safe-area-inset-bottom)); }
  .review-dialog { margin: auto; }
}

@media (max-width: 390px) {
  .app-bar-inner { min-height: 62px; padding: 8px 12px; }
  .brand-mark { width: 38px; height: 38px; border-radius: 12px; }
  .icon-button { width: 44px; height: 44px; }
  .icon-button.small { width: 44px; height: 44px; }
  .app-main { padding-inline: 8px; }
  .terminal-picker { padding: 10px; border-radius: 16px; }
  .console-card { border-radius: 17px; }
  .composer-shell { padding-inline: 8px; }
  .composer-row { grid-template-columns: minmax(0, 1fr) 48px 52px; gap: 6px; }
  .voice-button { width: 48px; }
  .send-button { min-width: 52px; padding-inline: 0; }
  .send-button span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
  .composer-options { padding-inline: 2px; }
}

@media (max-height: 650px) and (orientation: landscape) {
  .app-bar-inner { min-height: 54px; padding-block: 6px; }
  .brand-mark { width: 36px; height: 36px; }
  .terminal-picker { padding-block: 8px; }
  .picker-heading { margin-bottom: 5px; }
  .select-shell select { height: 44px; }
  .console-header { min-height: 50px; }
  .composer-shell { padding-block: 7px; }
  .composer-row input, .voice-button, .send-button { min-height: 46px; }
}

@media (max-height: 520px) {
  .controller-body { height: auto; min-height: 100dvh; overflow: auto; grid-template-rows: auto minmax(300px, 1fr) auto; }
  .composer-shell { position: sticky; bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
