/* ── mimi ── two glowing eyes on a dark screen ──────────────── */

/* This design commits to one look — a dark panel with cyan eyes — so the
   palette is stated once rather than inverted per theme. */
:root {
  --bg: #05070c;
  --bg-2: #0b1018;
  --eye: #5ce9ff;
  --glow-near: rgb(92 233 255 / 0.55);
  --glow-far: rgb(64 180 255 / 0.32);
  --ink: #dbe6f0;
  --muted: #5d6b7d;
  --line: #1a2331;
  --panel: #0d131c;
  --warn: #e8912a;
  --danger: #e74c3c;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system,
          "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  /* A faint pool of light behind the face keeps the black from reading as a
     dead screen. */
  background:
    radial-gradient(ellipse 70% 55% at 50% 42%, var(--bg-2) 0%, var(--bg) 72%);
  color: var(--ink);
  font-family: var(--font);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ── stage ─────────────────────────────────────────────────── */

#stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
           max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

#face {
  /* 100% of the padded stage, never 100vw — vw ignores the stage padding, so
     the SVG starts inset but still spans the full viewport width and hangs off
     the right edge. */
  width: min(100%, 160dvh);
  height: auto;
  max-height: 78dvh;
  overflow: visible;
  /* The bloom is what makes a shape change read across the room. Two stacked
     shadows: a tight core and a wide halo. */
  filter:
    drop-shadow(0 0 9px var(--glow-near))
    drop-shadow(0 0 34px var(--glow-far));
}

@media (orientation: portrait) and (max-width: 720px) {
  #stage { padding: 8px; }
  #face { width: 100%; max-height: 64dvh; }
}

@media (orientation: landscape) and (max-height: 560px) {
  #face { max-height: 72dvh; }
}

/* ── controls ──────────────────────────────────────────────── */

/* Three columns so the BUTTON sits on the true centre of the screen and the
   status sits beside it. Centring the pair as a group instead would drag the
   button off-centre, and shift it every time the status text changed length
   (idle → connecting → reconnecting). */
#controls {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(22px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  z-index: 5;
  pointer-events: none;   /* the gaps shouldn't swallow taps on the face */
}

#controls > * { pointer-events: auto; }

#connectBtn {
  grid-column: 2;
  justify-self: center;
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 11px 22px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;   /* "talk to mimi" was wrapping to two lines on phones */
  transition: border-color 180ms ease, transform 180ms ease, color 180ms ease;
}

#connectBtn[hidden] { display: none; }

#connectBtn:hover  { border-color: var(--muted); }
#connectBtn:active { transform: scale(0.97); }

#connectBtn[data-live="true"] {
  border-color: var(--eye);
  color: var(--eye);
}

#status {
  grid-column: 3;
  justify-self: start;
  margin-left: 14px;
  font-size: 13px;
  color: var(--muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
  transition: color 200ms ease;
}

#status[hidden] { display: none; }

#status::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 7px;
  vertical-align: 1px;
  background: var(--muted);
  transition: background 200ms ease, transform 200ms ease, box-shadow 200ms ease;
}

#status[data-state="idle"]::before      { background: var(--muted); opacity: 0.6; }
#status[data-state="listening"]         { color: #2ecc71; }
#status[data-state="listening"]::before { background: #2ecc71; box-shadow: 0 0 8px rgb(46 204 113 / 0.6); animation: pulse 1.2s ease-in-out infinite; }
#status[data-state="speaking"]          { color: var(--eye); }
#status[data-state="speaking"]::before  { background: var(--eye); box-shadow: 0 0 8px var(--glow-near); animation: pulse 0.8s ease-in-out infinite; }
#status[data-state="thinking"]::before  { background: #ffb02e; box-shadow: 0 0 8px rgb(255 176 46 / 0.6); animation: pulse 0.9s ease-in-out infinite; }
#status[data-state="error"]::before     { background: #e74c3c; }

@keyframes pulse {
  0%, 100% { opacity: 1;    transform: scale(1); }
  50%      { opacity: 0.35; transform: scale(0.8); }
}

.pal-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.balance-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--chip);
  border: 1px solid var(--line);
  color: var(--fg);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
  transition: border-color 200ms ease, opacity 200ms ease;
}

.balance-pill[hidden] { display: none; }

.balance-pill[data-low="true"] {
  border-color: #ffb02e;
  color: #ffb02e;
}

.account-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  background: var(--chip);
  margin-bottom: 12px;
}

.account-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.account-info {
  flex: 1;
  min-width: 0;
}

.account-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.account-email {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.account-signout {
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 4px;
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  cursor: pointer;
}
.account-signout:hover {
  color: var(--eye);
  border-color: var(--eye);
}

/* ── top-right icon buttons ────────────────────────────────── */

#paletteBtn, #memoryBtn, #attachBtn {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  appearance: none;
  border: 1.5px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  border-radius: 50%;
  cursor: pointer;
  z-index: 21;
  transition: color 180ms ease, border-color 180ms ease, transform 180ms ease;
}

#memoryBtn { right: calc(60px + env(safe-area-inset-right)); }
#attachBtn { right: calc(106px + env(safe-area-inset-right)); }

#paletteBtn svg, #memoryBtn svg, #attachBtn svg { width: 21px; height: 21px; display: block; }
#paletteBtn:hover, #memoryBtn:hover, #attachBtn:hover { color: var(--eye); border-color: var(--eye); }
#paletteBtn:active, #memoryBtn:active, #attachBtn:active { transform: scale(0.94); }
#attachBtn[data-busy="true"] { color: var(--eye); border-color: var(--eye); animation: attach-pulse 1.1s ease-in-out infinite; }

#paletteBtn[aria-expanded="true"],
#memoryBtn[aria-expanded="true"] { color: var(--eye); border-color: var(--eye); }

#palette {
  position: fixed;
  top: calc(60px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  width: min(280px, calc(100vw - 28px));
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  z-index: 22;
  box-shadow: 0 18px 44px rgb(0 0 0 / 0.34);
}

#palette[hidden] { display: none; }

.pal-section {
  margin-bottom: 12px;
}

.pal-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.pal-divider {
  height: 1px;
  background: var(--line);
  margin: 12px 0;
}

.pal-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}

.pal-row input[type="color"] {
  appearance: none;
  -webkit-appearance: none;
  width: 48px;
  height: 28px;
  padding: 0;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: none;
  cursor: pointer;
}

.pal-row input[type="color"]::-webkit-color-swatch-wrapper { padding: 3px; }
.pal-row input[type="color"]::-webkit-color-swatch { border: none; border-radius: 5px; }
.pal-row input[type="color"]::-moz-color-swatch { border: none; border-radius: 5px; }

#palPresets {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin: 12px 0 10px;
}

.pal-swatch {
  appearance: none;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
  padding: 0;
  transition: transform 140ms ease, border-color 140ms ease;
}

.pal-swatch i {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  display: block;
}

.pal-swatch:hover { transform: scale(1.12); }
.pal-swatch[aria-pressed="true"] { border-color: var(--eye); }

#palReset {
  appearance: none;
  width: 100%;
  border: 1.5px solid var(--line);
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
}

#palReset:hover { color: var(--ink); border-color: var(--muted); }

/* ── memory panel ──────────────────────────────────────────── */

#memory {
  position: fixed;
  top: calc(60px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  width: min(340px, calc(100vw - 28px));
  max-height: min(560px, calc(100dvh - 90px));
  overflow-y: auto;
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  z-index: 22;
  box-shadow: 0 18px 44px rgb(0 0 0 / 0.34);
}

#memory[hidden] { display: none; }

#memory h3 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

#memory h3 + .mem-list { margin-bottom: 16px; }

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

.mem-list li {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
  background: rgb(128 128 128 / 0.07);
}

.mem-body { flex: 1; min-width: 0; }

.mem-text {
  display: block;
  font-size: 13px;
  line-height: 1.4;
  color: var(--ink);
  overflow-wrap: anywhere;
}

.mem-meta {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
}

.mem-muted .mem-text { opacity: 0.6; }
.mem-muted .mem-meta { color: var(--warn); }

.mem-x {
  appearance: none;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 17px;
  line-height: 1;
  padding: 0 2px;
  cursor: pointer;
  opacity: 0.5;
  transition: opacity 140ms ease, color 140ms ease;
}

.mem-x:hover { opacity: 1; color: var(--danger); }

/* Needs the li to beat `.mem-list li`'s background. */
.mem-list li.mem-empty {
  background: none;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  padding: 2px 0 6px;
}

.mem-footer-actions {
  display: flex;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1.5px solid var(--line);
}

.mem-btn-subtle, .mem-btn-danger {
  flex: 1;
  text-align: center;
  appearance: none;
  border: 1.5px solid var(--line);
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 11px;
  font-weight: 500;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 140ms ease, color 140ms ease;
}

.mem-btn-subtle:hover { color: var(--eye); border-color: var(--eye); }
.mem-btn-danger:hover { color: var(--danger); border-color: var(--danger); }

/* Account row. Lives here rather than floating bottom-left, where on a phone it
   sat alongside the connect button and made the whole bottom edge read as one
   left-packed clump. */
#memAccount {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1.5px solid var(--line);
  font-size: 12px;
  color: var(--muted);
}

#memEmail {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#signOutBtn {
  appearance: none;
  border: none;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  padding: 2px 0;
  cursor: pointer;
  flex: none;
  text-decoration: underline;
  text-underline-offset: 3px;
}

#signOutBtn:hover { color: var(--ink); }
#signOutBtn[hidden] { display: none; }

/* ── caption ───────────────────────────────────────────────── */

#caption {
  position: fixed;
  left: 50%;
  bottom: calc(78px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  margin: 0;
  max-width: min(680px, 88vw);
  text-align: center;
  font-size: 15px;
  line-height: 1.5;
  color: var(--muted);
  pointer-events: none;
  opacity: 0;
  transition: opacity 300ms ease;
  z-index: 4;
}

#caption:not(:empty) { opacity: 1; }

/* Spoken words: one at a time, so each can carry real weight. The opacity
   transition above is also what fades the final word out when a turn ends. */
#caption[data-mode="word"] {
  bottom: calc(92px + env(safe-area-inset-bottom));
  font-size: clamp(24px, 4.4vw, 36px);
  font-weight: 600;
  letter-spacing: -0.015em;
  line-height: 1.2;
  color: var(--eye);
  /* Replacement is instant; only the fade at the end of a turn animates. */
  transition: opacity 260ms ease;
}

/* ── first-run hint ────────────────────────────────────────── */

#hint {
  position: fixed;
  top: calc(16px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  max-width: min(560px, 90vw);
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 12px 18px;
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink);
  z-index: 20;
}

#hint code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.92em;
  background: rgb(255 255 255 / 0.07);
  padding: 1px 6px;
  border-radius: 5px;
}

/* Expression changes still read; it's the ambient fidgeting that gets damped
   (the rest is handled in face.js). */
@media (prefers-reduced-motion: reduce) {
  #status::before { animation: none !important; }
}

/* The glow is two stacked drop-shadows, and a filtered SVG is re-rasterised
   every frame the face moves — which is every frame. On a desktop GPU that's
   free; on a phone it competes with the audio scheduling for the main thread
   and speech starts breaking up. One shadow at a smaller radius reads almost
   identically at phone size and costs a fraction. */
@media (max-width: 560px), (pointer: coarse) {
  #face {
    filter: drop-shadow(0 0 7px var(--glow-near));
  }
}

/* Someone who has asked for less motion gets no bloom either — it's the most
   expensive thing on screen and the least necessary. */
@media (prefers-reduced-motion: reduce) {
  #face { filter: none; }
}

/* ── phone layout ──────────────────────────────────────────────
   Deliberately last in the file. These override #controls, #status and
   #caption, all of which are declared in different sections above — and a
   media query carries no extra specificity, so it only wins by coming after.
   Twice now this block has silently done nothing from higher up. */

@media (max-width: 560px) {
  #controls {
    grid-template-columns: 1fr;
    justify-items: center;
    row-gap: 9px;
  }

  #connectBtn { grid-column: 1; grid-row: 2; }
  #status { grid-column: 1; grid-row: 1; justify-self: center; margin-left: 0; }

  /* The stack is taller, so the subtitle needs to sit higher to clear it. */
  #caption { bottom: calc(110px + env(safe-area-inset-bottom)); }
  #caption[data-mode="word"] { bottom: calc(120px + env(safe-area-inset-bottom)); }
}


/* ── document drop ──────────────────────────────────────────── */

@keyframes attach-pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.45; }
}

#dropVeil {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(3px);
  animation: veil-in 140ms ease-out;
}

#dropVeil[hidden] { display: none; }

@keyframes veil-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.drop-inner {
  text-align: center;
  color: var(--ink);
  pointer-events: none;
}

.drop-ring {
  width: 96px;
  height: 96px;
  margin: 0 auto 18px;
  border-radius: 50%;
  border: 2px dashed var(--eye);
  box-shadow: 0 0 34px var(--glow-far);
  animation: ring-breathe 1.6s ease-in-out infinite;
}

@keyframes ring-breathe {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.07); }
}

.drop-inner p {
  margin: 0 0 6px;
  font-size: 17px;
  letter-spacing: 0.01em;
}

.drop-inner small {
  color: var(--muted);
  font-size: 12.5px;
}

/* ── draft action card ──────────────────────────────────────── */

#actionCard {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(112px + env(safe-area-inset-bottom));
  z-index: 40;
  width: min(420px, calc(100vw - 32px));
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 12px 12px 15px;
  border: 1.5px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  backdrop-filter: blur(8px);
  animation: card-in 220ms cubic-bezier(0.2, 0.9, 0.3, 1);
}

#actionCard[hidden] { display: none; }

@keyframes card-in {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to   { opacity: 1; transform: translate(-50%, 0); }
}

.action-body { flex: 1; min-width: 0; }

.action-kind {
  display: block;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--eye);
  margin-bottom: 3px;
}

#actionDetail {
  margin: 0;
  font-size: 13px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.action-buttons { display: flex; align-items: center; gap: 6px; }

.action-open {
  display: inline-block;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1.5px solid var(--eye);
  color: var(--eye);
  font-size: 12.5px;
  text-decoration: none;
  white-space: nowrap;
  transition: background 160ms ease, color 160ms ease;
}

.action-open:hover { background: var(--eye); color: var(--bg); }

.action-dismiss {
  appearance: none;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  padding: 2px 6px;
  cursor: pointer;
}

.action-dismiss:hover { color: var(--ink); }

@media (max-width: 480px) {
  #attachBtn { right: calc(106px + env(safe-area-inset-right)); }
  #actionCard { bottom: calc(96px + env(safe-area-inset-bottom)); }
}


/* ── account status ─────────────────────────────────────────── */

.status-banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 13px;
  margin-bottom: 10px;
  border-radius: 9px;
  border: 1.5px solid var(--line);
  background: var(--panel);
}

.status-banner[hidden] { display: none; }
.status-banner[data-state="pending"]  { border-color: var(--warn); }
.status-banner[data-state="paused"]   { border-color: var(--warn); }
.status-banner[data-state="declined"] { border-color: var(--danger); }

.status-chip {
  flex: none;
  font-size: 10px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  padding: 3px 7px;
  border-radius: 4px;
  background: var(--bg-2);
  color: var(--ink);
}

.status-banner[data-state="pending"] .status-chip,
.status-banner[data-state="paused"] .status-chip { color: var(--warn); }
.status-banner[data-state="declined"] .status-chip { color: var(--danger); }

.status-banner p {
  margin: 0;
  font-size: 12.4px;
  line-height: 1.45;
  color: var(--muted);
}

.credit-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 9px;
  font-size: 12.4px;
  color: var(--muted);
}

.credit-row[hidden] { display: none; }

.credit-request {
  appearance: none;
  border: 1.5px solid var(--line);
  background: none;
  color: var(--ink);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 160ms ease, color 160ms ease;
}

.credit-request:hover:not(:disabled) { border-color: var(--eye); color: var(--eye); }
.credit-request:disabled { opacity: 0.55; cursor: default; }
