/* Thai font, self-hosted so the page loads in mainland China (Google Fonts is blocked there).
   Subset to Thai + basic Latin + ◌ (U+25CC); see fonts/OFL.txt for the license. */
@font-face {
  font-family: 'Noto Sans Thai Looped';
  src: url('fonts/NotoSansThaiLooped-Regular.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Noto Sans Thai Looped';
  src: url('fonts/NotoSansThaiLooped-SemiBold.woff2') format('woff2');
  font-weight: 600;
  font-display: swap;
}

:root {
  --bg: #faf7f2;
  --surface: #ffffff;
  --surface-2: #f3efe7;
  --text: #1f2328;
  --muted: #5f6670;
  --border: #e4ddd0;
  --accent: #b4532a;
  --accent-contrast: #ffffff;
  --accent-2: #3e5a73;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);

  /* Colour only marks a letter's role, in five families: every kind of 前引字 shares one colour,
     every consonant that starts a syllable shares another. Tones are shown by their contour. */
  --role-lead-voiced: #b0287f;
  --role-lead: var(--role-lead-voiced);
  --role-lead-pseudo: var(--role-lead-voiced);
  --role-initial: #1f5fd1;
  --role-led: var(--role-initial);
  --role-cluster: var(--role-initial);
  --role-vowel: #c2253a;
  --role-tone: #d4610b;
  --role-final: #1d8a43;
  --role-silent: #9aa0a6;

  --tone-mid: var(--text);
  --tone-low: var(--text);
  --tone-falling: var(--text);
  --tone-high: var(--text);
  --tone-rising: var(--text);

  --font-thai: 'Noto Sans Thai Looped', 'Sarabun', 'Thonburi', 'Tahoma', sans-serif;
  --font-ui: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Noto Sans CJK SC', 'Noto Sans SC', system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --surface: #1e2127;
    --surface-2: #262a31;
    --text: #e8e6e3;
    --muted: #a0a6ae;
    --border: #343941;
    --accent: #e0855a;
    --accent-contrast: #16181c;
    --accent-2: #8fb3d1;
    --shadow: none;

    --role-lead-voiced: #f07cc8;
    --role-initial: #7aa7ff;
    --role-vowel: #ff7a8a;
    --role-tone: #ffad5c;
    --role-final: #5fd38a;
    --role-silent: #6f757d;

  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  line-height: 1.6;
}

main, .site-header, .site-footer {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 16px;
}

.site-header { padding-top: 20px; padding-bottom: 4px; display: flex; align-items: baseline; gap: 4px 14px; flex-wrap: wrap; }
.site-header h1 { margin: 0; font-size: 1.5rem; letter-spacing: 0.02em; }
.tagline { margin: 0; color: var(--muted); font-size: 0.9rem; }
/* Once a word is analysed, the title gives way so the result starts higher. */
.has-result .site-header { display: none; }
.has-result .search { margin-top: 18px; }

.site-footer {
  color: var(--muted);
  font-size: 0.85rem;
  padding-top: 32px;
  padding-bottom: 40px;
}

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

.thai { font-family: var(--font-thai); }

/* Search */
.search { display: flex; gap: 8px; margin: 14px 0 12px; }
.search input {
  flex: 1;
  min-width: 0;
  font: 1.4rem/1.4 var(--font-thai);
  padding: 6px 14px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
.search input:focus { outline: none; border-color: var(--accent); }
.search button {
  font: 600 1.05rem var(--font-ui);
  padding: 0 22px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-contrast);
  cursor: pointer;
}
.search button:disabled { opacity: 0.6; cursor: progress; }

/* "试试": same shape as 分析, in a second color, after a thin divider. */
.search .divider { width: 1px; align-self: stretch; margin: 6px 2px; background: var(--border); }
.search .try { background: var(--accent-2); padding: 0 14px; white-space: nowrap; }
.search .try .thai { font: 600 1.05rem var(--font-thai); margin-left: 2px; }
.search .try[hidden] { display: none; }

.status { margin: 8px 0; color: var(--muted); }
.status:empty { display: none; }
.status.error {
  color: var(--role-vowel);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--role-vowel);
  border-radius: 8px;
  padding: 10px 14px;
}

/* Result */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 1.15rem; }
.card h3 { margin: 0 0 8px; font-size: 1rem; }

.overview { text-align: center; padding: 12px 20px 14px; margin-bottom: 12px; }
.overview .word { position: relative; display: inline-block; font: 600 3rem/1.45 var(--font-thai); word-break: break-all; margin-top: 2px; }
.seg { cursor: pointer; border-radius: 8px; }
.seg:hover { background: var(--surface-2); }
.seg:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
/* Brackets under each syllable, its romanization under the bracket (see layoutBrackets). */
.brk { position: absolute; height: 7px; border: 2px solid var(--muted); border-top: 0; border-radius: 0 0 4px 4px; pointer-events: none; }
.brk.v { border-style: dashed; border-color: var(--accent-2); }
.brk-label { position: absolute; transform: translateX(-50%); font: 600 1.15rem/1.2 var(--font-ui); white-space: nowrap; pointer-events: none; }
.brk-label.v { font-size: 0.95rem; color: var(--accent-2); }
.brk-note { position: absolute; left: 50%; transform: translateX(-50%); font: 0.75rem var(--font-ui); color: var(--accent-2); white-space: nowrap; }
.brk-note .thai { font-size: 0.85rem; }
.overview .sub { display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; }
.overview .speak-row { display: inline-flex; gap: 6px; }
.overview .meaning { margin: 0; font-size: 1.05rem; line-height: 1.4; }
.meaning .pos { font-size: 0.75rem; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 0 5px; margin-right: 6px; vertical-align: 2px; }
.meaning .sep { color: var(--muted); margin: 0 8px; }
.meaning.none { font-size: 0.85rem; color: var(--muted); }
.overview .speak-hint { margin: 2px 0 0; }

.speak {
  font: 1rem var(--font-ui);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 999px;
  padding: 2px 12px;
  font-size: 0.85rem;
  letter-spacing: 0;
  cursor: pointer;
}
.speak:disabled { cursor: not-allowed; opacity: 0.6; }
.hint { color: var(--muted); font-size: 0.85rem; }

/* Syllable cards */
.syl-cards { display: grid; gap: 8px; margin-bottom: 16px; }
@media (min-width: 860px) {
  .syl-cards { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); align-items: start; }
  /* An opened breakdown needs the full width for its letter cards and steps. */
  .syl-card:has(.full[open]) { grid-column: 1 / -1; }
}
.syl-card { margin: 0; padding: 10px 16px; display: grid; gap: 6px; position: relative; }
.syl-card.flash { animation: flash 1s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 3px var(--accent); } to { box-shadow: var(--shadow); } }
@media (prefers-reduced-motion: reduce) { .syl-card.flash { animation: none; } }
.syl-top { display: flex; align-items: center; gap: 10px; }
.syl-top .t { font: 600 1.5rem/1.25 var(--font-thai); }
.syl-top .r { font-weight: 600; font-size: 1.05rem; }
.syl-top .tone-badge { border: 0; padding: 0; font-weight: 600; }
.syl-top .tone-badge:hover { background: none; text-decoration: underline; }
.virtual { color: var(--muted); font-weight: 400; }
.tokens { display: flex; flex-wrap: wrap; gap: 2px 12px; font-size: 0.8rem; color: var(--muted); }
.tokens b { font-size: 1.1rem; font-weight: 600; margin-right: 3px; }
.formula { display: flex; flex-wrap: wrap; align-items: center; gap: 3px; font-size: 0.8rem; }
.formula .f { border: 1px solid var(--border); background: var(--surface-2); border-radius: 7px; padding: 0 6px; white-space: nowrap; color: var(--text); font: inherit; line-height: 1.5; }
.formula .f small { color: var(--muted); font-size: 0.7rem; margin-left: 3px; }
.formula button.f { cursor: pointer; }
.formula button.f:hover, .formula button.f[aria-expanded="true"] { border-color: var(--text); }
.formula .op { color: var(--muted); }
.formula .res { white-space: nowrap; }
.special { background: var(--surface-2); border-radius: 10px; padding: 5px 10px; font-size: 0.82rem; line-height: 1.5; display: grid; gap: 2px; }
.special p { margin: 0; }
.special b { font-size: 1.05rem; }
.special .class-badge { color: var(--text); font-weight: 600; padding: 0; margin: 0 1px; border: 0; border-bottom: 1px dashed var(--muted); border-radius: 0; font-size: inherit; line-height: inherit; vertical-align: baseline; }
.full summary { position: absolute; top: 12px; right: 14px; cursor: pointer; color: var(--accent-2); font-size: 0.8rem; list-style: none; }
.full summary::-webkit-details-marker { display: none; }
.full summary::after { content: ' ▸'; }
.full[open] summary::after { content: ' ▾'; }
.full[open] { border-top: 1px dashed var(--border); padding-top: 10px; }
.result > .warning { text-align: center; font-size: 0.8rem; margin: -6px 0 16px; }

/* Letter roles */
.r-lead { color: var(--role-lead); }
.r-leadVoiced { color: var(--role-lead-voiced); }
.r-leadPseudo { color: var(--role-lead-pseudo); }
.r-led { color: var(--role-led); }
.r-initial { color: var(--role-initial); }
.r-cluster { color: var(--role-cluster); }
.r-vowel { color: var(--role-vowel); }
.r-tone { color: var(--role-tone); }
.r-final { color: var(--role-final); }
.r-silent { color: var(--role-silent); }
.r-mark { color: var(--muted); }

/* Stacked copies that give combining marks their own color (see letters.js). */
.inked { position: relative; display: inline-block; }
.inked .ink { position: absolute; left: 0; top: 0; white-space: nowrap; pointer-events: none; }
.inked .ghost, .inked .ghost * { color: transparent; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 0.8rem; color: var(--muted); justify-content: center; }
.overview .legend { padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.legend span::before {
  content: '';
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 3px;
  margin-right: 5px;
  background: currentColor;
  vertical-align: -1px;
}

/* Tone badges */
.tone-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 10px 2px 6px;
  border-radius: 999px;
  font-size: 0.85rem;
  border: 1px solid currentColor;
  white-space: nowrap;
  background: none;
  font-family: inherit;
  line-height: inherit;
}
button.tone-badge { cursor: help; }

/* Class chips: same pill as the tone badge; vowel kinds in the part's role color, consonant
   classes inline in the "为什么读" steps */
.class-badge {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 2px 10px;
  border-radius: 999px;
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.3;
  color: inherit;
  border: 1px solid currentColor;
  background: none;
  white-space: nowrap;
  cursor: pointer;
}
.class-badge:hover, .class-badge[aria-expanded="true"] { background: var(--surface-2); }
.class-badge .thai { font-size: 0.95rem; }
.steps .class-badge { color: var(--accent); padding: 0 8px; margin: 0 2px; font-size: 0.9em; vertical-align: 1px; }
button.tone-badge:hover { background: var(--surface-2); }
.tone-mid { color: var(--tone-mid); }
.tone-low { color: var(--tone-low); }
.tone-falling { color: var(--tone-falling); }
.tone-high { color: var(--tone-high); }
.tone-rising { color: var(--tone-rising); }
.contour { display: block; }
.contour path { fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
.contour line { stroke: currentColor; opacity: 0.2; }

/* Syllable breakdown */
.parts { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; align-content: start; }
.why { margin-top: 14px; }
.why h3 { margin: 0; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
@media (min-width: 860px) {
  .syl-body { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 20px; align-items: start; }
  .why { margin-top: 0; }
  .why .steps { margin-top: 8px; }
}
.part {
  border: 1px solid var(--border);
  border-top: 4px solid currentColor;
  border-radius: 10px;
  padding: 10px 12px;
}
.part .label { font-size: 0.8rem; font-weight: 600; }
.part .head { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; }
.part .glyph { font: 600 1.8rem/1.4 var(--font-thai); color: var(--text); }
.part .glyph .ph { color: var(--muted); font-weight: 400; }
.part .sound { font-size: 1rem; color: var(--text); margin-left: 6px; }
.part .meta { font-size: 0.8rem; color: var(--muted); }
.part .explain { font-size: 0.88rem; color: var(--text); margin-top: 4px; }
.part .variant { font-size: 0.88rem; color: var(--text); margin-top: 6px; padding: 6px 8px; border-radius: 6px; background: var(--surface-2); }
.part .variant b { margin-right: 6px; }
.part .variant .thai { font-size: 1.05rem; }
.vowel-variants { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 4px; }
.vowel-variants li { display: flex; align-items: baseline; gap: 6px; padding: 2px 6px; border-radius: 4px; }
.vowel-variants li .thai { font-size: 1.15rem; }
.vowel-variants li small { color: var(--muted); margin-left: auto; }
.vowel-variants li.here { background: var(--surface-2); outline: 1px solid var(--text); }

.steps { counter-reset: step; list-style: none; padding: 0; margin: 12px 0 0; }
.steps li {
  counter-increment: step;
  position: relative;
  padding: 4px 0 4px 30px;
  border-left: 2px solid var(--border);
  margin-left: 12px;
}
.steps li:last-child { border-left-color: transparent; }
.steps li::before {
  content: counter(step);
  position: absolute;
  left: -13px;
  top: 6px;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 0.8rem;
  display: grid; place-items: center;
}
.steps li.conclusion::before { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.steps .k { font-weight: 600; margin-right: 6px; }
.steps li.conclusion { font-weight: 600; }

.notes { margin: 12px 0 0; padding: 10px 14px; border-radius: 10px; background: var(--surface-2); font-size: 0.9rem; }
.notes p { margin: 0; }
.notes p + p { margin-top: 4px; }

/* Tone table */
.table-wrap { overflow-x: auto; }
table.tones { border-collapse: collapse; width: 100%; min-width: 560px; font-size: 0.9rem; }
table.tones th, table.tones td { border: 1px solid var(--border); padding: 8px 6px; text-align: center; }
table.tones thead th { background: var(--surface-2); font-weight: 600; }
table.tones tbody th { text-align: left; white-space: nowrap; background: var(--surface-2); }
table.tones td.na { color: var(--muted); }
table.tones td.hit { outline: 3px solid var(--accent); outline-offset: -3px; font-weight: 600; }
table.tones td .who { display: block; font: 0.85rem var(--font-thai); color: var(--accent); }
table.tones td .who .active { background: var(--accent); color: var(--accent-contrast); border-radius: 4px; padding: 0 4px; }

/* Rules */
.rules details { border-top: 1px solid var(--border); padding: 10px 0; }
.rules details:first-of-type { border-top: none; }
.rules summary { cursor: pointer; font-weight: 600; }
.rules summary .applies { font-weight: 400; color: var(--muted); font-size: 0.85rem; margin-left: 8px; }
.rules p { margin: 6px 0 0; color: var(--text); }

.warning { color: var(--muted); font-size: 0.88rem; }

/* Tone key popover: how to read the romanization marks */
.result { position: relative; }
.tone-pop {
  position: absolute;
  z-index: 10;
  width: min(420px, 100%);
  margin: 0;
  padding: 14px;
  box-shadow: 0 8px 28px rgb(0 0 0 / 0.16);
}
.tone-pop h3 { font-size: 0.9rem; }
.tone-pop ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.tone-pop li {
  display: grid;
  grid-template-columns: 1.4em 34px 7.5em minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid transparent;
}
.tone-pop li.used { border-color: currentColor; }
.tone-pop li.focus { background: var(--surface-2); }
.tone-pop .mark { font-size: 1.2rem; font-weight: 600; text-align: center; }
.tone-pop .name { font-weight: 600; font-size: 0.88rem; white-space: nowrap; }
.tone-pop .ref { color: var(--muted); font-size: 0.8rem; }
.tone-pop .hint { margin: 8px 0 0; }
.tone-pop h3 .thai { font-weight: 400; color: var(--muted); }
.class-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 6px; }
.class-tiles .tile {
  display: flex; flex-direction: column; align-items: center;
  padding: 4px 2px 5px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: center;
  line-height: 1.25;
}
.class-tiles .tile.here { border-color: var(--text); background: var(--surface-2); }
.class-tiles .ch { font-size: 1.5rem; font-weight: 600; }
.class-tiles .snd { font-size: 0.75rem; color: var(--muted); }
.class-tiles .word { font-size: 0.85rem; margin-top: 2px; }
.class-tiles .zh { font-size: 0.7rem; color: var(--muted); }
.vowel-tiles { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
.vowel-tiles .forms { display: flex; gap: 10px; }
.vowel-tiles .ch { font-size: 1.3rem; padding: 0 2px; border-radius: 4px; }
.vowel-tiles .ch.here { text-decoration: underline; text-underline-offset: 4px; }

@media (max-width: 560px) {
  .card { padding: 14px; }
  .tagline { display: none; }
  .overview { padding: 8px 12px 10px; }
  .overview .legend { font-size: 0.75rem; padding-bottom: 4px; }
  .overview .sub { margin-top: 4px; }
  .overview .word { font-size: 2.6rem; }
  .brk-label { font-size: 1.05rem; }
  .syl-card { padding: 8px 12px; gap: 5px; }
  .full summary { top: 10px; right: 12px; }
  .formula { font-size: 0.76rem; gap: 2px; }
  .formula .f { padding: 0 5px; }
  .search { margin-top: 10px; margin-bottom: 8px; }
  .has-result .search { margin-top: 10px; }
  .syl-top .t { font-size: 1.45rem; }
  .search input { font-size: 1.3rem; padding: 6px 10px; }
  .search { gap: 6px; }
  .search button { padding: 0 14px; }
  .search .try { padding: 0 10px; }
}
