:root {
  --black: #0a0a0a;
  --black-soft: #111111;
  --black-panel: #161616;
  --gold: #d4af37;
  --gold-light: #f0d875;
  --gold-dim: #9a7b2a;
  --gold-glow: rgba(212, 175, 55, 0.35);
  --white: #ffffff;
  --white-soft: #e8e8e8;
  --white-dim: #a8a8a8;
  --border: #2a2418;
  --border-gold: #5c4a1a;
  --error: #ff4d4d;
  --top-bar-h: 2.75rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--black);
}

body {
  margin: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  position: fixed;
  inset: 0;
  font-family: "Segoe UI", "Helvetica Neue", system-ui, sans-serif;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(212, 175, 55, 0.12) 0%, transparent 55%),
    linear-gradient(180deg, #0d0d0d 0%, var(--black) 100%);
  background-attachment: fixed;
  color: var(--white-soft);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.hidden { display: none !important; }

/* ── App shell (fixed viewport, no page scroll) ── */
.shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  padding:
    var(--safe-top)
    var(--safe-right)
    var(--safe-bottom)
    var(--safe-left);
}

.top-bar {
  flex: 0 0 var(--top-bar-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 0.25rem;
  min-height: var(--top-bar-h);
}

.screen {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.setup-screen {
  align-items: center;
  justify-content: center;
  padding: 0.75rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.app-screen {
  width: 100%;
  max-width: 920px;
  margin: 0 auto;
}

/* ── Setup panel ── */
.panel.setup {
  width: 100%;
  max-width: 480px;
  padding: 1.5rem;
  background: var(--black-panel);
  border: 1px solid var(--border-gold);
  border-radius: 12px;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(212, 175, 55, 0.08);
}

.panel.setup h1 {
  margin: 0 0 0.5rem;
  font-weight: 300;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--white);
}

.panel.setup h1 span { color: var(--gold); }

.panel.setup label {
  display: block;
  margin: 1rem 0 0.4rem;
  font-size: 0.8rem;
  color: var(--white-dim);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.panel.setup input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border-gold);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
}

.panel.setup input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 2px var(--gold-glow);
}

.hint { font-size: 0.78rem; color: var(--white-dim); margin-top: 1rem; }

/* ── Buttons ── */
.panel.setup button, .btn {
  cursor: pointer;
  border: 1px solid var(--border-gold);
  background: var(--black-soft);
  color: var(--white);
  padding: 0.65rem 1.2rem;
  border-radius: 8px;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: border-color 0.2s, box-shadow 0.2s;
  min-height: 44px;
  touch-action: manipulation;
  user-select: none;
}

.panel.setup button:hover, .btn:hover:not(:disabled) {
  border-color: var(--gold);
  box-shadow: 0 0 16px var(--gold-glow);
}

.panel.setup button {
  margin-top: 1.2rem;
  width: 100%;
  background: linear-gradient(135deg, #1a1608 0%, #0a0a0a 100%);
  border-color: var(--gold-dim);
  color: var(--gold-light);
}

.btn.primary {
  background: linear-gradient(135deg, #2a2210 0%, #111111 100%);
  border-color: var(--gold);
  color: var(--gold-light);
}

.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Header ── */
.app-header {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.35rem 1rem 0.65rem;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.brand-main {
  font-size: 1.25rem;
  letter-spacing: 0.18em;
  font-weight: 700;
  color: var(--white);
  line-height: 1;
  white-space: nowrap;
}

.brand-main span { color: var(--gold); }

.brand-sub {
  font-size: 0.58rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--white-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  flex-shrink: 0;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border-gold);
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--gold-light);
  background: rgba(212, 175, 55, 0.06);
}

.badge.LISTENING { border-color: var(--gold); color: var(--gold-light); box-shadow: 0 0 12px var(--gold-glow); }
.badge.SPEAKING { border-color: var(--white); color: var(--white); background: rgba(255,255,255,0.06); }
.badge.THINKING { border-color: var(--gold-dim); color: var(--gold); animation: pulse-badge 1.5s ease infinite; }
.badge.READY { border-color: var(--gold); color: var(--gold-light); }
.badge.MUTED { border-color: var(--white-dim); color: var(--white-dim); }
.badge.OFFLINE { border-color: var(--error); color: var(--error); }
.badge.INITIALISING { border-color: var(--border-gold); color: var(--white-dim); }

@keyframes pulse-badge {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

/* ── Main layout (flex fill, only log scrolls) ── */
.app-main {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding: 0.35rem 1rem 0.5rem;
  overflow: hidden;
}

.hud {
  flex: 0 0 auto;
  text-align: center;
  padding: 0.25rem 0.5rem 0;
}

.orb {
  width: min(28vw, 140px);
  height: min(28vw, 140px);
  min-width: 96px;
  min-height: 96px;
  margin: 0 auto 0.5rem;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 30%, rgba(255,255,255,0.08) 0%, transparent 40%),
    radial-gradient(circle, #1a1508 0%, #0a0a0a 65%);
  border: 2px solid var(--gold-dim);
  box-shadow:
    0 0 50px rgba(212, 175, 55, 0.12),
    inset 0 0 30px rgba(0, 0, 0, 0.8);
  transition: box-shadow 0.3s, border-color 0.3s;
}

.orb.LISTENING {
  border-color: var(--gold);
  box-shadow: 0 0 70px var(--gold-glow), inset 0 0 20px rgba(212, 175, 55, 0.08);
  animation: pulse 2s ease-in-out infinite;
}

.orb.SPEAKING {
  border-color: var(--white);
  box-shadow: 0 0 80px rgba(255, 255, 255, 0.15);
  animation: pulse 0.8s ease-in-out infinite;
}

.orb.THINKING {
  border-color: var(--gold-light);
  box-shadow: 0 0 60px var(--gold-glow);
  animation: spin-gold 4s linear infinite;
}

.orb.READY { border-color: var(--gold-dim); }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

@keyframes spin-gold {
  0% { box-shadow: 0 0 50px rgba(212, 175, 55, 0.2); }
  50% { box-shadow: 0 0 70px rgba(212, 175, 55, 0.45); }
  100% { box-shadow: 0 0 50px rgba(212, 175, 55, 0.2); }
}

#status-text {
  color: var(--white-dim);
  letter-spacing: 0.04em;
  font-size: 0.82rem;
  margin: 0;
  line-height: 1.3;
}

.controls {
  flex: 0 0 auto;
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}

.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.65rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border-gold);
  background: var(--black-soft);
  color: var(--white-soft);
  cursor: pointer;
  min-height: 44px;
  touch-action: manipulation;
}

.file-btn:hover { border-color: var(--gold); color: var(--gold-light); }

.chat-input {
  flex: 0 0 auto;
  display: flex;
  gap: 0.5rem;
}

.chat-input input {
  flex: 1;
  min-width: 0;
  padding: 0.7rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border-gold);
  background: var(--black);
  color: var(--white);
  font-size: 16px;
}

.chat-input input::placeholder { color: var(--white-dim); }

.chat-input input:focus {
  outline: none;
  border-color: var(--gold);
  box-shadow: 0 0 0 2px var(--gold-glow);
}

.log-panel {
  flex: 1 1 auto;
  min-height: 0;
  background: var(--black-panel);
  border: 1px solid var(--border-gold);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: inset 0 2px 12px rgba(0, 0, 0, 0.4);
}

#log {
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0.75rem;
  font-family: Consolas, "Courier New", monospace;
  font-size: 0.78rem;
  line-height: 1.5;
}

#log .line { margin-bottom: 0.35rem; color: var(--white-dim); word-break: break-word; }
#log .line.you { color: var(--white); }
#log .line.mark { color: var(--gold-light); }
#log .line.err { color: var(--error); }
#log .line.sys { color: var(--gold-dim); font-style: italic; }

/* ── Language toggle ── */
.lang-switch {
  display: inline-flex;
  border: 1px solid var(--border-gold);
  border-radius: 999px;
  overflow: hidden;
  background: var(--black-soft);
}

.lang-btn {
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--white-dim);
  padding: 0.35rem 0.75rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  min-height: 36px;
  min-width: 40px;
  touch-action: manipulation;
}

.lang-btn.active {
  background: linear-gradient(135deg, #2a2210 0%, #1a1608 100%);
  color: var(--gold-light);
}

/* ── Desktop: slightly larger orb ── */
@media (min-width: 641px) {
  .orb {
    width: 160px;
    height: 160px;
  }

  .brand-main { font-size: 1.5rem; letter-spacing: 0.22em; }
  .brand-sub { font-size: 0.65rem; letter-spacing: 0.35em; }
}

/* ── Mobile: stack controls, lock layout ── */
@media (max-width: 640px) {
  .app-header { padding-left: 0.75rem; padding-right: 0.75rem; }
  .app-main { padding-left: 0.75rem; padding-right: 0.75rem; }

  .controls {
    flex-direction: column;
    align-items: stretch;
  }

  .controls .btn,
  .controls .file-btn {
    width: 100%;
  }

  .chat-input {
    flex-direction: column;
  }

  .chat-input input,
  .chat-input .btn {
    width: 100%;
  }
}

/* Standalone / PWA installed */
@media (display-mode: standalone) {
  .top-bar { padding-top: 0.15rem; }
}
