/* Semantic colors mirror the Android app (values/colors.xml + values-night). */
:root {
  --background: #ffffff;
  --surface: #ffffff;
  --status-bar: #f5f5f5;
  --text-primary: #000000;
  --text-muted: #757575;
  --accent: #018786;
  --red: #ff0000;
  --button-bg: #eeeef0;
  --button-bg-active: #e1e1e4;
  --button-border: #d8d8d8;
  --button-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  --divider-line: rgba(0, 0, 0, 0.12);
  --scrim: rgba(0, 0, 0, 0.45);
}

/* Dark palette: applied when the system prefers dark AND the user hasn't forced light. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --background: #121212;
    --surface: #1e1e1e;
    --status-bar: #1e1e1e;
    --text-primary: #ececec;
    --text-muted: #9e9e9e;
    --accent: #03dac5;
    --button-bg: #1f1f22;
    --button-bg-active: #2c2c30;
    --button-border: #3a3a3a;
    --button-shadow: none;
    --divider-line: rgba(255, 255, 255, 0.12);
    --scrim: rgba(0, 0, 0, 0.6);
  }
}

/* Explicit overrides chosen in Settings. */
:root[data-theme="light"] {
  --background: #ffffff;
  --surface: #ffffff;
  --status-bar: #f5f5f5;
  --text-primary: #000000;
  --text-muted: #757575;
  --accent: #018786;
  --button-bg: #eeeef0;
  --button-bg-active: #e1e1e4;
  --button-border: #d8d8d8;
  --button-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  --divider-line: rgba(0, 0, 0, 0.12);
  --scrim: rgba(0, 0, 0, 0.45);
}
:root[data-theme="dark"] {
  --background: #121212;
  --surface: #1e1e1e;
  --status-bar: #1e1e1e;
  --text-primary: #ececec;
  --text-muted: #9e9e9e;
  --accent: #03dac5;
  --button-bg: #1a1a1c;
  --button-bg-active: #26262a;
  --button-border: #3a3a3a;
  --button-shadow: none;
  --divider-line: rgba(255, 255, 255, 0.12);
  --scrim: rgba(0, 0, 0, 0.6);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--background);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

/* Pin the shell to the visual viewport. A height of 100dvh leaves the button row
   floating above empty space in Firefox, whose dvh can resolve shorter than the
   body (sized 100%), exposing background below the bar. `position: fixed; inset: 0`
   ties the shell to the viewport itself, so the row sits on the bottom edge in
   every browser. */
#app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Transcript: grows to fill, scrolls, pins newest content to the bottom. */
#transcript {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  margin: 6px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  scrollbar-width: none;
}
#transcript::-webkit-scrollbar {
  display: none;
}
/* Pins content to the bottom (like the Android gravity="bottom") while staying
   scrollable once it overflows. */
#transcriptInner {
  margin-top: auto;
  font-size: var(--transcript-size, 14px);
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  -webkit-user-select: text;
  user-select: text;
}

.line {
  color: var(--text-primary);
}
.line.muted {
  color: var(--text-muted);
}
.line.accent {
  color: var(--accent);
}
.line.error {
  color: var(--red);
}

/* Session marker: a centered rule carrying the start time and the live
   record→translate language pair. The newest one tracks the current selection;
   older ones stay frozen as a log of the conversation. Two equal `1fr` side
   columns hold the flanking rule lines, the time + language pair sit in the auto
   columns between them. Grid (not flex) for the tracks: a flex row sized the two
   empty pseudo-lines from their own content and, in Firefox, handed all the slack
   to the left one — collapsing the right line so the marker looked left-aligned.
   Equal grid columns are symmetric in every engine. */
.session-divider {
  display: grid;
  grid-template-columns: 1fr auto auto 1fr;
  align-items: center;
  gap: 12px;
  margin: 22px 2px 14px;
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: var(--text-muted);
  -webkit-user-select: none;
  user-select: none;
}
.session-divider::before,
.session-divider::after {
  content: "";
  height: 1px;
  background: var(--divider-line);
}
.session-divider .time {
  letter-spacing: 0.06em;
  font-variant-numeric: tabular-nums;
}
.session-divider .langs {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 600;
  letter-spacing: 0.1em;
}
.session-divider .arrow {
  font-weight: 400;
  opacity: 0.65;
}

/* Bottom button row, mirroring activity_main.xml weights. */
/* #app already insets for the bottom safe area, so the row only needs a small
   fixed margin — adding the inset here too would double it and waste space. */
#buttons {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 4px 8px 6px;
  flex: 0 0 auto;
}

#buttons button {
  flex: 1 1 0;
  height: 34px;
  min-width: 0;
  border: none;
  border-radius: 6px;
  background: var(--button-bg);
  box-shadow: var(--button-shadow);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 400;
  letter-spacing: 0.02em;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, background 0.12s ease;
}
#buttons button.wide {
  flex-grow: 1.4;
  font-weight: 500;
  letter-spacing: 0.06em;
}
#buttons button:active {
  background: var(--button-bg-active);
  transform: scale(0.97);
}
/* Monochrome icons, matching the native app's button bar. Inline SVGs draw the
   record dot / stop square / play triangle at an exact size, immune to the font
   substitution and emoji presentation that made the old glyphs unpredictable. */
#buttons button .icon {
  width: 13px;
  height: 13px;
  fill: currentColor;
}
#translateButton {
  font-size: 15px;
}

.hidden {
  display: none !important;
}

/* Overlays (settings + API key). */
.overlay {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#settingsSheet,
#apiKeySheet {
  background: var(--surface);
  color: var(--text-primary);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 240px;
  max-width: 88vw;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
}

.settings-group {
  display: flex;
  flex-direction: column;
}

.settings-item {
  font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  font-size: 15px;
  padding: 12px 0;
  cursor: pointer;
  color: var(--text-primary);
  white-space: pre;
}
.settings-item.selected {
  color: var(--accent);
}

.sheet-title {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 14px;
}

#apiKeyInput {
  width: 100%;
  padding: 10px 12px;
  font-size: 15px;
  border: 1px solid var(--button-border);
  border-radius: 8px;
  background: var(--background);
  color: var(--text-primary);
}

.sheet-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 16px;
}
.sheet-actions .spacer {
  flex: 1;
}
.sheet-actions a {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 14px;
}
.text-button {
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 15px;
  padding: 6px 4px;
  cursor: pointer;
}
.text-button.accent {
  color: var(--accent);
  font-weight: 600;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  padding: 10px 16px;
  border-radius: 20px;
  font-size: 14px;
  z-index: 20;
  max-width: 80vw;
  text-align: center;
}

/* ===========================================================================
   Selecting, editing and clearing lines (issues #43, #52, #53, #62).

   The transcript stopped being a wall of text you could only add to. A tap
   selects a line; the bar above the buttons then offers the three things the
   issue asks for — speak it, edit it, delete it — and tapping the selected line
   again opens it for editing in place.
   =========================================================================== */

.line.selected {
  background: var(--button-bg-active);
  box-shadow: inset 2px 0 0 var(--accent);
  border-radius: 4px;
  padding-left: 8px;
  margin-left: -8px;
}

/* The editor replaces the line's text in place: same size, same font, same
   wrapping, so the line does not jump when it becomes editable. */
.line-editor {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  border: none;
  outline: none;
  resize: none;
  overflow: hidden;
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  line-height: inherit;
  white-space: pre-wrap;
  word-break: break-word;
  caret-color: var(--accent);
  -webkit-user-select: text;
  user-select: text;
}

/* Actions for the selected line. Sits directly above the main button row. */
#selectionBar {
  display: flex;
  align-items: stretch;
  gap: 6px;
  padding: 0 8px 4px;
  flex: 0 0 auto;
}
#selectionBar button {
  flex: 1 1 0;
  height: 30px;
  border: none;
  border-radius: 6px;
  background: var(--button-bg);
  box-shadow: var(--button-shadow);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.06s ease, background 0.12s ease;
}
#selectionBar button:active {
  background: var(--button-bg-active);
  transform: scale(0.97);
}
#selectionBar button .icon {
  width: 14px;
  height: 14px;
  fill: currentColor;
}
#selectionBar button.danger,
.text-button.danger {
  color: var(--red);
}

/* The speak button is also the stop button (issue #62): one control, showing the
   thing it will do next. Only one of its two glyphs is ever drawn. */
#speakAloudButton .stop,
#speakAloudButton.speaking .play {
  display: none;
}
#speakAloudButton.speaking .stop {
  display: block;
}
#speakAloudButton.speaking {
  color: var(--red);
}

/* A button with nothing to act on says so, rather than failing silently. */
#buttons button:disabled {
  opacity: 0.35;
  cursor: default;
}
#buttons button:disabled:active {
  transform: none;
  background: var(--button-bg);
}

/* The language pair reads as a pair: two codes with the swap between them. */
#buttons button.lang {
  font-weight: 500;
  letter-spacing: 0.06em;
}
#swapButton {
  flex-grow: 0.6;
}

.settings-separator {
  height: 1px;
  background: var(--divider-line);
  margin: 8px 0;
}
