:root {
  --paper: #f2f4f7;
  --ink: #1c2434;
  --muted: #5c6578;
  --line: #d3d8e2;
  --accent: #2b4fd8;
  --accent-ink: #ffffff;
  --warn: #b8730f;
  --ok: #1f7a4d;
  --bad: #b3261e;
  --field: #ffffff;
  --radius: 4px;
}
* { box-sizing: border-box; }
html {
  font: 16px/1.5 "IBM Plex Sans", "Segoe UI", system-ui,
        "Noto Sans SC", "Noto Sans JP", "Noto Sans KR", "Noto Sans Thai",
        "Noto Sans Arabic", "Microsoft YaHei", "Hiragino Sans", "Malgun Gothic",
        sans-serif;
  color: var(--ink); background: var(--paper);
}
/* CJK and Thai need more vertical room than Latin at the same size: glyphs are
   denser and ascenders/descenders collide on a 1.5 line box. */
html:lang(zh), html:lang(ja), html:lang(ko), html:lang(th) { line-height: 1.7; }
body { margin: 0; }
main { max-width: 680px; margin: 0 auto; padding: 24px 20px 64px; }
h1 { font-size: 1.75rem; font-weight: 600; letter-spacing: -0.01em; margin: 0 0 12px; }
h2 { font-size: 1.05rem; font-weight: 600; margin: 40px 0 8px; }
p { margin: 0 0 12px; }
a { color: var(--accent); }
.lead { color: var(--muted); max-width: 60ch; }
.fine, .opt { color: var(--muted); font-size: 0.85rem; }
.alt { margin-top: 24px; color: var(--muted); }
.empty { color: var(--muted); }

.top { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--field); }
.brand { color: var(--ink); text-decoration: none; font-weight: 600; }
.brand span { color: var(--muted); font-weight: 400; }
.top nav { display: flex; gap: 18px; align-items: center; }
.top nav a { color: var(--ink); text-decoration: none; }
.top nav a.home-btn { border: 1px solid var(--line); border-radius: 6px; padding: 5px 12px; color: var(--muted); }
.top nav a.home-btn:hover { color: var(--ink); border-color: var(--ink); }
.top nav form { margin: 0; }
button.link { background: none; border: 0; padding: 0; color: var(--muted); font: inherit; cursor: pointer; }

.notice { padding: 10px 14px; border-left: 3px solid var(--accent); background: var(--field); margin-bottom: 16px; }
.notice.ok { border-color: var(--ok); }
.notice.bad { border-color: var(--bad); }

/* forms */
.stack { display: flex; flex-direction: column; gap: 14px; margin-top: 20px; }
.narrow { max-width: 380px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 0.95rem; min-width: 0; }
input[type=email], input[type=password], input[type=text], input:not([type]), input[type=number], textarea {
  font: inherit; padding: 9px 11px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--field); color: var(--ink);
  width: 100%; box-sizing: border-box;
}
input:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.primary { align-self: flex-start; font: inherit; font-weight: 600; padding: 10px 18px; border: 0; border-radius: var(--radius); background: var(--accent); color: var(--accent-ink); cursor: pointer; }
button.secondary { font: inherit; padding: 8px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--field); color: var(--ink); cursor: pointer; }
button.secondary.danger { color: var(--bad); }
button.chip { font: inherit; font-size: 0.9rem; padding: 5px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--field); cursor: pointer; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.row form { margin: 0; }

/* plans */
fieldset.plans { border: 0; padding: 0; margin: 0; }
fieldset.plans legend { font-size: 0.95rem; margin-bottom: 6px; }
.plans { display: flex; flex-direction: column; gap: 8px; }
.plan { position: relative; display: grid; grid-template-columns: 1fr auto; grid-template-areas: "name price" "blurb blurb" "fine fine"; column-gap: 12px; row-gap: 4px; padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--field); cursor: pointer; }
.plan-fine { line-height: 1.5; }
.plan:hover { border-color: var(--muted); }
/* Highlight follows the live radio state, not just what the server rendered. */
.plan:has(input:checked) { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.plan:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.plan input { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.plan-name { grid-area: name; font-weight: 600; }
.plan-price { grid-area: price; color: var(--muted); }
.plan-blurb { grid-area: blurb; }
.plan-fine { grid-area: fine; color: var(--muted); font-size: 0.85rem; }
.plan-tag { font-weight: 400; color: var(--muted); }
.plan-tag.pending { color: var(--warn); }

/* usage meter — the one loud element */
.meter-block { margin: 20px 0 8px; padding: 20px 0 8px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.meter { position: relative; height: 28px; background: var(--field); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.meter .fill { height: 100%; background: var(--accent); transition: width 240ms ease-out; }
.meter .tick { position: absolute; top: 0; bottom: 0; inset-inline-start: 80%; width: 1px; background: var(--line); }
.meter-block.warn .fill { background: var(--warn); }
.meter-read { margin-top: 8px; }
.meter-read strong { font-size: 1.25rem; }
.balance { color: var(--muted); }
.balance strong { color: var(--ink); }
.balance a { margin-left: 8px; }

/* license key */
textarea.key { width: 100%; font: 0.85rem/1.4 "IBM Plex Mono", ui-monospace, Menlo, Consolas, monospace; word-break: break-all; resize: vertical; margin-bottom: 10px; }

/* tables */
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; margin-bottom: 8px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-weight: 600; color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.neg { color: var(--bad); }

@media (max-width: 560px) {
  .top { flex-direction: column; align-items: flex-start; }
  table { font-size: 0.85rem; }
  th, td { padding: 6px 4px; }
}
@media (prefers-reduced-motion: reduce) { .meter .fill { transition: none; } }

/* ---------- admin ---------- */
main.wide { max-width: 1100px; }
.top.admin { border-bottom: 2px solid var(--ink); }
.top.admin .who { color: var(--muted); font-size: 0.9rem; margin-left: 8px; }
.top.admin nav a.signout { border: 1px solid var(--line); border-radius: 6px; padding: 5px 12px; color: var(--muted); }
.top.admin nav a.signout:hover { color: var(--ink); border-color: var(--ink); }
.admin-login { max-width: 20rem; margin: 8vh auto 0; }
.admin-login form { display: flex; flex-direction: column; gap: 12px; }
.admin-login button { align-self: flex-start; margin-top: 4px; }
.crumb { color: var(--muted); font-size: 0.9rem; margin-bottom: 4px; }
.crumb a { color: var(--muted); }

.strip { display: flex; flex-wrap: wrap; gap: 8px 28px; padding: 12px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); margin: 12px 0 20px; }
.strip div { display: flex; flex-direction: column; }
.strip .k { font-size: 0.8rem; color: var(--muted); }
.strip .v { font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.filters input[type=search] { flex: 1 1 220px; }
.filters select { font: inherit; padding: 8px 10px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--field); color: var(--ink); }
.filters .reset { color: var(--muted); font-size: 0.9rem; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; background: var(--line); color: var(--ink); }
.badge.premium { background: #dfe6fb; color: var(--accent); }
.badge.base { background: #e2f1ea; color: var(--ok); }
.badge.demo { background: var(--line); }
.badge.off { background: #f6dedc; color: var(--bad); }
.badge.topup { background: #e2f1ea; color: var(--ok); }
.badge.plan_fee { background: #dfe6fb; color: var(--accent); }
.badge.usage { background: #fbeedb; color: var(--warn); }
.badge.adjustment { background: #ece6f7; color: #5b3fa0; }
.badge.refund { background: #e2f1ea; color: var(--ok); }

tr.dim td { color: var(--muted); }
.muted { color: var(--muted); }
.mono { font-family: "IBM Plex Mono", ui-monospace, Menlo, Consolas, monospace; font-size: 0.8rem; }
.pager { display: flex; gap: 16px; align-items: center; color: var(--muted); font-size: 0.9rem; }

.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 28px; }
.cols section h2 { margin-top: 16px; }
.kv { width: auto; }
.kv th { font-weight: 400; color: var(--muted); padding-right: 24px; }
.balance.big strong { font-size: 1.4rem; }
.meter.small { height: 12px; margin-top: 4px; }
.actions { margin-top: 32px; padding-top: 8px; border-top: 1px solid var(--line); }
.actions .stack { margin-top: 8px; }
.actions .stack form { margin: 0; }
strong.neg { color: var(--bad); }

/* admin: plan editor */
.plan-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; margin-top: 16px; }

/* Password change: narrow column so the fields don't stretch across a wide page. */
.password-form { display: flex; flex-direction: column; gap: 12px; max-width: 22rem; }
.password-form button { align-self: flex-start; margin-top: 4px; }
.plan-edit { display: flex; flex-direction: column; gap: 10px; padding: 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--field); }
.plan-edit h2 { margin: 0 0 4px; display: flex; align-items: center; gap: 8px; }
.plan-edit label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.plan-edit label small { color: var(--muted); }
.plan-edit .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.plan-edit textarea { font: inherit; }
.plan-edit button { align-self: flex-start; margin-top: 4px; }
.plan-edit .fine { margin: 0; }


/* ---------------------------------------------------------------- i18n ---- */

/* Language switcher. Sits in the header next to the nav links. */
.top nav form.lang { margin: 0; }
.top nav form.lang label { flex-direction: row; align-items: center; gap: 6px; }
.top nav form.lang select {
  font: inherit; font-size: 0.9rem; padding: 4px 8px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--field); color: var(--ink); cursor: pointer;
}

/* Visible to screen readers only — the select has a visible glyph but no
   visible text label, so the accessible name has to come from somewhere. */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Right-to-left (Arabic).
   Most of the layout is flexbox and inherits direction for free; these are the
   places that hard-code a physical side and would otherwise point the wrong way. */
[dir="rtl"] .meter .fill { margin-inline-start: 0; }
[dir="rtl"] .notice { border-left: 0; border-right: 3px solid var(--accent); }
[dir="rtl"] .notice.ok { border-right-color: var(--ok); }
[dir="rtl"] .notice.bad { border-right-color: var(--bad); }
[dir="rtl"] table th, [dir="rtl"] table td { text-align: right; }
[dir="rtl"] .num { text-align: left; }

/* Money and token counts stay left-to-right even inside RTL text: "$27.07-"
   is what you get otherwise, with the minus sign drifting to the wrong end. */
[dir="rtl"] .num, [dir="rtl"] .balance strong, [dir="rtl"] .plan-price,
[dir="rtl"] .meter-read strong {
  direction: ltr; unicode-bidi: isolate;
}

/* The license key is base64url — it must never be reordered by bidi. */
.key { direction: ltr; text-align: left; unicode-bidi: isolate; }
