/* ── Design tokens ──────────────────────────────────────────────────────── */
:root {
  --text-xs:   clamp(0.75rem,  0.7rem  + 0.25vw, 0.875rem);
  --text-sm:   clamp(0.875rem, 0.8rem  + 0.35vw, 1rem);
  --text-base: clamp(1rem,     0.95rem + 0.25vw, 1.125rem);
  --text-lg:   clamp(1.125rem, 1rem    + 0.75vw, 1.5rem);
  --text-xl:   clamp(1.5rem,   1.2rem  + 1.25vw, 2.25rem);

  --space-1: 0.25rem; --space-2: 0.5rem;  --space-3: 0.75rem;
  --space-4: 1rem;    --space-5: 1.25rem; --space-6: 1.5rem;
  --space-8: 2rem;

  --radius-sm: 0.375rem; --radius-md: 0.5rem;
  --radius-lg: 0.75rem;  --radius-xl: 1rem; --radius-full: 9999px;

  --transition: 180ms cubic-bezier(0.16, 1, 0.3, 1);

  /* Dark (default) */
  --color-bg:           #0f0e0c;
  --color-surface:      #1a1917;
  --color-surface-2:    #222120;
  --color-border:       #333230;
  --color-text:         #e8e6e3;
  --color-text-muted:   #888784;
  --color-text-faint:   #55524f;
  --color-primary:      #4f98a3;
  --color-primary-h:    #227f8b;
  --color-error:        #d163a7;
  --color-success:      #6daa45;
  --shadow-sm: 0 1px 3px oklch(0 0 0 / 0.35);
  --shadow-md: 0 4px 16px oklch(0 0 0 / 0.45);
  --bubble-self-bg:     #1c3a3d;
  --bubble-self-border: #2a5558;
  --bubble-them-bg:     #222120;
  --bubble-them-border: #333230;
}

[data-theme="light"] {
  --color-bg:           #f7f6f2;
  --color-surface:      #f9f8f5;
  --color-surface-2:    #f3f0ec;
  --color-border:       #d4d1ca;
  --color-text:         #28251d;
  --color-text-muted:   #7a7974;
  --color-text-faint:   #bab9b4;
  --color-primary:      #01696f;
  --color-primary-h:    #0c4e54;
  --color-error:        #a12c7b;
  --color-success:      #437a22;
  --shadow-sm: 0 1px 2px oklch(0.2 0.01 80 / 0.06);
  --shadow-md: 0 4px 12px oklch(0.2 0.01 80 / 0.09);
  --bubble-self-bg:     #d4e8ea;
  --bubble-self-border: #b0d4d8;
  --bubble-them-bg:     #f3f0ec;
  --bubble-them-border: #d4d1ca;
}

/* ── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html {
  -webkit-text-size-adjust: none; text-size-adjust: none;
  -webkit-font-smoothing: antialiased;
  height: 100%; overflow: hidden;
}
body {
  font-family: 'Satoshi', system-ui, sans-serif;
  font-size: var(--text-base);
  color: var(--color-text);
  background: var(--color-bg);
  height: 100%; overflow: hidden;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
button { cursor: pointer; background: none; border: none; font: inherit; color: inherit; }
input, textarea { font: inherit; color: inherit; background: transparent; border: none; }
img, svg { display: block; }
a, button { transition: color var(--transition), background var(--transition), opacity var(--transition); }
:focus-visible { outline: 2px solid var(--color-primary); outline-offset: 3px; border-radius: var(--radius-sm); }

/* ── Screen system ──────────────────────────────────────────────────────── */
#app { width: 100%; height: 100%; position: relative; overflow: hidden; }
.screen {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  opacity: 0; pointer-events: none;
  transform: translateX(24px);
  transition: opacity 220ms ease, transform 220ms ease;
}
.screen.active { opacity: 1; pointer-events: auto; transform: translateX(0); }

/* ── Nav bar ────────────────────────────────────────────────────────────── */
.nav-bar {
  display: flex; align-items: center; gap: var(--space-3);
  padding: var(--space-4);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg);
  min-height: 56px; flex-shrink: 0;
}
.nav-title { font-size: var(--text-base); font-weight: 600; flex: 1; }
.btn-back {
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-md); color: var(--color-text-muted);
  margin-left: calc(-1 * var(--space-2));
}
.btn-back:hover { color: var(--color-text); background: var(--color-surface); }

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--space-2); padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-full); font-size: var(--text-sm); font-weight: 600;
  min-height: 48px; white-space: nowrap;
}
.btn-lg { min-height: 56px; padding: var(--space-4) var(--space-8); font-size: var(--text-base); }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-h); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--color-text-muted); border: 1px solid var(--color-border); }
.btn-ghost:hover { color: var(--color-text); background: var(--color-surface); }

/* ── Home ───────────────────────────────────────────────────────────────── */
#screen-home { justify-content: center; }
.home-wrap {
  padding: var(--space-8);
  display: flex; flex-direction: column; align-items: center;
  gap: var(--space-6); text-align: center;
}
.logo-block { display: flex; align-items: center; gap: var(--space-3); }
.logo-icon { width: 40px; height: 40px; }
.logo-text { font-size: var(--text-xl); font-weight: 700; letter-spacing: -0.02em; }
.home-tagline { color: var(--color-text-muted); max-width: 28ch; }
.home-actions { display: flex; flex-direction: column; gap: var(--space-3); width: 100%; max-width: 320px; }
.home-actions .btn { width: 100%; }

/* ── Join ───────────────────────────────────────────────────────────────── */
.join-wrap {
  flex: 1; overflow-y: auto; padding: var(--space-6) var(--space-4);
  display: flex; flex-direction: column; gap: var(--space-5);
}
.qr-scan-area {
  position: relative; width: 100%; max-width: 280px; margin: 0 auto;
  border-radius: var(--radius-xl); overflow: hidden;
  background: #000; aspect-ratio: 1;
}
#qr-video { width: 100%; height: 100%; object-fit: cover; }
.qr-frame {
  position: absolute; inset: 20px;
  border: 2px solid var(--color-primary); border-radius: var(--radius-lg);
  pointer-events: none;
}
.qr-hint {
  position: absolute; bottom: var(--space-3); left: 0; right: 0;
  text-align: center; font-size: var(--text-xs); color: rgba(255,255,255,0.65);
}
.divider-label {
  text-align: center; font-size: var(--text-xs); color: var(--color-text-faint);
  position: relative; padding: 0 var(--space-4);
}
.divider-label::before, .divider-label::after {
  content: ''; position: absolute; top: 50%;
  width: calc(50% - 90px); height: 1px; background: var(--color-border);
}
.divider-label::before { left: 0; } .divider-label::after { right: 0; }
.code-input-row { display: flex; gap: var(--space-2); }
.code-input {
  flex: 1; background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: var(--radius-md); padding: var(--space-3) var(--space-4);
  font-size: var(--text-lg); font-weight: 700; text-align: center;
  letter-spacing: 0.15em; min-height: 56px;
  transition: border-color var(--transition);
}
.code-input:focus { border-color: var(--color-primary); outline: none; }
.field-error { font-size: var(--text-sm); color: var(--color-error); min-height: 1.4em; }

/* ── Waiting ────────────────────────────────────────────────────────────── */
.waiting-wrap {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: var(--space-6); padding: var(--space-8) var(--space-4);
}
.waiting-qr-block { display: flex; flex-direction: column; align-items: center; gap: var(--space-4); }
#qr-code-display canvas { border-radius: var(--radius-lg); }
.waiting-code-label { font-size: var(--text-xs); color: var(--color-text-muted); text-transform: uppercase; letter-spacing: 0.08em; }
.pairing-code-big {
  font-size: var(--text-xl); font-weight: 700; letter-spacing: 0.2em;
  color: var(--color-primary); font-variant-numeric: tabular-nums;
}
.waiting-hint { font-size: var(--text-sm); color: var(--color-text-muted); text-align: center; max-width: 28ch; }
.waiting-spinner { display: flex; gap: var(--space-2); }
.dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--color-primary);
  animation: pulse 1.4s ease-in-out infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes pulse {
  0%, 80%, 100% { opacity: 0.2; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ── Language pick ──────────────────────────────────────────────────────── */
.lang-wrap {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: var(--space-6); padding: var(--space-8) var(--space-4);
}
.lang-hint { color: var(--color-text-muted); text-align: center; }
.lang-hint strong { color: var(--color-text); }
.lang-grid { display: flex; flex-direction: column; gap: var(--space-3); width: 100%; max-width: 320px; }
.lang-btn {
  width: 100%; min-height: 64px;
  background: var(--color-surface); border: 1px solid var(--color-border);
  border-radius: var(--radius-xl); padding: var(--space-4) var(--space-6);
  font-size: var(--text-base); font-weight: 600; text-align: left;
  display: flex; align-items: center; gap: var(--space-3);
  transition: background var(--transition), border-color var(--transition);
}
.lang-btn:hover { background: var(--color-surface-2, #2a2927); border-color: var(--color-primary); }
.lang-btn .lang-flag { font-size: 1.6rem; line-height: 1; }
.lang-btn.taken { opacity: 0.3; pointer-events: none; }

/* ── Session / Chat ─────────────────────────────────────────────────────── */
.session-bar { justify-content: space-between; }
.session-langs {
  font-size: var(--text-sm); font-weight: 600;
  display: flex; align-items: center; gap: var(--space-2);
}
.session-langs .arrow { color: var(--color-primary); }
.session-meta { display: flex; align-items: center; gap: var(--space-3); }
.session-code { font-size: var(--text-xs); color: var(--color-text-faint); letter-spacing: 0.05em; }
.conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--color-text-faint); transition: background var(--transition);
}
.conn-dot.connected { background: var(--color-success); }
.conn-dot.connecting { background: var(--color-primary); animation: pulse 1.2s ease-in-out infinite; }

.messages {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  padding: var(--space-4);
  display: flex; flex-direction: column; gap: var(--space-3);
  scroll-behavior: smooth;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }

.msg-row { display: flex; flex-direction: column; max-width: 82%; }
.msg-row.self { align-self: flex-end; align-items: flex-end; }
.msg-row.them { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-xl);
  line-height: 1.5; word-break: break-word;
  box-shadow: var(--shadow-sm);
}
.msg-row.self .bubble {
  background: var(--bubble-self-bg);
  border: 1px solid var(--bubble-self-border);
  border-bottom-right-radius: var(--radius-sm);
}
.msg-row.them .bubble {
  background: var(--bubble-them-bg);
  border: 1px solid var(--bubble-them-border);
  border-bottom-left-radius: var(--radius-sm);
}
.bubble-original { font-size: var(--text-base); color: var(--color-text); }
.bubble-translation {
  font-size: var(--text-sm); color: var(--color-text-muted);
  margin-top: var(--space-1); padding-top: var(--space-1);
  border-top: 1px solid oklch(from var(--color-border) l c h / 0.5);
  font-style: italic;
}
.bubble-translation.pending { opacity: 0.45; }
.bubble-meta {
  font-size: var(--text-xs); color: var(--color-text-faint);
  margin-top: var(--space-1); padding: 0 var(--space-2);
}
.msg-system {
  align-self: center; font-size: var(--text-xs); color: var(--color-text-faint);
  text-align: center; padding: var(--space-1) var(--space-3);
}
.empty-state {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: var(--space-3); padding: var(--space-8);
  color: var(--color-text-faint); text-align: center;
}
.empty-state svg { opacity: 0.25; }
.empty-state p { font-size: var(--text-sm); max-width: 24ch; }

/* ── Typing indicator ───────────────────────────────────────────────────── */
.partner-typing {
  min-height: 24px; padding: 0 var(--space-4) var(--space-1);
  font-size: var(--text-xs); color: var(--color-text-faint); font-style: italic;
}

/* ── Composer ───────────────────────────────────────────────────────────── */
.composer {
  display: flex; align-items: flex-end; gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  padding-bottom: max(var(--space-3), env(safe-area-inset-bottom));
  border-top: 1px solid var(--color-border);
  background: var(--color-bg); flex-shrink: 0;
}
.msg-textarea {
  flex: 1; background: var(--color-surface);
  border: 1px solid var(--color-border) !important;
  border-radius: var(--radius-xl);
  padding: var(--space-3) var(--space-4);
  font-size: var(--text-base); resize: none; line-height: 1.5;
  max-height: 120px; overflow-y: auto;
  transition: border-color var(--transition);
}
.msg-textarea:focus { border-color: var(--color-primary) !important; outline: none; }
.msg-textarea::placeholder { color: var(--color-text-faint); }
.btn-send {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--color-primary); color: #fff;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.btn-send:hover:not(:disabled) { background: var(--color-primary-h); }
.btn-send:active:not(:disabled) { transform: scale(0.9); }
.btn-send:disabled { opacity: 0.35; cursor: not-allowed; }

/* ── Theme toggle ───────────────────────────────────────────────────────── */
.theme-toggle {
  position: fixed;
  top: calc(env(safe-area-inset-top) + var(--space-3));
  right: var(--space-4); z-index: 100;
  width: 40px; height: 40px; border-radius: var(--radius-full);
  display: flex; align-items: center; justify-content: center;
  color: var(--color-text-muted);
}
.theme-toggle:hover { color: var(--color-text); background: var(--color-surface); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
