:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --line: #dde2e8;
  --accent: #1f4e79;
  --accent-text: #ffffff;
  --good: #1a7f37;
  --bad: #b42318;
  --warn-bg: #fff4e5;
  --user: #e8f0fa;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --card: #1a1f25;
    --text: #e6e9ed;
    --muted: #9aa5b1;
    --line: #2c333b;
    --accent: #6ea8dc;
    --accent-text: #0b1520;
    --good: #4cc270;
    --bad: #ff7b72;
    --warn-bg: #2b2213;
    --user: #1d2a38;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", "Sukhumvit Set", "Noto Sans Thai", sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--accent); }
header {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; text-decoration: none; font-size: 18px; }
nav { display: flex; gap: 4px; flex-wrap: wrap; flex: 1; }
nav a {
  padding: 6px 10px; border-radius: 8px; text-decoration: none; color: var(--text);
}
nav a.active { background: var(--accent); color: var(--accent-text); }
.help-link {
  background: none; border: 1px solid var(--line); color: var(--bad);
  border-radius: 8px; padding: 6px 10px; font: inherit; cursor: pointer;
}
main { max-width: 900px; margin: 0 auto; padding: 16px; }
h1 { font-size: 22px; margin: 4px 0 12px; }
h2 { font-size: 18px; margin: 16px 0 8px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin: 0 0 12px;
}
.muted { color: var(--muted); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; min-width: 160px; }
label { display: block; font-size: 14px; color: var(--muted); margin: 8px 0 4px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px;
}
textarea { min-height: 90px; resize: vertical; }
button, .btn {
  font: inherit; border: 0; border-radius: 8px; padding: 10px 14px; cursor: pointer;
  background: var(--accent); color: var(--accent-text); text-decoration: none; display: inline-block;
}
button.secondary, .btn.secondary { background: transparent; color: var(--accent); border: 1px solid var(--line); }
button.danger { background: transparent; color: var(--bad); border: 1px solid var(--line); }
button:disabled { opacity: .5; cursor: default; }
.banner { background: var(--warn-bg); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; }
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.pill { font-size: 12px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.error { color: var(--bad); }
.good { color: var(--good); }
.bad { color: var(--bad); }

/* chat */
.chat { display: flex; flex-direction: column; gap: 10px; margin-bottom: 140px; }
.msg { border-radius: var(--radius); padding: 10px 14px; max-width: 92%; overflow-x: auto; }
.msg.user { align-self: flex-end; background: var(--user); white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--line); }
.msg p:first-child { margin-top: 0; }
.msg p:last-child { margin-bottom: 0; }
.composer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 4;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
}
.composer .inner { max-width: 900px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { min-height: 48px; max-height: 40vh; }
.rec { background: var(--bad); }
pre { overflow-x: auto; background: var(--bg); padding: 10px; border-radius: 8px; }
table { border-collapse: collapse; width: 100%; }
td, th { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: left; vertical-align: top; }

/* flashcards */
.flash { min-height: 180px; display: flex; flex-direction: column; justify-content: center; font-size: 18px; }
.rate { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.rate button:nth-child(1) { background: var(--bad); }
.rate button:nth-child(4) { background: var(--good); }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--text); max-width: 420px; }
dialog::backdrop { background: rgba(0, 0, 0, .4); }
