:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --line: #deded9;
  --accent: #2456c9;
  --accent-text: #ffffff;
  --danger: #b3261e;
  --warn-bg: #fff4d6;
  --warn-text: #6b4e00;
  --err-bg: #fde8e6;
  --ok: #1d7a3a;
  --chip: #eceef4;
  --chip-start: #e3f0e6;
  --radius: 10px;
  --tap: 44px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #222220;
    --text: #ececea;
    --muted: #a0a09a;
    --line: #3a3a37;
    --accent: #7aa2ff;
    --accent-text: #0d1630;
    --danger: #ff8a80;
    --warn-bg: #3a3214;
    --warn-text: #f3d98a;
    --err-bg: #3d1d1a;
    --ok: #6fd08f;
    --chip: #30323a;
    --chip-start: #23382a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
h1 { font-size: 1.15rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .5rem; }
h3 { font-size: .95rem; margin: 0 0 .25rem; }
p { margin: .4rem 0; }
code { font-size: .9em; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--danger); }
.warn { color: var(--warn-text); background: var(--warn-bg); padding: .4rem .6rem; border-radius: 6px; }
.spacer { flex: 1; }

/* ---------- header */
.top {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.top-inner, .tabs, main { max-width: 900px; margin: 0 auto; }
.top-inner { display: flex; align-items: center; gap: 1rem; padding: .6rem 1rem; }
.title { flex: 1; min-width: 0; }
.status { margin: 0; color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status.dirty { color: var(--warn-text); }
.tabs { display: flex; gap: .25rem; padding: 0 .75rem; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.tabs button {
  appearance: none; border: 0; background: none; color: var(--muted);
  font: inherit; font-size: .95rem; min-height: var(--tap); padding: 0 .8rem;
  border-bottom: 3px solid transparent; cursor: pointer; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 1rem 1rem 4rem; padding-bottom: calc(4rem + env(safe-area-inset-bottom)); }

/* ---------- controls */
.btn {
  appearance: none; font: inherit; font-size: .95rem;
  min-height: var(--tap); padding: 0 1rem;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--text); cursor: pointer;
  touch-action: manipulation;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: none; color: var(--muted); }
.btn.small { min-height: 36px; padding: 0 .7rem; font-size: .88rem; }
.btn.icon { min-width: 40px; padding: 0; }
@media (pointer: coarse) { .btn.small { min-height: var(--tap); } }

input[type="text"], input[type="password"], input[type="tel"], textarea {
  font: inherit; font-size: 16px; /* 16px stops iOS zooming on focus */
  color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px;
  padding: .55rem .7rem; min-height: var(--tap); width: 100%;
}
/* Separate rules: an older browser (KaiOS 3.1 is Gecko 84) drops a whole rule if it
   doesn't know one selector in it, and :focus-visible is newer than that. */
input:focus, textarea:focus, .btn:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn:focus:not(:focus-visible) { outline: none; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin: .4rem 0; }
.row > input { flex: 1 1 12rem; width: auto; min-width: 0; }
.field { display: block; }
.field-label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .25rem; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.grid .wide { grid-column: 1 / -1; }
@media (max-width: 560px) { .grid { grid-template-columns: 1fr; } }

.switch { display: inline-flex; align-items: center; gap: .5rem; min-height: var(--tap); cursor: pointer; }
.switch input { width: 22px; height: 22px; accent-color: var(--accent); }

.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem; margin-bottom: 1rem;
}

/* ---------- notices / toast */
.notice { border-radius: var(--radius); padding: .6rem .9rem; margin-bottom: 1rem; border: 1px solid var(--line); }
.notice.error { background: var(--err-bg); }
.notice.info { background: var(--surface); }
.toast {
  position: fixed; left: 50%; bottom: calc(1.5rem + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 999px;
  font-size: .9rem; z-index: 20; max-width: calc(100% - 2rem);
}

/* ---------- find */
.find-result:empty { display: none; }
.find-result { margin-top: .5rem; }
.find-line { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.digits-inline { font-family: ui-monospace, Menlo, monospace; font-size: 1.05rem; letter-spacing: .05em; }

/* ---------- rules */
.rules-head { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .5rem; }
.rule-list { display: flex; flex-direction: column; gap: .6rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; scroll-margin-top: 120px; }
.card.invalid { border-color: var(--danger); }
.card-head {
  appearance: none; border: 0; background: none; color: inherit; font: inherit; text-align: left;
  width: 100%; min-height: 52px; padding: .5rem .9rem; cursor: pointer;
  display: flex; align-items: center; gap: .7rem;
}
.digits { font-family: ui-monospace, Menlo, monospace; font-weight: 700; font-size: 1.1rem; min-width: 4.5ch; letter-spacing: .05em; }
.badge { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; padding: .1rem .45rem; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.badge.first { border-color: var(--accent); color: var(--accent); }
.summary { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--muted); }
.warn-dot { background: var(--warn-bg); color: var(--warn-text); border-radius: 999px; width: 1.4rem; height: 1.4rem; display: inline-grid; place-items: center; font-weight: 700; font-size: .8rem; }
.chev { color: var(--muted); }
.card-body { padding: 0 .9rem .9rem; border-top: 1px solid var(--line); }
.section { margin-top: .9rem; }
.card-foot { margin-top: 1rem; }

.warn-list { margin: .6rem 0 0; padding: 0; list-style: none; }
.warn-list li { background: var(--warn-bg); color: var(--warn-text); padding: .35rem .6rem; border-radius: 6px; margin-bottom: .3rem; font-size: .88rem; }

.cand { border: 1px solid var(--line); border-radius: 8px; padding: .6rem; margin: .5rem 0; }
.cand-head { display: flex; align-items: center; gap: .5rem; }
.rank { color: var(--muted); font-size: .85rem; min-width: 1.2rem; text-align: right; }
.cand-word { flex: 1; min-width: 0; font-weight: 600; }
.cand-tools { display: flex; gap: .25rem; }
.chips { display: flex; flex-wrap: wrap; gap: .35rem; margin: .5rem 0 .2rem; align-items: center; }
.chip { display: inline-flex; align-items: center; gap: .15rem; background: var(--chip); border-radius: 999px; padding: .1rem .15rem .1rem .65rem; font-size: .92rem; }
.chip.start { background: var(--chip-start); font-style: italic; }
.chip-x {
  appearance: none; border: 0; background: none; color: var(--muted); cursor: pointer;
  width: 30px; height: 30px; border-radius: 999px; font-size: .8rem;
}
@media (pointer: coarse) { .chip-x { width: 36px; height: 36px; } }
.chip-x:hover { background: var(--line); }

/* ---------- json / test / history / phone */
#json-text { min-height: 60vh; font-family: ui-monospace, Menlo, monospace; font-size: 14px; line-height: 1.4; white-space: pre; tab-size: 2; }
@media (pointer: coarse) { #json-text { font-size: 16px; } }
.json-status.ok { color: var(--ok); }
.json-status.error { color: var(--danger); }
.test-out { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem; white-space: pre-wrap; font-family: ui-monospace, Menlo, monospace; min-height: 3rem; }
.history { list-style: none; padding: 0; margin: 1rem 0; }
.history-item { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: .4rem .7rem; margin-bottom: .4rem; min-height: 52px; }
.history-item .when { font-weight: 600; }
.steps { padding-left: 1.3rem; }
.steps li { margin-bottom: .4rem; }
.url-box {
  display: block; flex: 1 1 12rem; min-width: 0;
  font-family: ui-monospace, Menlo, monospace; font-size: 14px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  padding: .55rem .7rem; word-break: break-all; user-select: all; -webkit-user-select: all;
}

/* ---------- admin login */
.login { max-width: 420px; margin: 2rem auto; }
.home-link { color: inherit; text-decoration: none; }
.title h1 .muted { font-weight: 400; }

/* ---------- landing page */
a { color: var(--accent); }
.plain { padding-left: 1.2rem; }
.plain li { margin-bottom: .35rem; }
.try-out { font-size: 1.05rem; min-height: 1.5em; }
.rules-view { display: flex; flex-direction: column; gap: .5rem; }
.view-rule { display: flex; gap: .9rem; align-items: baseline; border-top: 1px solid var(--line); padding-top: .5rem; }
.view-rule:first-child { border-top: 0; padding-top: 0; }
.view-body p { margin: .1rem 0 .3rem; line-height: 1.9; }
.view-body .chip { padding: .05rem .6rem; }
.foot { text-align: center; margin-top: 2rem; }

/* ---------- small screens: KaiOS phones (240x320) and narrow windows */
@media (max-width: 360px) {
  :root { --tap: 32px; --radius: 6px; }
  body { font-size: 14px; line-height: 1.35; }
  h1 { font-size: 15px; }
  h2 { font-size: 14px; margin-bottom: .3rem; }
  h3 { font-size: 13px; }
  p { margin: .3rem 0; }

  /* Header scrolls away instead of taking a third of the screen. */
  .top { position: static; }
  .top-inner { padding: .35rem .5rem; gap: .5rem; }
  .status { font-size: 11px; }
  .title h1 .muted { display: none; }
  #save { min-height: 30px; padding: 0 .7rem; }
  .tabs { flex-wrap: wrap; gap: 0; padding: 0 .25rem; overflow: visible; }
  .tabs button { min-height: 30px; padding: 0 .45rem; font-size: 12px; border-bottom-width: 2px; }

  main { padding: .4rem .4rem 2rem; }
  .panel { padding: .5rem; margin-bottom: .5rem; }
  .notice { padding: .4rem .5rem; margin-bottom: .5rem; }
  .login { margin: .5rem auto; }

  .btn { font-size: 13px; padding: 0 .6rem; border-radius: 6px; }
  .btn.small { min-height: 28px; padding: 0 .45rem; font-size: 12px; }
  .btn.icon { min-width: 28px; }
  input[type="text"], input[type="password"], input[type="tel"], textarea { font-size: 14px; padding: .3rem .45rem; min-height: 30px; border-radius: 6px; }
  .row { gap: .3rem; margin: .3rem 0; }
  .row > input { flex: 1 1 100%; }
  .field-label { font-size: 11px; }
  .small { font-size: 11px; }
  .switch input { width: 16px; height: 16px; }

  /* Rule cards: digits, badge and arrow on one line, the summary on the next. */
  .rule-list { gap: .35rem; }
  .card { scroll-margin-top: 0; }
  .card-head { flex-wrap: wrap; min-height: 0; padding: .35rem .5rem; gap: .1rem .4rem; }
  .digits { font-size: 15px; min-width: 0; }
  .badge { font-size: 9px; padding: 0 .3rem; }
  .chev { margin-left: auto; }
  .summary { order: 5; flex: 1 1 100%; font-size: 12px; }
  .warn-dot { width: 1.1rem; height: 1.1rem; font-size: 11px; }
  .card-body { padding: 0 .5rem .5rem; }
  .section { margin-top: .5rem; }
  .card-foot { margin-top: .5rem; }
  .warn-list li { font-size: 11px; padding: .25rem .4rem; }

  /* A word gets its own line; the move/remove buttons sit under it. */
  .cand { padding: .35rem; margin: .35rem 0; }
  .cand-head { flex-wrap: wrap; gap: .25rem; }
  .rank { display: none; }
  .cand-word { flex: 1 1 100%; }
  .cand-tools { margin-left: auto; }
  .chips { gap: .25rem; margin: .3rem 0 .1rem; }
  .chip { font-size: 12px; padding: 0 0 0 .45rem; }
  .chip-x { width: 24px; height: 24px; font-size: 10px; }

  .rules-head { gap: .3rem; }
  .find-line { gap: .25rem; }
  .digits-inline { font-size: 14px; }

  #json-text { min-height: 55vh; font-size: 11px; }
  .test-out { font-size: 12px; padding: .5rem; }
  .history-item { min-height: 0; padding: .3rem .45rem; gap: .3rem; font-size: 12px; }
  .url-box { flex-basis: 100%; font-size: 12px; padding: .35rem .45rem; border-radius: 6px; }
  .steps, .plain { padding-left: 1.1rem; }
  .steps li, .plain li { margin-bottom: .25rem; }
  code { font-size: 11px; }
  .toast { font-size: 12px; padding: .4rem .7rem; bottom: .5rem; }

  /* Landing page: a rule's digits above its words. */
  .view-rule { flex-direction: column; gap: 0; }
  .view-body p { line-height: 1.7; font-size: 13px; }
  .try-out { font-size: 13px; }
  .foot { margin-top: 1rem; }
}
