/* ============================================================
   Ours — warm dark theme, self-contained (no external fonts/CDN)
   ============================================================ */

:root {
  /* palette */
  --bg:            #16101a;
  --bg-deep:       #0f0b13;
  --surface:       #1f1723;
  --surface-2:     #2a1f30;
  --surface-3:     #362941;
  --line:          #3b2c45;
  --line-soft:     #2e2337;

  --text:          #f4ecf3;
  --text-dim:      #c6b6c9;
  --text-faint:    #9a8aa0;

  --accent:        #ff8fa3;   /* soft rose */
  --accent-strong: #ff6b88;
  --accent-deep:   #b8455f;
  --accent-soft:   rgba(255, 143, 163, .14);
  --gold:          #e8c98b;   /* warm secondary */
  --ok:            #7fd6a8;
  --warn:          #f0b96b;
  --danger:        #ff7a7a;

  --self-bg:       linear-gradient(160deg, #4a2438 0%, #3a1f30 100%);
  --peer-bg:       linear-gradient(160deg, #241c30 0%, #1e182a 100%);

  /* type */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
          "Noto Sans", "Noto Sans Vietnamese", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

  /* shape */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --r-xl: 26px;
  --shadow: 0 12px 32px rgba(0, 0, 0, .45);
  --shadow-sm: 0 4px 14px rgba(0, 0, 0, .3);

  --pane-gap: 12px;
  --chat-w: 420px;

  color-scheme: dark;
}

/* ---------- reset-ish ---------- */
*, *::before, *::after { box-sizing: border-box; }

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

body {
  background:
    radial-gradient(1100px 700px at 12% -10%, #2a1730 0%, transparent 60%),
    radial-gradient(900px 600px at 105% 110%, #241a30 0%, transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  /* Vietnamese diacritics stack high AND low: generous leading everywhere */
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden; /* panes scroll internally */
}

h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.35; }
p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip-path: inset(50%);
  overflow: hidden; white-space: nowrap;
}

/* JS may toggle either the `hidden` attribute or a `.hidden` class.
   The id-selectors below have to be listed explicitly: an id rule that sets
   `display` would otherwise out-specify the bare `[hidden]` rule. */
[hidden],
#login-screen[hidden],
#app[hidden],
#video-placeholder[hidden],
#video-local[hidden],
#typing-indicator[hidden],
#login-error[hidden] { display: none; }
.hidden { display: none !important; }

/* ---------- focus ---------- */
:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45em;
  min-height: 44px;
  padding: .55em 1.05em;
  border: 1px solid var(--line);
  border-radius: var(--r-xl);
  background: var(--surface-2);
  color: var(--text);
  font-size: .95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background .16s ease, border-color .16s ease,
              transform .1s ease, opacity .16s ease;
}
.btn:hover:not(:disabled) { background: var(--surface-3); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }

.btn-primary {
  background: linear-gradient(150deg, var(--accent-strong), var(--accent-deep));
  border-color: transparent;
  color: #2a0f18;
  font-weight: 650;
}
.btn-primary:hover:not(:disabled) {
  background: linear-gradient(150deg, #ff8298, var(--accent-deep));
}

.btn-danger {
  background: transparent;
  border-color: rgba(255, 122, 122, .5);
  color: var(--danger);
}
.btn-danger:hover:not(:disabled) { background: rgba(255, 122, 122, .12); }

.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

.btn-icon {
  min-width: 44px;
  padding: .5em;
  border-radius: 50%;
  font-size: 1.05rem;
}

.btn-block { width: 100%; }

/* toggle buttons keep an obvious "off" state */
.btn-toggle[aria-pressed="false"] {
  background: rgba(255, 122, 122, .14);
  border-color: rgba(255, 122, 122, .45);
  color: var(--danger);
  text-decoration: line-through;
}

/* ---------- form fields ---------- */
.field { display: flex; flex-direction: column; gap: .3rem; min-width: 0; }
.field > label {
  font-size: .8rem;
  letter-spacing: .02em;
  color: var(--text-dim);
}
.field .opt { color: var(--text-faint); font-weight: 400; }

.field-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: .75rem;
}
@media (min-width: 520px) {
  .field-row { grid-template-columns: 1fr 1fr; }
}

input[type="text"], input[type="password"], select, textarea {
  width: 100%;
  padding: .7rem .85rem;
  background: var(--bg-deep);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  line-height: 1.5;          /* room for ẫ / ộ */
  transition: border-color .16s ease, box-shadow .16s ease;
}
input::placeholder, textarea::placeholder { color: var(--text-faint); }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select {
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: right 1.1rem center, right .85rem center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 2.2rem;
}

/* ============================================================
   Login
   ============================================================ */
#login-screen {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: max(1.25rem, env(safe-area-inset-top)) 1.25rem
           max(1.25rem, env(safe-area-inset-bottom));
  overflow-y: auto;
}

.login-card {
  width: min(100%, 420px);
  padding: clamp(1.5rem, 5vw, 2.25rem);
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow);
}

.login-head { text-align: center; margin-bottom: 1.5rem; }
.login-mark {
  font-size: 2rem;
  color: var(--accent);
  filter: drop-shadow(0 0 12px rgba(255, 143, 163, .35));
}
.login-head h1 {
  font-size: 1.8rem;
  letter-spacing: .01em;
  margin: .15rem 0 .35rem;
}
.login-sub { color: var(--text-dim); font-size: .88rem; }

#login-form { display: flex; flex-direction: column; gap: 1rem; }

.login-error {
  padding: .6rem .8rem;
  background: rgba(255, 122, 122, .12);
  border: 1px solid rgba(255, 122, 122, .45);
  border-radius: var(--r-md);
  color: #ffc2c2;
  font-size: .88rem;
}

/* ============================================================
   App shell
   ============================================================ */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;      /* dvh follows the on-screen keyboard */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.topbar {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .5rem .75rem;
  border-bottom: 1px solid var(--line-soft);
  background: rgba(15, 11, 19, .6);
  backdrop-filter: blur(8px);
  flex: 0 0 auto;
}

.brand {
  font-size: 1rem;
  letter-spacing: .02em;
  white-space: nowrap;
}
.brand span { color: var(--accent); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: .4em;
  padding: .18rem .6rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: .74rem;
  color: var(--text-dim);
  white-space: nowrap;
}
.badge::before {
  content: "";
  width: .5em; height: .5em;
  border-radius: 50%;
  background: var(--text-faint);
}
/* the JS sets data-state; class names are also honoured for flexibility */
.badge[data-state="online"]::before,
.badge[data-state="connected"]::before,
.badge.online::before { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.badge[data-state="connecting"]::before,
.badge.connecting::before { background: var(--warn); animation: pulse 1.4s ease-in-out infinite; }
.badge[data-state="offline"]::before,
.badge[data-state="error"]::before,
.badge.offline::before { background: var(--danger); }

.call-status {
  flex: 1 1 auto;
  min-width: 0;
  font-size: .78rem;
  color: var(--text-faint);
  text-align: right;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#btn-settings { flex: 0 0 auto; }

/* ---------- panes ---------- */
.panes {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--pane-gap);
  padding: var(--pane-gap);
}

.pane { min-height: 0; min-width: 0; display: flex; flex-direction: column; }

/* ============================================================
   Video
   ============================================================ */
.pane-video { flex: 0 0 auto; gap: .6rem; }

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 10;
  max-height: 34dvh;
  background:
    radial-gradient(70% 70% at 50% 35%, #241a2e 0%, var(--bg-deep) 100%);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

#video-remote {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}

#video-local {
  position: absolute;
  right: 3%;
  bottom: 4%;
  width: 27%;
  max-width: 160px;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  background: #000;
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  transform: scaleX(-1);   /* mirror your own preview */
  z-index: 2;
}

#video-placeholder {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: .3rem;
  padding: 1rem;
  text-align: center;
  background: inherit;
}
.placeholder-mark {
  font-size: 2.2rem;
  color: var(--accent);
  opacity: .8;
  animation: heartbeat 3.2s ease-in-out infinite;
}
.placeholder-title { font-size: 1rem; color: var(--text-dim); }
.placeholder-hint { font-size: .82rem; color: var(--text-faint); }
.placeholder-hint strong { color: var(--accent); font-weight: 600; }

.call-controls {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  justify-content: center;
}
.call-controls .btn { flex: 1 1 auto; min-width: 108px; }

/* ============================================================
   Chat
   ============================================================ */
.pane-chat {
  flex: 1 1 auto;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-lg);
  overflow: hidden;
}

#chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-3) transparent;
  display: flex;
  flex-direction: column;
  gap: .55rem;
  padding: .9rem .8rem;
}
#chat-log::-webkit-scrollbar { width: 8px; }
#chat-log::-webkit-scrollbar-thumb {
  background: var(--surface-3);
  border-radius: 999px;
}

/* ---------- messages ---------- */
.msg {
  max-width: min(88%, 46ch);
  padding: .55rem .8rem .5rem;
  border: 1px solid var(--line-soft);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  overflow-wrap: anywhere;
  animation: rise .22s ease both;
}

.msg-self {
  align-self: flex-end;
  background: var(--self-bg);
  border-color: rgba(255, 143, 163, .22);
  border-bottom-right-radius: var(--r-sm);
}
.msg-peer {
  align-self: flex-start;
  background: var(--peer-bg);
  border-bottom-left-radius: var(--r-sm);
}

/* translated line is the one you actually read */
.msg-translated {
  font-size: 1.02rem;
  line-height: 1.65;        /* diacritics never clip */
  color: var(--text);
  padding-bottom: .1em;     /* descenders + tone marks get room */
}

/* original is the quiet reference under it */
.msg-original {
  margin-top: .3rem;
  padding-top: .3rem;
  border-top: 1px dashed var(--line);
  font-size: .84rem;
  line-height: 1.55;
  color: var(--text-faint);
}

/* if a message has no translation yet, the original carries the message */
.msg-pending .msg-original,
.msg-error .msg-original {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
  font-size: 1.02rem;
  line-height: 1.65;
  color: var(--text);
}

.msg-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .4rem;
  margin-bottom: .25rem;
  font-size: .72rem;
  color: var(--text-faint);
}
.msg-self .msg-meta { justify-content: flex-end; }

.msg-time {
  font-size: .7rem;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
}

.msg-lang-tag {
  display: inline-block;
  padding: .05rem .45rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-size: .64rem;
  font-weight: 600;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-dim);
  white-space: nowrap;
}
.msg-self .msg-lang-tag {
  border-color: rgba(255, 143, 163, .35);
  color: var(--accent);
}

/* pending: subdued, gently breathing */
.msg-pending { opacity: .78; }
.msg-pending .msg-translated {
  color: var(--text-faint);
  font-style: italic;
  animation: pulse 1.5s ease-in-out infinite;
}
.msg-pending .msg-meta::after {
  content: "translating…";
  color: var(--gold);
  font-style: italic;
  animation: pulse 1.5s ease-in-out infinite;
}

/* error: accent border + a retry you cannot miss */
.msg-error {
  border-color: var(--accent-strong);
  border-left: 3px solid var(--accent-strong);
  background-image: linear-gradient(90deg, var(--accent-soft), transparent 42%);
}
.msg-error .msg-translated {
  color: var(--warn);
  font-size: .84rem;
  font-style: italic;
}
.msg-error .msg-translated:empty::before {
  content: "Translation unavailable";
}

.msg-retry {
  display: inline-flex;
  align-items: center;
  gap: .35em;
  margin-top: .45rem;
  min-height: 32px;
  padding: .2rem .75rem;
  border: 1px solid var(--accent-strong);
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: .78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .16s ease, color .16s ease;
}
.msg-retry:hover { background: var(--accent-strong); color: #2a0f18; }
.msg-retry:disabled { opacity: .5; cursor: not-allowed; }
/* Hidden on a healthy message; always available on a failed one. */
.msg .msg-retry { display: none; }
.msg-error .msg-retry, .msg-retry.msg-error { display: inline-flex; }

/* A message can also get stranded in `pending` — the server was interrupted while the
   translation was in flight, and the row is replayed from history still saying
   "translating…". Such a bubble must never become a dead end, so Retry is present on
   pending too: laid out collapsed and unfocusable (visibility:hidden) while translation
   still has a fair chance, then unfolded at ~60s. The delay survives
   prefers-reduced-motion (only animation-duration is clamped above), so the button always
   arrives; replayed-from-history pending rows surface it a minute after they render. */
.msg-pending .msg-retry {
  display: inline-flex;
  visibility: hidden;
  overflow: hidden;
  height: 0;
  min-height: 0;
  margin-top: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-width: 0;
  opacity: 0;
  animation: retry-surface .25s ease 60s forwards;
}

/* ---------- typing indicator ---------- */
#typing-indicator {
  flex: 0 0 auto;
  min-height: 1.4rem;
  padding: 0 1rem .15rem;
  font-size: .78rem;
  font-style: italic;
  color: var(--accent);
  opacity: .85;
  animation: pulse 1.6s ease-in-out infinite;
}
#typing-indicator[hidden] { display: none; }

/* ---------- composer ---------- */
#chat-form {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  padding: .6rem .7rem calc(.6rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line-soft);
  background: var(--surface);
}

#chat-input {
  flex: 1 1 auto;
  min-height: 44px;
  max-height: 9rem;
  resize: none;
  overflow-y: auto;
  border-radius: var(--r-lg);
  padding: .6rem .85rem;
  line-height: 1.55;
}

#btn-send { flex: 0 0 auto; align-self: flex-end; }

/* ============================================================
   Settings dialog
   ============================================================ */
#settings-dialog {
  width: min(100% - 1.5rem, 560px);
  max-height: min(88dvh, 800px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  overflow: hidden;
}
#settings-dialog::backdrop {
  background: rgba(10, 6, 13, .68);
  backdrop-filter: blur(3px);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--line-soft);
  background: var(--surface-2);
  position: sticky;
  top: 0;
  z-index: 1;
}
.dialog-head h2 { font-size: 1.05rem; }

.dialog-body {
  max-height: calc(min(88dvh, 800px) - 60px);
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.dialog-section { display: flex; flex-direction: column; gap: .7rem; }
.dialog-section h3 { font-size: .95rem; color: var(--accent); }
.hint { font-size: .82rem; color: var(--text-faint); line-height: 1.55; }
.hint em { color: var(--gold); font-style: normal; }

#glossary-form { display: flex; flex-direction: column; gap: .75rem; }
#glossary-form .btn { align-self: flex-start; }

#glossary-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
#glossary-list:empty::after {
  content: "No terms yet.";
  color: var(--text-faint);
  font-size: .82rem;
  font-style: italic;
}
#glossary-list > * {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .35rem .6rem;
  padding: .5rem .7rem;
  background: var(--bg-deep);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-md);
  font-size: .88rem;
  line-height: 1.5;
}
#glossary-list button {
  margin-left: auto;
  min-height: 32px;
  padding: .1rem .6rem;
  border: 1px solid rgba(255, 122, 122, .45);
  border-radius: 999px;
  background: transparent;
  color: var(--danger);
  font-size: .78rem;
  cursor: pointer;
}
#glossary-list button:hover { background: rgba(255, 122, 122, .12); }

/* ============================================================
   Animations
   ============================================================ */
@keyframes pulse    { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }
@keyframes rise     { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
/* unfolds the Retry button on a message stuck in `pending` (see .msg-pending .msg-retry) */
@keyframes retry-surface {
  to {
    visibility: visible;
    height: 32px;
    margin-top: .45rem;
    padding-top: .2rem;
    padding-bottom: .2rem;
    border-width: 1px;
    opacity: 1;
  }
}
@keyframes heartbeat{ 0%, 100% { transform: scale(1); } 12% { transform: scale(1.14); } 26% { transform: scale(1); } 38% { transform: scale(1.09); } }

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

/* ============================================================
   Responsive — side-by-side once there is room
   ============================================================ */
@media (min-width: 900px) {
  .panes { flex-direction: row; padding: 14px; --pane-gap: 14px; }

  .pane-video { flex: 1 1 auto; }
  .stage {
    flex: 1 1 auto;
    aspect-ratio: auto;
    max-height: none;
    min-height: 0;
  }
  .call-controls { flex: 0 0 auto; }
  .call-controls .btn { flex: 0 1 auto; }

  .pane-chat { flex: 0 0 var(--chat-w); max-width: 46vw; }

  .topbar { padding: .6rem 1rem; }
  .brand { font-size: 1.1rem; }
}

@media (min-width: 1400px) {
  :root { --chat-w: 470px; }
}

/* very short landscape phones: keep the composer usable */
@media (max-height: 520px) and (max-width: 899px) {
  .stage { max-height: 26dvh; }
  .call-controls .btn { min-width: 0; padding: .45em .7em; font-size: .85rem; }
}

@media (prefers-contrast: more) {
  :root {
    --text-dim: #e2d6e4;
    --text-faint: #c3b4c6;
    --line: #6a5478;
    --line-soft: #55405f;
  }
}

/* ---- destructive actions ------------------------------------------------ */

.dialog-section-danger {
  border-top: 1px solid var(--line, rgba(255, 255, 255, 0.08));
  padding-top: 1rem;
}

.btn-danger {
  background: transparent;
  border: 1px solid var(--danger, #b4404a);
  color: var(--danger-text, #ff8a94);
}

.btn-danger:hover,
.btn-danger:focus-visible {
  background: color-mix(in srgb, var(--danger, #b4404a) 18%, transparent);
}

/* Armed state: the second click is the one that actually deletes, so it has to
   look unmistakably different from the first. */
.btn-danger[data-armed='true'] {
  background: var(--danger, #b4404a);
  color: #fff;
  font-weight: 600;
}
