:root {
  color-scheme: light;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", Tahoma, sans-serif;
  background: #f7f6f2;
  color: #1d2624;
  letter-spacing: 0;
  --ink: #1d2624;
  --muted: #6d7773;
  --line: #d9ddd8;
  --paper: #f7f6f2;
  --green: #176b55;
  --yellow: #e3ad37;
  --red: #bd5546;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
button, input { font: inherit; letter-spacing: 0; }
button { -webkit-tap-highlight-color: transparent; }
body { min-width: 320px; background: var(--paper); }

.app-shell {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.topbar {
  height: 58px;
  display: grid;
  grid-template-columns: 72px 1fr 72px;
  align-items: center;
  border-bottom: 1px solid var(--line);
  padding: 0 8px;
}

.brand { text-align: center; font-size: 21px; font-weight: 750; color: var(--green); }
.icon-button, .close-button {
  min-height: 44px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 650;
}
.icon-button:first-child { text-align: left; }
.icon-button:last-child { text-align: right; }

.practice {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(180px, 1fr) auto auto;
  align-items: center;
  width: min(100%, 720px);
  margin: 0 auto;
  padding: 14px 20px 20px;
}
.practice.answer-shown { grid-template-rows: auto minmax(110px, 0.55fr) auto auto; }
.practice.answer-shown .question-stage { min-height: 120px; padding-top: 8px; padding-bottom: 8px; }
.practice.answer-shown .thai-word { font-size: 42px; }

.mode-label {
  justify-self: center;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.question-stage {
  width: 100%;
  min-height: 210px;
  border: 0;
  background: transparent;
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  padding: 20px 4px;
}

.thai-word {
  max-width: 100%;
  overflow-wrap: anywhere;
  font-family: Tahoma, "Noto Sans Thai", sans-serif;
  font-size: 58px;
  line-height: 1.4;
  font-weight: 650;
}
.reveal-hint { color: var(--muted); font-size: 14px; }

.answer { width: 100%; text-align: center; border-top: 1px solid var(--line); padding-top: 20px; }
.phonemic { font-family: Tahoma, "Noto Sans Thai", sans-serif; font-size: 27px; line-height: 1.5; }
.romanization { color: var(--green); font-size: 22px; font-weight: 720; margin-top: 6px; }
.meaning { font-size: 19px; margin-top: 12px; }
.answer-actions { display: flex; justify-content: center; gap: 10px; margin-top: 18px; }
.command-button {
  min-height: 44px;
  border: 1px solid var(--green);
  background: var(--green);
  color: white;
  padding: 0 18px;
  border-radius: 6px;
  font-weight: 700;
}
.command-button.secondary { background: transparent; color: var(--green); }
.details { margin-top: 16px; color: var(--muted); font-size: 14px; }
.details div { display: flex; justify-content: center; gap: 12px; }
.details strong { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }
.details a { color: var(--green); display: inline-block; margin-top: 10px; }
.status { min-height: 22px; color: var(--red); font-size: 14px; text-align: center; }

.rating-bar {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 10px max(12px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: rgba(247, 246, 242, 0.96);
}
.rating-button {
  min-width: 0;
  min-height: 58px;
  border-radius: 6px;
  border: 1px solid currentColor;
  background: transparent;
  padding: 7px 5px;
  font-size: 14px;
  font-weight: 750;
  line-height: 1.25;
}
.rating-button.missed { color: var(--red); }
.rating-button.sound { color: #85620e; }
.rating-button.tone { color: var(--green); background: #e6f1ed; }
.rating-button:disabled { opacity: 0.45; }

.sheet {
  width: min(100% - 24px, 520px);
  max-height: calc(100dvh - 24px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--paper);
  color: var(--ink);
}
.sheet::backdrop { background: rgba(20, 29, 27, 0.45); }
.sheet form { padding: 20px; }
.sheet-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--line); padding-bottom: 12px; }
.sheet h2 { margin: 0; font-size: 22px; }
.sheet h3 { font-size: 16px; margin: 26px 0 10px; }
fieldset { border: 0; padding: 0; margin: 24px 0; }
legend { font-size: 14px; font-weight: 750; margin-bottom: 9px; }
.segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.segmented button { min-height: 46px; border: 0; border-right: 1px solid var(--line); background: transparent; padding: 5px; font-size: 14px; }
.segmented button:last-child { border-right: 0; }
.segmented button.active { background: var(--green); color: white; font-weight: 750; }
#romanizationControl { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; background: var(--line); margin-top: 20px; }
.stats-grid div { background: var(--paper); text-align: center; padding: 18px 4px; }
.stats-grid strong { display: block; font-size: 28px; color: var(--green); }
.stats-grid span { font-size: 12px; color: var(--muted); }
.weak-features { display: grid; gap: 8px; }
.weak-feature { display: flex; justify-content: space-between; border-bottom: 1px solid var(--line); padding: 9px 0; }
.weak-feature span:last-child { color: var(--red); font-weight: 700; }

@media (max-width: 390px) {
  .thai-word { font-size: 48px; }
  .practice.answer-shown .thai-word { font-size: 38px; }
  .practice { padding-left: 14px; padding-right: 14px; }
  .rating-button { font-size: 12px; }
}

@media (min-width: 700px) {
  .thai-word { font-size: 68px; }
  .rating-bar { width: min(100%, 720px); margin: 0 auto; }
}

@media (prefers-reduced-motion: no-preference) {
  .answer:not([hidden]) { animation: reveal 160ms ease-out; }
  @keyframes reveal { from { opacity: 0; transform: translateY(5px); } }
}
