:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --text: #1a1a1a;
  --muted: #6b7280;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --accent-soft-2: #e0ebff;
  /* Pinyin font size as a ratio of the surrounding character — controlled
     by the A+/A- buttons. Capped via JS to [0.30, 0.70]. */
  --pinyin-ratio: 0.42;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 20px 60px;
}

.topnav { margin-bottom: 16px; }
.back-link {
  display: inline-block;
  font-size: 14px;
  color: var(--muted);
  text-decoration: none;
}
.back-link:hover { color: var(--accent); }

.page-header { margin-bottom: 24px; }
.page-header h1 {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 4px;
  letter-spacing: -0.01em;
  cursor: default;
  /* Hover area = text only, not the full page width */
  width: fit-content;
  max-width: 100%;
}
.subtitle {
  color: var(--muted);
  font-size: 14px;
  margin: 0 0 14px;
  line-height: 1.6;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover { background: #f3f4f6; border-color: #9ca3af; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-secondary span[aria-hidden] { color: var(--accent); font-size: 14px; }

/* Side-by-side panels on wide screens, stacked on narrow */
.panels {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
@media (min-width: 900px) {
  .panels { flex-direction: row; }
  .panels > .panel {
    flex: 1 1 0;
    min-width: 0;
    margin-bottom: 0;
  }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
  margin-bottom: 16px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.field-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}
.panel-header .field-label { margin-bottom: 0; }
.header-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

/* Textarea + overlay — overlay shows hoverable word spans on top of the
   editable textarea. Text in overlay is invisible (color: transparent) and
   only word spans receive pointer events; everywhere else lets the textarea
   receive clicks for editing. */
.textarea-wrap {
  position: relative;
}
.textarea-wrap textarea,
.textarea-wrap .input-overlay {
  width: 100%;
  min-height: 110px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 16px;
  line-height: 1.55;
  font-weight: 400;
  resize: none;
  background: var(--bg);
  color: var(--text);
  white-space: pre-wrap;
  word-break: normal;
  overflow-wrap: break-word;
  margin: 0;
}
.textarea-wrap textarea { display: block; resize: none; overflow: hidden; }
.textarea-wrap textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.textarea-wrap .input-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  color: transparent;
  background: transparent;
  border-color: transparent;
  overflow: hidden;
}
.textarea-wrap .input-overlay .word {
  pointer-events: auto;
  border-radius: 2px;
}
@media (min-width: 900px) {
  /* Larger, more readable text on laptop screens */
  .textarea-wrap textarea,
  .textarea-wrap .input-overlay { font-size: 18px; min-height: 160px; }
  .output-box { font-size: 26px; }
}

.status-line { min-height: 18px; margin-top: 8px; }
.status { font-size: 12px; color: var(--muted); }

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.toggle input { margin: 0; }

/* Pinyin A+ / A− controls */
.font-controls {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}
.font-controls[hidden] { display: none; }
.font-btn {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 4px;
  font: inherit;
  font-size: 11px;
  color: var(--muted);
  padding: 2px 6px;
  line-height: 1.2;
  cursor: pointer;
}
.font-btn:hover { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.font-btn:disabled { opacity: 0.4; cursor: default; }
.font-btn:disabled:hover { background: var(--panel); color: var(--muted); border-color: var(--border); }

.output-box {
  min-height: 60px;
  padding: 18px 16px;
  background: var(--accent-soft);
  border: 1px solid #dbeafe;
  border-radius: 8px;
  font-size: 22px;
  line-height: 1.85;
  word-break: normal;
  overflow-wrap: break-word;
  white-space: pre-wrap;   /* preserve newlines */
}
.output-box.empty {
  color: var(--muted);
  font-size: 16px;
  background: var(--bg);
  border-style: dashed;
}
@media (min-width: 900px) {
  .output-box.empty { font-size: 18px; }
}
.output-box.with-pinyin { line-height: 2.6; padding-top: 24px; }

/* Each transliterated word stays together (no mid-word linebreaks).
   inline (NOT inline-block) so border-bottom hugs the text baseline. */
.word { white-space: nowrap; border-radius: 2px; }
.g2p {
  border-bottom: 1px dashed var(--muted);
  cursor: help;
}

/* Cross-panel word highlight: translucent so the textarea text underneath
   the input overlay stays readable (highlight is a tint, not a cover) */
.word.word-link-hover { background: rgba(37, 99, 235, 0.18); }

ruby { ruby-position: over; }
rt {
  /* font-size scales off the surrounding character, controlled by --pinyin-ratio */
  font-size: calc(var(--pinyin-ratio) * 1em);
  font-weight: normal;
  color: var(--muted);
  font-family: inherit;
  line-height: 1.2;
  letter-spacing: 0;
}
/* Hide ruby annotations when pinyin toggle is off. The ruby tags are always
   rendered (so titles/tooltips work), but rt is hidden. */
.output-box:not(.with-pinyin) rt,
.translit-table:not(.with-pinyin) rt {
  display: none;
}

details { margin-top: 14px; font-size: 13px; color: var(--muted); }
summary { cursor: pointer; user-select: none; }
.ipa-line {
  font-family: inherit;
  font-size: 14px;
  color: var(--muted);
  margin-top: 8px;
  word-break: normal;
  overflow-wrap: break-word;
  line-height: 1.6;
  white-space: pre-wrap;   /* preserve newlines */
}

/* Floating bubble shown on mobile when a Chinese word is tapped */
.word-bubble {
  position: absolute;
  z-index: 10000;
  background: #1f2937;
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  box-shadow: 0 6px 20px rgba(0,0,0,0.18);
  pointer-events: none;
  max-width: 80vw;
  transform: translate(-50%, -100%);
  margin-top: -6px;
}
.word-bubble[hidden] { display: none; }
.word-bubble .bubble-en { font-weight: 600; }
.word-bubble .bubble-ipa { color: #cbd5e1; font-size: 13px; margin-top: 2px; }
.word-bubble::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  transform: translateX(-50%);
  width: 0; height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #1f2937;
}

/* ============== Modal ============== */
#table-modal {
  border: none;
  border-radius: 12px;
  padding: 0;
  max-width: 95vw;
  width: 1100px;
  max-height: 92vh;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
  overflow: hidden;
}
#table-modal::backdrop { background: rgba(15, 23, 42, 0.55); }

.modal-header {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex-wrap: wrap;
}
.modal-header h2 { margin: 0; font-size: 17px; font-weight: 600; }
.modal-source-link {
  font-size: 13px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}
.modal-source-link:hover { text-decoration: underline; }
.modal-header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 16px;
}
#close-modal {
  background: none;
  border: none;
  font-size: 26px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
  padding: 0 4px;
  border-radius: 4px;
}
#close-modal:hover { color: var(--text); background: #f3f4f6; }

.modal-body {
  padding: 0 0 16px;
  overflow: auto;
  max-height: calc(92vh - 110px);
}

/* ============== Transcription table ============== */
table.translit-table {
  border-collapse: separate;
  border-spacing: 0;
  font-size: 14px;
  width: max-content;
  min-width: 100%;
}
table.translit-table th,
table.translit-table td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 4px 6px;
  text-align: center;
  vertical-align: middle;
}
table.translit-table thead th { border-top: 1px solid var(--border); }
table.translit-table th:first-child,
table.translit-table td:first-child { border-left: 1px solid var(--border); }

/* Column headers (top, sticky) */
table.translit-table thead th {
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 8px 6px;
}
/* Row headers (left, sticky) — 80px, labels can wrap */
table.translit-table tbody th {
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  text-align: right;
  padding: 4px 8px;
  position: sticky;
  left: 0;
  z-index: 1;
  width: 80px;
  min-width: 80px;
  max-width: 80px;
  white-space: normal;
  line-height: 1.25;
}
table.translit-table thead th:first-child {
  left: 0;
  z-index: 3;
  width: 80px;
  min-width: 80px;
  max-width: 80px;
}

table.translit-table td {
  font-size: 18px;
  line-height: 1.45;
  color: var(--text);
  white-space: nowrap;
  background: var(--panel);
}
table.translit-table.with-pinyin td {
  line-height: 2.1;
  padding-top: 10px;
  font-size: 17px;
}

table.translit-table td.empty {
  background: #e6e7eb;
  color: transparent;
  cursor: default;
}
table.translit-table td.empty::before { content: none; }
table.translit-table td.empty:has(+ td.empty) {
  border-right-color: transparent;
}
table.translit-table td.empty.no-border-bottom { border-bottom-color: transparent; }
table.translit-table td.empty.no-border-right  { border-right-color: transparent; }

/* Hover crosshair */
table.translit-table tbody tr.row-hover > td:not(.empty),
table.translit-table td.col-hover:not(.empty) {
  background: #eaf2ff;
}
table.translit-table tbody tr.row-hover > th,
table.translit-table thead th.col-hover {
  background: var(--accent-soft-2);
}
table.translit-table td.cell-hover {
  background: #bfdbfe;
}

/* Alt characters in table cells */
table.translit-table .alt {
  display: inline-block;
  margin-left: 4px;
  color: var(--muted);
  cursor: help;
}
table.translit-table .alt::before { content: "("; }
table.translit-table .alt::after  { content: ")"; }
table.translit-table.with-pinyin .alt { font-size: 0.9em; }
