:root {
  --bg: #f5f6f3;
  --panel: #ffffff;
  --panel-2: #eef0ec;
  --ink: #18201d;
  --muted: #5b6662;
  --line: #dde2dc;
  --accent: #116a5c;
  --accent-ink: #ffffff;
  --accent-soft: #dcefe9;
  --night: #183a34;
  --night-ink: #eef6f2;
  --ok: #1d7444;
  --ok-soft: #e1f3e8;
  --bad: #b3261e;
  --bad-soft: #fdeceb;
  --warn: #8a5300;
  --warn-soft: #fff3da;
  --info: #2f4f8f;
  --info-soft: #e8eefb;
  --A1: transparent; --A1-sw: #cfe9d6;
  --A2: #e3f4e7; --A2-sw: #9fd6ae;
  --B1: #fbf3c8; --B1-sw: #e9cf55;
  --B2: #fde2c4; --B2-sw: #ef9f4c;
  --C1: #fbd2cf; --C1-sw: #e0645b;
  --C2: #ead5f7; --C2-sw: #a46bd1;
  --off: #dfe4ee; --off-sw: #8592ab;
  --shadow: 0 1px 2px rgba(20, 40, 30, 0.06), 0 8px 24px rgba(20, 40, 30, 0.06);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0f1513;
  --panel: #18201d;
  --panel-2: #202a26;
  --ink: #e8efeb;
  --muted: #9aa8a2;
  --line: #2e3a35;
  --accent: #5fd3bb;
  --accent-ink: #082019;
  --accent-soft: #163a32;
  --night: #1f4a42;
  --night-ink: #eef6f2;
  --ok: #72d69c;
  --ok-soft: #15301f;
  --bad: #ff8a80;
  --bad-soft: #3a1c1b;
  --warn: #ffc76b;
  --warn-soft: #3a2c14;
  --info: #a9bdf5;
  --info-soft: #1d2540;
  --A1: transparent; --A1-sw: #2f5a3c;
  --A2: #1d3324; --A2-sw: #4f9c66;
  --B1: #3b3514; --B1-sw: #c9ad3a;
  --B2: #432b13; --B2-sw: #e08a3a;
  --C1: #4a1f1d; --C1-sw: #e0645b;
  --C2: #3a2350; --C2-sw: #b585e0;
  --off: #26304a; --off-sw: #7f8db0;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 8px 24px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 4.5rem; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow-x: hidden;
}
a { color: var(--accent); }
.hidden { display: none !important; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .86rem; }
h1, h2, h3 { line-height: 1.2; }
h2, .h2 { font-size: 1.12rem; margin: 0 0 .7rem; font-weight: 700; }
h3 { font-size: .98rem; margin: 1rem 0 .5rem; }
ul.plain { list-style: none; padding: 0; margin: 0; }
ul.plain li { padding: .2rem 0; }

button { font: inherit; cursor: pointer; border-radius: 10px; border: 1px solid transparent; padding: .55rem 1rem; color: var(--ink); }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); font-weight: 650; }
button.primary:hover:not(:disabled) { filter: brightness(1.08); }
button.ghost, .ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); border-radius: 10px; }
button.ghost:hover:not(:disabled), label.ghost:hover { border-color: var(--accent); color: var(--accent); }
button.small, .ghost.small { padding: .3rem .65rem; font-size: .85rem; }
button.icon { padding: .35rem .6rem; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---- Header ---------------------------------------------------------------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: .6rem; flex-wrap: wrap;
  padding: .65rem 16px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.backlink { text-decoration: none; color: var(--muted); font-size: .9rem; white-space: nowrap; }
.sep { color: var(--line); }
.appname { white-space: nowrap; }
.logo { display: inline-flex; align-items: center; justify-content: center; width: 1.6rem; height: 1.6rem; border-radius: 7px;
  background: var(--night); color: #9fe6d3; font-family: var(--serif); font-size: .85rem; margin-right: .2rem; }
.topactions { display: flex; align-items: center; gap: .45rem; flex-wrap: wrap; }
.credits { font-size: .88rem; color: var(--muted); }
.topup { font-size: .88rem; font-weight: 600; text-decoration: none; }

/* ---- Hero and examples ----------------------------------------------------- */
main { max-width: 1080px; margin: 0 auto; padding: 0 16px 3rem; }
.hero { padding: 2rem 0 .4rem; }
.hero h1 { font-family: var(--serif); font-size: clamp(1.8rem, 5vw, 2.7rem); margin: 0 0 .5rem; letter-spacing: -.01em; font-weight: 700; }
.lede { font-size: 1.07rem; color: var(--muted); max-width: 44rem; margin: 0 0 .7rem; }
.pitch { margin: 0; font-size: .94rem; }
.pill { display: inline-block; background: var(--night); color: var(--night-ink); border-radius: 999px; padding: .12rem .6rem; font-size: .8rem;
  font-weight: 650; margin-right: .35rem; }
.examples { display: flex; flex-wrap: wrap; gap: .45rem; align-items: center; margin: 1rem 0 1.1rem; }
.exlabel { font-size: .88rem; color: var(--muted); margin-right: .2rem; }
.chip { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: .35rem .8rem; font-size: .88rem; }
.chip:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }

/* ---- Cards ------------------------------------------------------------------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; margin-bottom: 1rem; min-width: 0; }
.cardhead { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: .6rem; }
.cardhead h2 { margin: 0; }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1rem; }
@media (max-width: 820px) { .grid2 { grid-template-columns: minmax(0, 1fr); } }
.grid2 .card { margin-bottom: 0; }

/* ---- Editor -------------------------------------------------------------------- */
.edhead { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: .5rem; }
.edhead .h2 { margin: 0; }
.edtools { display: flex; gap: .4rem; }
.filebtn { cursor: pointer; display: inline-block; }
textarea, input, select {
  font: inherit; color: var(--ink); background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: .55rem .7rem; width: 100%;
}
#text { font-family: var(--serif); font-size: 1.05rem; line-height: 1.6; resize: vertical; min-height: 12rem; }
#text.drop { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea:focus, input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#count { margin: .4rem 0 0; }

/* ---- Report ---------------------------------------------------------------- */
.tiles, .beforeafter { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .7rem; margin-bottom: 1rem; }
.beforeafter { grid-template-columns: repeat(2, minmax(0, 1fr)); margin: .8rem 0; }
@media (max-width: 720px) { .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 420px) { .beforeafter { grid-template-columns: minmax(0, 1fr); } }
.tile, .ba { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: .75rem .85rem; display: flex; flex-direction: column; gap: .1rem; min-width: 0; }
.ba { background: var(--panel-2); }
.tlabel { font-size: .74rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 700; }
.tval { font-size: 1.6rem; font-family: var(--serif); line-height: 1.15; }
.ba .tval { font-size: 1.25rem; }
.tsub { font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex-wrap: wrap; }
.segbtn { border-radius: 0; background: transparent; padding: .3rem .75rem; font-size: .85rem; border: 0; }
.segbtn + .segbtn { border-left: 1px solid var(--line); }
.segbtn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.seg.tabs { margin: .4rem 0 .6rem; }

.profile { margin-bottom: .8rem; }
.pbar { display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: var(--panel-2); }
.pseg { display: block; height: 100%; }
.pseg.lvA1, .sw.lvA1 { background: var(--A1-sw); }
.pseg.lvA2, .sw.lvA2 { background: var(--A2-sw); }
.pseg.lvB1, .sw.lvB1 { background: var(--B1-sw); }
.pseg.lvB2, .sw.lvB2 { background: var(--B2-sw); }
.pseg.lvC1, .sw.lvC1 { background: var(--C1-sw); }
.pseg.lvC2, .sw.lvC2 { background: var(--C2-sw); }
.pseg.lvoff, .sw.lvoff { background: var(--off-sw); }
.plegend { display: flex; flex-wrap: wrap; gap: .3rem .9rem; margin-top: .45rem; font-size: .8rem; color: var(--muted); }
.pkey { display: inline-flex; align-items: center; gap: .3rem; }
.sw { display: inline-block; width: .8rem; height: .8rem; border-radius: 3px; }

.marked { font-family: var(--serif); font-size: 1.05rem; line-height: 1.85; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--panel-2); border-radius: 12px; padding: .9rem 1rem; max-height: 28rem; overflow-y: auto; }
.tk { border-radius: 3px; padding: 0 1px; }
.tk.lvA1 { background: var(--A1); }
.tk.lvA2 { background: var(--A2); }
.tk.lvB1 { background: var(--B1); }
.tk.lvB2 { background: var(--B2); }
.tk.lvC1 { background: var(--C1); }
.tk.lvC2 { background: var(--C2); }
.tk.lvoff { background: var(--off); }
.tk.name, .tk.num { text-decoration: underline dotted var(--muted); text-underline-offset: 3px; }
.marked.overmode .tk.over { box-shadow: inset 0 -2px 0 var(--bad); }

table.scores, table.chtable { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.scores th, table.scores td, table.chtable th, table.chtable td { text-align: left; padding: .4rem .45rem; border-bottom: 1px solid var(--line); vertical-align: top; }
table.scores thead th, table.chtable thead th { font-size: .74rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
table.scores tbody th { font-weight: 600; }
table.scores .hint { display: block; font-weight: 400; font-size: .76rem; color: var(--muted); }
table.scores td { font-variant-numeric: tabular-nums; white-space: nowrap; }
td.delta.good { color: var(--ok); font-weight: 650; }
td.delta.bad { color: var(--bad); font-weight: 650; }
.chtable td.from { color: var(--bad); }
.chtable td.to { color: var(--ok); }
.chtable td.why { color: var(--muted); font-size: .84rem; }
.chtable, .scores { display: block; overflow-x: auto; }
.chtable thead, .chtable tbody, .scores thead, .scores tbody { display: table; width: 100%; table-layout: auto; }

.hardlist { padding-left: 1.2rem; margin: 0; }
.hardlist li { margin-bottom: .55rem; }
.hmeta { display: block; font-size: .76rem; color: var(--muted); font-weight: 650; }
.htext { font-family: var(--serif); }
.rare { display: flex; flex-wrap: wrap; gap: .3rem; margin: 0; }
.wchip { font-size: .82rem; border-radius: 999px; padding: .08rem .55rem; border: 1px solid var(--line); }
.wchip.lvB2 { background: var(--B2); } .wchip.lvC1 { background: var(--C1); } .wchip.lvC2 { background: var(--C2); } .wchip.lvoff { background: var(--off); }
.exportrow { display: flex; flex-wrap: wrap; gap: .45rem; margin: .9rem 0; }
details.method, details.compare { margin: .4rem 0 1rem; font-size: .9rem; color: var(--muted); }
details.method summary, details.compare summary { cursor: pointer; font-weight: 600; color: var(--ink); }
details.compare { color: var(--ink); }

/* ---- Rewrite form ---------------------------------------------------------- */
.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .7rem .9rem; }
@media (max-width: 620px) { .fields { grid-template-columns: minmax(0, 1fr); } }
.fld { display: flex; flex-direction: column; gap: .25rem; font-size: .84rem; color: var(--muted); }
.fld .hint { font-size: .78rem; opacity: .85; }
.fld select, .fld input { color: var(--ink); font-size: .95rem; background: var(--panel); }
.fld.wide { grid-column: 1 / -1; }
.okline { color: var(--ok); }
.missline { color: var(--warn); }
.runrow { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; margin-top: .8rem; }
.meter { font-size: .85rem; color: var(--muted); }
.gate { margin-top: .6rem; background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: .5rem .75rem; font-size: .9rem; }
.gate p { margin: 0; }
.notice { border-radius: 10px; padding: .55rem .8rem; font-size: .9rem; margin: .5rem 0; }
.notice.info, p.notice { background: var(--info-soft); color: var(--info); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }

/* ---- Progress ------------------------------------------------------------------ */
.progresshead { display: flex; justify-content: space-between; gap: .6rem; }
.steps { list-style: none; padding: 0; margin: .7rem 0 0; display: grid; gap: .35rem; }
.stage { display: flex; gap: .55rem; align-items: center; color: var(--muted); font-size: .92rem; }
.stage.now { color: var(--ink); font-weight: 650; }
.stage.done { color: var(--ok); }
.stagedot { width: 1.4rem; height: 1.4rem; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-size: .75rem;
  border: 1px solid var(--line); background: var(--panel-2); flex: none; }
.stage.now .stagedot { border-color: var(--accent); color: var(--accent); }
.stage.done .stagedot { background: var(--ok-soft); border-color: var(--ok); }

/* ---- Result ----------------------------------------------------------------------- */
.reshead { display: flex; justify-content: space-between; align-items: baseline; gap: .6rem; flex-wrap: wrap; }
.reshead h2 { margin: 0; }
.levelread { font-style: italic; color: var(--muted); border-left: 3px solid var(--accent); padding-left: .7rem; margin: .7rem 0; }
.verdict { border-radius: 12px; padding: .7rem .9rem; margin: .7rem 0; }
.verdict.pass { background: var(--ok-soft); color: var(--ok); }
.verdict.miss { background: var(--warn-soft); color: var(--warn); }
.verdict p { margin: .3rem 0 0; color: var(--ink); }
.verdict .warnline { color: var(--warn); }
.diff { font-family: var(--serif); font-size: 1.02rem; line-height: 1.85; background: var(--panel-2); border-radius: 12px; padding: .9rem 1rem;
  max-height: 30rem; overflow-y: auto; overflow-wrap: anywhere; }
.diff del { background: var(--bad-soft); color: var(--bad); text-decoration: line-through; border-radius: 3px; padding: 0 2px; }
.diff ins { background: var(--ok-soft); color: var(--ok); text-decoration: none; border-radius: 3px; padding: 0 2px; }
.side { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .8rem; }
@media (max-width: 720px) { .side { grid-template-columns: minmax(0, 1fr); } }
.sidecol h3 { margin-top: 0; }
.sidecol .plain { font-family: var(--serif); white-space: pre-wrap; background: var(--panel-2); border-radius: 12px; padding: .8rem .9rem; overflow-wrap: anywhere; }
.resview .marked { max-height: none; }
.raw { white-space: pre-wrap; overflow-wrap: anywhere; font-family: var(--mono); font-size: .82rem; background: var(--panel-2); padding: .8rem; border-radius: 10px; }

/* ---- History ------------------------------------------------------------------------ */
.histhead { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: .5rem; }
.histhead h2 { margin: 0; }
.histhead input { max-width: 15rem; }
.histlist { list-style: none; padding: 0; margin: 0; }
.histitem { display: flex; gap: .5rem; align-items: center; border-bottom: 1px solid var(--line); padding: .35rem 0; }
.histopen { flex: 1; text-align: left; background: transparent; display: flex; flex-direction: column; padding: .35rem .2rem; min-width: 0; }
.histopen strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.histopen:hover strong { color: var(--accent); }
li.none { color: var(--muted); font-size: .9rem; padding: .3rem 0; }

/* ---- Footer and toasts ---------------------------------------------------------- */
.foot { max-width: 1080px; margin: 0 auto; padding: 1rem 16px 2.5rem; color: var(--muted); font-size: .84rem; border-top: 1px solid var(--line); }
.foot p { margin: .35rem 0; }
.toasts { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: .4rem; z-index: 20;
  width: min(92vw, 30rem); }
.toast { background: var(--night); color: var(--night-ink); border-radius: 10px; padding: .6rem .85rem; font-size: .9rem; box-shadow: var(--shadow); transition: opacity .5s; }
.toast.err { background: var(--bad); color: #fff; }
.toast a { color: inherit; font-weight: 700; }
.toast.gone { opacity: 0; }
@media (max-width: 520px) {
  .appname { font-size: .95rem; }
  .backlink { display: none; }
  .sep { display: none; }
  .tval { font-size: 1.35rem; }
}

/* ---- Docs pages (api.html, tokens.html) -------------------------------------- */
.doc { max-width: 900px; }
.doc h1 { margin-top: 1.5rem; font-family: var(--serif); }
.doc h2 { margin-top: 1.6rem; }
.doc code { font-family: var(--mono); font-size: .86em; background: var(--panel-2); border-radius: 4px; padding: .05rem .3rem; }
.doc pre, pre.block { overflow-x: auto; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: .7rem .8rem; font-family: var(--mono); font-size: .82rem; }
.codegroup { margin: .6rem 0 1.2rem; }
.tabs { display: flex; flex-wrap: wrap; gap: .25rem; margin-bottom: .35rem; }
.tab { font-size: .8rem; padding: .25rem .6rem; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; }
.tab[aria-selected="true"], .tab.active { background: var(--night); color: var(--night-ink); border-color: var(--night); }
.errtable { width: 100%; border-collapse: collapse; font-size: .9rem; display: block; overflow-x: auto; }
.errtable th, .errtable td { border-bottom: 1px solid var(--line); padding: .4rem .5rem; text-align: left; vertical-align: top; }
.statebox { border: 1px solid var(--line); border-radius: 8px; padding: .8rem; background: var(--panel-2); margin: .75rem 0 1.25rem; }
.statebox p { margin: .3rem 0; }
.tokenrow { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .5rem 0; }
.tokenbox { flex: 1 1 20rem; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: .55rem .7rem; font-size: .82rem;
  word-break: break-all; font-family: var(--mono); }
.mini { background: transparent; color: var(--ink); padding: .3rem .6rem; font-size: .82rem; border: 1px solid var(--line); border-radius: 8px; }
.mini:hover:enabled { background: var(--panel-2); }
.mono { font-family: var(--mono); }

@media print {
  .topbar, .examples, .editor, .rewrite, .history, .exportrow, .toasts, .foot, .seg { display: none !important; }
  .card { box-shadow: none; break-inside: avoid; }
  .marked { max-height: none; }
}
