:root {
  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #ebe9e3;
  --ink: #1b1a17;
  --ink-2: #57544c;
  --ink-3: #8c887e;
  --line: #e1ded6;
  --accent: #0d6b63;
  --accent-ink: #ffffff;
  --accent-soft: #d3e9e6;
  --rec: #c23b22;
  --warn: #b7791f;
  --radius: 18px;
  --shadow: 0 1px 2px rgb(20 20 10 / 0.05), 0 6px 20px rgb(20 20 10 / 0.05);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131312;
    --surface: #1d1d1b;
    --surface-2: #282826;
    --ink: #eeece7;
    --ink-2: #b2aea5;
    --ink-3: #7c786f;
    --line: #2f2e2b;
    --accent: #33b7a8;
    --accent-ink: #05201c;
    --accent-soft: #15403b;
    --rec: #f2674f;
    --warn: #e0a54a;
    --shadow: 0 0 0 transparent;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

html[data-auth="checking"] .app { visibility: hidden; }

.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: calc(24px + env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom));
  background: var(--bg);
}
.gate[hidden] { display: none; }
.gate-card {
  width: 100%;
  max-width: 380px;
  display: grid;
  gap: 12px;
  text-align: center;
}
.gate-card img { margin: 0 auto 4px; border-radius: 16px; }
.gate-card h1 { margin: 0; font-size: 22px; }
.gate-card p { margin: 0; color: var(--ink-2); font-size: 15px; line-height: 1.5; }
.gate-card input {
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  font-size: 18px;
  text-align: center;
}
.gate-card .gate-error { min-height: 1.5em; color: var(--rec); font-weight: 600; }
.gate-card .btn { width: 100%; padding: 14px; font-size: 17px; }

.app {
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

/* ---------- Top ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 12px 4px 16px;
}

.modes {
  display: inline-flex;
  padding: 3px;
  border-radius: 999px;
  background: var(--surface-2);
}
.modes button {
  border: 0;
  padding: 8px 15px;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-2);
  font-size: 15px;
  font-weight: 600;
}
.modes button[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.1);
}

.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 16px 8px;
}
.status { min-width: 0; }
.status span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topic-chip {
  flex: none;
  max-width: 55%;
  padding: 5px 12px;
  border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent-soft) 60%, transparent);
  color: var(--accent);
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topic-chip[hidden] { display: none; }

/* Thời gian dịch/đọc: mặc định ẩn, bật trong Cài đặt → Hiển thị. */
.app:not(.show-timing) .chip.timing,
.app:not(.show-timing) .stats { display: none; }

.slider {
  display: grid;
  grid-template-columns: auto 1fr 48px;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  margin-bottom: 6px;
  border: 1px solid var(--line);
  border-radius: 14px;
  font-size: 16px;
}
.slider input { width: 100%; accent-color: var(--accent); }
.slider output { text-align: right; font-variant-numeric: tabular-nums; color: var(--ink-2); }
.topic { flex: none; max-width: 55%; }
.topic select {
  max-width: 100%;
  appearance: none;
  -webkit-appearance: none;
  padding: 6px 28px 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%238c887e' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E") no-repeat right 11px center;
  font-size: 15px;
  font-weight: 600;
  text-overflow: ellipsis;
}
.top-actions { display: flex; gap: 2px; }

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
}

.status-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--ink-3);
}
.status[data-state="connecting"] .status-dot { background: var(--warn); animation: blink 1s ease-in-out infinite; }
.status[data-state="listening"] { color: var(--ink); }
.status[data-state="listening"] .status-dot { background: var(--rec); animation: pulse 1.6s ease-out infinite; }

@keyframes blink { 50% { opacity: 0.3; } }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--rec) 55%, transparent); }
  100% { box-shadow: 0 0 0 8px transparent; }
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 42px; height: 42px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-2);
}
.icon-btn:active { background: var(--surface-2); }

/* ---------- Language bar ---------- */
.langbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 4px;
  padding: 4px 16px 10px;
}

.lang { display: block; min-width: 0; }
.lang-cap {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-3);
  text-align: center;
}
.app[data-mode="conversation"] .langbar { display: none; }
.langbar .swap { align-self: end; margin-bottom: 2px; }

.lang select {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  text-align-last: center;
}
.lang select:disabled { opacity: 0.6; }
.swap { width: 38px; height: 38px; }

/* ---------- Feed ---------- */
.feed {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty {
  margin: auto 0;
  padding: 24px 8px;
  text-align: center;
  color: var(--ink-2);
  font-size: 15px;
  line-height: 1.55;
}
.empty-title { font-size: 20px; font-weight: 650; color: var(--ink); margin-bottom: 4px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 13px 16px 12px;
  box-shadow: inset 4px 0 0 var(--person, transparent), var(--shadow);
}

/* Vuốt thẻ sang trái để lộ nút Xóa */
.swipe-row {
  position: relative;
  flex: none;
}
.swipe-row[hidden] { display: none; }
.swipe-row.removing {
  overflow: hidden;
  opacity: 0;
  transition: height 200ms ease, opacity 200ms ease;
}
.swipe-row > .card {
  position: relative;
  z-index: 1;
  touch-action: pan-y;
  transition: transform 180ms ease;
}
.swipe-row > .card.dragging { transition: none; }
.swipe-delete {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 120px;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  gap: 2px;
  padding: 0 22px 0 0;
  border: 0;
  border-radius: var(--radius);
  background: var(--rec);
  color: #fff;
  font-size: 13px;
  font-weight: 650;
}
.swipe-delete svg { margin-right: 4px; }
.swipe-row:not(.open) .swipe-delete { pointer-events: none; }
/* Chỉ hiện nút Xóa khi đang vuốt hoặc đã mở, để không lộ viền đỏ quanh góc bo của thẻ. */
.swipe-row:not(.open):not(.swiping) .swipe-delete { visibility: hidden; }

.who {
  flex: none;
  font-weight: 700;
  color: var(--ink);
}
.who[hidden] { display: none; }

.orig {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 15px;
  line-height: 1.45;
  color: var(--ink-2);
}

.tag {
  flex: none;
  padding: 1px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink-2);
  text-transform: uppercase;
}

.trans {
  margin: 6px 0 0;
  font-size: 21px;
  line-height: 1.38;
  font-weight: 550;
  letter-spacing: -0.005em;
  overflow-wrap: anywhere;
}
.trans:empty { display: none; }

.nonfinal { color: var(--ink-3); }

.card.ghost {
  background: transparent;
  box-shadow: none;
  border: 1px dashed var(--line);
}

.card.pending .trans::after {
  content: "";
  display: inline-block;
  width: 1.1em;
  height: 0.25em;
  margin-left: 4px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--ink-3) 30%, transparent 30% 40%, var(--ink-3) 40% 70%, transparent 70% 80%, var(--ink-3) 80%);
  opacity: 0.6;
  animation: blink 1s ease-in-out infinite;
}

.card.error .trans { font-size: 15px; font-weight: 500; color: var(--rec); }

.extra {
  display: flex;
  gap: 8px;
  align-items: baseline;
  margin: 6px 0 0;
  font-size: 18px;
  line-height: 1.38;
  font-weight: 500;
}
.extra.failed span:last-child { font-size: 14px; color: var(--rec); }
.extra.waiting span:last-child { color: var(--ink-3); }

.app[data-mode="presentation"] .trans { font-size: 27px; line-height: 1.32; }
.app[data-mode="presentation"] .orig { font-size: 14px; }

.meta {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}
.meta:empty { display: none; }

.chip.dict { border: 0; font-family: inherit; color: var(--accent); font-weight: 600; }
.chip {
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--ink-2);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* ---------- Dock ---------- */
.dock {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "stats mic sound";
  align-items: center;
  gap: 10px;
  padding: 10px 16px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.stats {
  margin: 0;
  white-space: pre-line;
  font-size: 12px;
  line-height: 1.35;
  color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}
.stats { grid-area: stats; }
.sound {
  grid-area: sound;
  justify-self: end;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink);
}
.sound .sound-off { display: none; }
.sound[aria-pressed="false"] { color: var(--ink-3); background: transparent; }
.sound[aria-pressed="false"] .sound-on { display: none; }
.sound[aria-pressed="false"] .sound-off { display: block; }
.now-using { margin: 4px 0 0; font-weight: 600; }
.mic { grid-area: mic; }
.people { grid-area: people; display: none; }

.app[data-mode="conversation"] .dock {
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "stats sound" "people people";
  row-gap: 6px;
  padding-top: 8px;
}
.app[data-mode="conversation"] .mic { display: none; }
.app[data-mode="conversation"] .stats { white-space: normal; }
.app[data-mode="conversation"] .sound { width: 40px; height: 40px; }
.app[data-mode="conversation"] .people {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.people-row {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 4px;
  scrollbar-width: none;
}
.people-row::-webkit-scrollbar { display: none; }

.person {
  flex: none;
  display: grid;
  gap: 2px;
  min-width: 104px;
  padding: 10px 12px;
  border: 2px solid var(--person);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  text-align: left;
  transition: box-shadow 90ms linear, background 120ms ease, transform 120ms ease;
}
.person:active { transform: scale(0.97); }
.person-name {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
}
.person-name::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--person);
}
.person-lang { font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.person[aria-pressed="true"] {
  background: var(--person);
  color: #fff;
  box-shadow: 0 0 0 calc(3px + var(--level, 0) * 12px) color-mix(in srgb, var(--person) 28%, transparent);
}
.person[aria-pressed="true"] .person-name::before { background: #fff; }
.person[aria-pressed="true"] .person-lang { color: rgb(255 255 255 / 0.85); }
.person.add {
  min-width: 64px;
  place-items: center;
  border: 2px dashed var(--line);
  color: var(--ink-2);
  font-size: 15px;
  font-weight: 600;
}

.stop {
  flex: none;
  display: grid;
  place-items: center;
  width: 52px;
  height: 52px;
  border: 0;
  border-radius: 50%;
  background: var(--rec);
  color: #fff;
}
.stop[hidden] { display: none; }

.mic {
  --level: 0;
  position: relative;
  display: grid;
  place-items: center;
  width: 76px; height: 76px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 0 0 calc(4px + var(--level) * 16px) color-mix(in srgb, var(--accent) 22%, transparent);
  transition: box-shadow 90ms linear, transform 120ms ease;
}
.mic:active { transform: scale(0.96); }
.mic:disabled { opacity: 0.6; }
.mic .stop-icon { display: none; }
.mic[aria-pressed="true"] { background: var(--rec); box-shadow: 0 0 0 calc(4px + var(--level) * 16px) color-mix(in srgb, var(--rec) 22%, transparent); }
.mic[aria-pressed="true"] .mic-icon { display: none; }
.mic[aria-pressed="true"] .stop-icon { display: block; }

/* ---------- Settings sheet ---------- */
.sheet {
  width: 100%;
  max-width: 720px;
  max-height: 88dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 22px 22px 0 0;
  background: var(--surface);
  color: var(--ink);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.38); }
.sheet[open] { animation: rise 180ms ease-out; }
@keyframes rise { from { transform: translateY(24px); opacity: 0; } }

.sheet-body { padding: 8px 16px calc(20px + env(safe-area-inset-bottom)); }

.sheet-head {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  background: var(--surface);
}
.sheet-head h2 { margin: 0; font-size: 19px; }

.text-btn {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 16px;
  font-weight: 650;
  padding: 8px 4px;
}

.group {
  margin: 0 0 18px;
  padding: 0;
  border: 0;
}
.group legend {
  padding: 0;
  margin-bottom: 8px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.03em;
  color: var(--ink-3);
  text-transform: uppercase;
}

.hint { margin: 0 0 8px; font-size: 14px; line-height: 1.45; color: var(--ink-2); }
.hint code { font-size: 13px; padding: 1px 5px; border-radius: 5px; background: var(--surface-2); }

.option, .toggle {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 11px 12px;
  margin-bottom: 6px;
  border: 1px solid var(--line);
  border-radius: 14px;
}
.option:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent-soft) 45%, transparent); }
.option:has(input:disabled) { opacity: 0.5; }
.option input, .toggle input { margin-top: 3px; accent-color: var(--accent); width: 18px; height: 18px; flex: none; }
.option span, .toggle span { display: grid; gap: 2px; font-size: 16px; }
.option small, .toggle small { font-size: 13px; line-height: 1.4; color: var(--ink-2); }
.toggle { border-style: dashed; }

textarea, input[type="password"], .person-edit input, .person-edit select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  font-size: 16px;
  line-height: 1.45;
}
textarea { resize: vertical; }

.row { display: flex; gap: 8px; }

.person-edit {
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr) minmax(0, 132px) 40px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.person-edit .swatch { width: 10px; height: 10px; border-radius: 50%; background: var(--person); }
.person-edit input, .person-edit select { padding: 9px 10px; }
.icon-x {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--ink-2);
  font-size: 20px;
  line-height: 1;
}
.icon-x:disabled { opacity: 0.35; }
#addPerson, #logout { width: 100%; }

.btn {
  flex: none;
  padding: 11px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 15px;
  font-weight: 600;
}
.btn.danger { width: 100%; color: var(--rec); background: transparent; }
.btn.primary { border-color: transparent; background: var(--accent); color: var(--accent-ink); }
.btn:disabled { opacity: 0.45; }

.export-summary { margin: 4px 0 14px; font-size: 16px; font-weight: 600; }
.export-actions { display: grid; gap: 8px; margin-bottom: 14px; }
.export-actions .btn { width: 100%; padding: 13px 16px; font-size: 16px; }

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-3);
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(118px + env(safe-area-inset-bottom));
  max-width: min(92vw, 520px);
  padding: 10px 16px;
  border-radius: 14px;
  background: var(--ink);
  color: var(--bg);
  font-size: 14px;
  line-height: 1.4;
  text-align: center;
  transform: translate(-50%, 12px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

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