:root {
  --bg: #fbf7f2;
  --panel: #fff;
  --ink: #2c2622;
  --muted: #7d7167;
  --line: #e6dcd0;
  --accent: #a8543f;
  --accent-soft: #f4e6e0;
  --warn-bg: #fdf4e3;
  --warn-line: #e0c48a;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #191614;
    --panel: #221e1b;
    --ink: #ece5dd;
    --muted: #a1948a;
    --line: #3a322c;
    --accent: #e08a6d;
    --accent-soft: #33241f;
    --warn-bg: #2e2618;
    --warn-line: #6b5527;
  }
}

* { box-sizing: border-box; }

/* A class that sets `display` outruns the UA rule for [hidden], which would leave
   the toolbar on screen with nothing to act on. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

.layout { display: flex; min-height: 100vh; }

/* ---- sidebar ---- */
.sidebar {
  width: 240px;
  flex: 0 0 240px;
  border-right: 1px solid var(--line);
  padding: 24px 16px;
  background: var(--panel);
}
.sidebar h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 12px;
}
.history { list-style: none; margin: 0; padding: 0; }
.history li { margin-bottom: 2px; }
.history button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-radius: 6px;
  padding: 8px 10px;
  color: inherit;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  line-height: 1.35;
}
.history button:hover { background: var(--accent-soft); }
.history button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.history .chan { display: block; font-size: 12px; color: var(--muted); }
.history .empty { color: var(--muted); font-size: 14px; padding: 8px 10px; }

.signout { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--line); }
.signout button {
  width: 100%;
  padding: 8px;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  background: none;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}
.signout button:hover { color: var(--accent); border-color: var(--accent); }

/* ---- main ---- */
.main { flex: 1; max-width: 820px; padding: 40px 32px 120px; }
.masthead h1 { margin: 0; font-size: 28px; letter-spacing: -0.02em; }
.tagline { margin: 4px 0 28px; color: var(--muted); }

.form { display: flex; gap: 8px; }
.form input {
  flex: 1;
  /* Flex items default to min-width:auto, so the input refuses to shrink below
     its placeholder and shoves the button off a narrow screen. */
  min-width: 0;
  padding: 12px 14px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form button {
  flex: 0 0 auto;
  white-space: nowrap;
  padding: 12px 22px;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
}
.form button:disabled { opacity: 0.5; cursor: default; }

.refresh {
  display: block;
  margin: 10px 2px 0;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
}

/* ---- progress ---- */
.status { margin-top: 28px; }
.stages { list-style: none; margin: 0; padding: 0; }
.stages li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 5px 0;
  color: var(--muted);
  font-size: 14px;
}
.stages li::before {
  content: "";
  width: 9px; height: 9px;
  border-radius: 50%;
  border: 1.5px solid var(--line);
  flex: 0 0 auto;
}
.stages li.active { color: var(--accent); font-weight: 600; }
.stages li.active::before { background: var(--accent); border-color: var(--accent); animation: pulse 1.2s infinite; }
.stages li.done::before { background: var(--muted); border-color: var(--muted); }
@keyframes pulse { 50% { opacity: 0.3; } }
.hint { color: var(--muted); font-size: 13px; margin-top: 10px; }

.error {
  margin-top: 24px;
  padding: 14px 16px;
  border: 1px solid var(--accent);
  border-left-width: 4px;
  border-radius: var(--radius);
  background: var(--panel);
}

/* ---- pattern ---- */
.pattern { margin-top: 36px; }
.pattern h1 { font-size: 26px; margin: 0 0 4px; letter-spacing: -0.02em; }
.pattern .tutorial { margin: 0 0 2px; font-size: 14px; }
.pattern .tutorial a { color: var(--accent); word-break: break-all; }
.pattern .meta { color: var(--muted); font-size: 14px; margin-bottom: 28px; }

.abbrs { font-size: 15px; line-height: 1.75; }
.sizing { font-style: italic; color: var(--muted); font-size: 14px; margin: 0 0 8px; }

.aside {
  margin: 8px 0 14px;
  padding: 9px 14px;
  border-left: 3px solid var(--line);
  background: var(--panel);
  border-radius: 0 6px 6px 0;
  font-size: 15px;
}
.aside-kind { font-weight: 700; }
.pattern h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  padding-bottom: 6px;
  margin: 32px 0 14px;
}
.pattern h3 { font-size: 18px; margin: 26px 0 10px; }
.pattern ul { padding-left: 20px; }
.pattern table { border-collapse: collapse; font-size: 14px; }
.pattern td { padding: 3px 20px 3px 0; vertical-align: top; }
.pattern td:first-child { font-weight: 600; white-space: nowrap; }

.section { break-inside: avoid; page-break-inside: avoid; }
.step { margin: 0 0 10px; }
.step .label { font-weight: 700; }
.step .count { font-variant-numeric: tabular-nums; }
.step .note {
  display: block;
  margin: 3px 0 0 16px;
  padding-left: 10px;
  border-left: 2px solid var(--line);
  color: var(--muted);
  font-size: 14px;
}
.legend { font-size: 13px; color: var(--muted); font-style: italic; margin-top: 18px; }

.ts {
  margin-left: 6px;
  font-size: 12px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.ts:hover { text-decoration: underline; }

.unclear {
  margin-top: 32px;
  padding: 14px 18px;
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  border-radius: var(--radius);
}
.unclear h2 { border: 0; margin-top: 0; }
.unclear p { font-size: 13px; color: var(--muted); margin: 0 0 8px; }
.unclear ul { margin: 0; }

/* ---- toolbar ---- */
.toolbar {
  position: fixed;
  right: 24px;
  bottom: 24px;
  display: flex;
  gap: 8px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.12);
}
.toolbar button {
  padding: 8px 14px;
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: none;
  border: 1px solid var(--line);
  border-radius: 7px;
  cursor: pointer;
}
.toolbar button:hover { border-color: var(--accent); color: var(--accent); }

/* ---- print / Save as PDF ---- */
@media print {
  @page { margin: 18mm 16mm; }

  .sidebar, .form, .refresh, .status, .toolbar, .masthead, .error { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  .layout { display: block; }
  .main { max-width: none; padding: 0; }
  .pattern { margin: 0; }

  /* Links are dead on paper: keep the time, drop the underline and colour. */
  .ts { color: #666; text-decoration: none; }
  .pattern .meta a { color: #000; text-decoration: none; }

  .pattern h2 { color: #000; border-bottom: 1px solid #999; break-after: avoid; page-break-after: avoid; }
  .pattern h3 { break-after: avoid; page-break-after: avoid; }
  .step { break-inside: avoid; page-break-inside: avoid; }
  .aside { background: none; border-left: 2px solid #999; break-inside: avoid; }
  .pattern .tutorial a { color: #000; }
  .unclear { background: none; border: 1px solid #999; break-inside: avoid; }

  /* Printed copies get separated from the tab they came from. */
  .pattern::after {
    content: "Source: " attr(data-source);
    display: block;
    margin-top: 18pt;
    padding-top: 6pt;
    border-top: 1px solid #ccc;
    font-size: 8pt;
    color: #666;
  }
}

@media (max-width: 720px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; flex: none; border-right: 0; border-bottom: 1px solid var(--line); }
  .main { padding: 24px 18px 120px; }
}
