/*
  Priority map (drives every layout rule below — do not reorder without updating this):
    1. State indicator — what the agent is doing right now. Always visible, always first.
    2. Hold / Go-ahead controls — the patience is the product; must be thumb-reachable.
    3. Mic level meter — visible proof the user is being heard during long silence.
    4. Live transcript — phone: last few turns only. Tablet/desktop: full scrollback.
    5. Session list + settings — phone: behind a sheet. Desktop: inline third column.

  Narrow (<=390px, iPhone portrait) is the PRIMARY context: one-handed, controls at the
  bottom within thumb reach. Tablet (768-1024) and desktop (>=1280) get a two/three-column
  layout with transcript beside controls, never replacing the state indicator's prominence.
*/

:root {
  --bg: #f4f6f8;
  --bg-elevated: #ffffff;
  --text: #10202c;
  --text-muted: #4b6072;
  --border: #d7dee4;
  --accent: #2b6a8f;
  --accent-strong: #1c4e6b;
  --danger: #a33b2c;
  --danger-strong: #7e2d21;
  --ok: #2f7d5a;
  --focus-ring: #1c4e6b;
  --meter-fill: #2b6a8f;
  --shadow: 0 1px 3px rgba(16, 32, 44, 0.08), 0 8px 24px rgba(16, 32, 44, 0.06);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0a1420;
    --bg-elevated: #101f2c;
    --text: #eaf3fa;
    --text-muted: #9fb4c4;
    --border: #223546;
    --accent: #5c93b8;
    --accent-strong: #7fb2d6;
    --danger: #d9705d;
    --danger-strong: #e88a78;
    --ok: #5cbf93;
    --focus-ring: #7fb2d6;
    --meter-fill: #5c93b8;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

/* A class rule's `display` always outranks the UA [hidden] rule by specificity — without this,
   toggling `.hidden = true` on `.app`/`.auth-gate` (both set their own `display`) does nothing
   visible. Keep this ahead of every component rule below. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: clamp(16px, 1.4vw + 12px, 18px);
  line-height: 1.5;
  overflow-x: hidden;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

a {
  color: var(--accent-strong);
}

/* ---------- App shell ---------- */

.app {
  display: grid;
  min-height: 100dvh;
  grid-template-areas:
    "topbar"
    "state"
    "meter"
    "transcript"
    "controls";
  grid-template-rows: auto auto auto 1fr auto;
  gap: 12px;
  padding: env(safe-area-inset-top) 16px calc(env(safe-area-inset-bottom) + 12px);
}

.topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.brand img {
  width: 28px;
  height: 28px;
  border-radius: 8px;
}

.icon-button {
  min-width: 44px;
  min-height: 44px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* ---------- State indicator (priority 1) ---------- */

.state-card {
  grid-area: state;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  text-align: center;
}

.state-label {
  font-size: clamp(1.5rem, 4vw + 0.5rem, 2.1rem);
  font-weight: 700;
  margin: 0 0 4px;
}

.state-sub {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.state-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-right: 8px;
  background: var(--text-muted);
}

.state-card[data-state="listening"] .state-dot { background: var(--accent); }
.state-card[data-state="hard_listen"] .state-dot { background: var(--danger); }
.state-card[data-state="thinking"] .state-dot { background: #b8952f; }
.state-card[data-state="speaking"] .state-dot { background: var(--ok); }

@media (prefers-reduced-motion: no-preference) {
  .state-card[data-state="listening"] .state-dot,
  .state-card[data-state="hard_listen"] .state-dot {
    animation: pulse 2.4s ease-in-out infinite;
  }
}

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

/* ---------- Mic meter (priority 3) ---------- */

.meter-card {
  grid-area: meter;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}

.meter-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.meter-track {
  flex: 1;
  height: 10px;
  border-radius: 6px;
  background: var(--border);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0%;
  background: var(--meter-fill);
  transition: width 90ms linear;
}

/* ---------- Transcript (priority 4) ---------- */

.transcript-card {
  grid-area: transcript;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px;
  overflow-y: auto;
  min-height: 0;
}

.transcript-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.turn {
  max-width: 75ch;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 0.95rem;
}

.turn[data-role="user"] {
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  align-self: flex-end;
}

.turn[data-role="agent"] {
  background: color-mix(in srgb, var(--text-muted) 12%, transparent);
  align-self: flex-start;
}

.turn.is-interim {
  opacity: 0.6;
  font-style: italic;
}

/* ---------- Controls (priority 2) ---------- */

.controls {
  grid-area: controls;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding-bottom: 4px;
}

.control-btn {
  min-height: 56px;
  border-radius: 14px;
  border: 1px solid var(--border);
  font-weight: 600;
  font-size: 1.05rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.control-btn.hold {
  background: var(--danger);
  color: #fff8f6;
  border-color: var(--danger-strong);
}

.control-btn.hold[aria-pressed="true"] {
  background: var(--danger-strong);
}

.control-btn.go {
  background: var(--accent);
  color: #f5fbff;
  border-color: var(--accent-strong);
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---------- Sheets (settings / sessions) — phone: full-screen sheet ---------- */

.sheet {
  position: fixed;
  inset: 0;
  background: rgba(10, 20, 32, 0.4);
  display: flex;
  align-items: flex-end;
  z-index: 20;
}

.sheet[hidden] {
  display: none;
}

.sheet-panel {
  background: var(--bg-elevated);
  width: 100%;
  max-height: 85dvh;
  overflow-y: auto;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(env(safe-area-inset-bottom) + 20px);
  box-shadow: var(--shadow);
}

.sheet-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 18px;
}

.sheet-row label {
  font-weight: 600;
  font-size: 0.9rem;
}

.sheet-row .hint {
  color: var(--text-muted);
  font-size: 0.82rem;
}

input[type="range"] {
  width: 100%;
  height: 44px;
}

select,
input[type="text"] {
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 10px;
}

.backchannel-note {
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--ok) 14%, transparent);
  font-size: 0.85rem;
}

.close-sheet {
  min-width: 44px;
  min-height: 44px;
}

/* ---------- Auth gate ---------- */

.auth-gate {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  min-height: 100dvh;
  padding: 24px;
  text-align: center;
}

.auth-gate a.control-btn.go {
  text-decoration: none;
  padding: 0 28px;
}

/* Clerk's mounted <SignIn/> — width scales with viewport (narrow: full-bleed card down to
   the gate's own 24px padding; wide: capped so it reads as a card, not a stretched form).
   Clerk renders its own internal scroll/touch handling; we only own the outer box. */
.clerk-signin {
  width: min(100%, 400px);
}

.auth-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  max-width: 42ch;
}

/* The hint slot is an error slot by default — "sign-in is unavailable", "not configured". When
   it carries plain information instead, it must not read as a warning. */
#authHint {
  color: var(--danger);
}

#authHint.is-note {
  color: var(--text-muted);
}

@media (min-width: 768px) {
  .auth-gate {
    gap: 20px;
  }
}

/* ---------- Tablet: two columns, transcript beside controls ---------- */

@media (min-width: 768px) {
  .app {
    grid-template-columns: 1.1fr 1.4fr;
    grid-template-areas:
      "topbar   topbar"
      "state    transcript"
      "meter    transcript"
      "controls transcript";
    grid-template-rows: auto auto auto 1fr;
    max-width: 900px;
    margin: 0 auto;
  }

  /* .controls shares the flexible last row with .transcript's tail so the transcript can
     grow — without this the row's default stretch inflates the buttons to fill that height. */
  .controls {
    align-self: end;
  }

  .transcript-card {
    max-height: calc(100dvh - 48px);
  }

  .sheet {
    align-items: center;
    justify-content: center;
  }

  .sheet-panel {
    width: min(480px, 92vw);
    max-height: 80dvh;
    border-radius: 20px;
  }
}

/* ---------- Desktop: three columns — transcript, controls, sessions/settings inline ---------- */

@media (min-width: 1280px) {
  .app {
    grid-template-columns: 340px 1fr 320px;
    grid-template-areas:
      "topbar     topbar     topbar"
      "state      transcript sidebar"
      "meter      transcript sidebar"
      "controls   transcript sidebar";
    grid-template-rows: auto auto auto 1fr;
    max-width: 1400px;
  }

  .controls {
    align-self: end;
  }

  .sidebar {
    grid-area: sidebar;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .sidebar .sheet-panel-inline {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
  }

  /* On desktop, settings/sessions render inline in .sidebar; the modal sheets are unused. */
  #settingsSheet,
  #sessionsSheet {
    display: none !important;
  }
}

@media print {
  .controls,
  .meter-card,
  .icon-button {
    display: none;
  }
}
