:root {
  --bg: #0b0f14;
  --bg-grid: rgba(52, 211, 153, 0.035);
  --surface: #121821;
  --surface-2: #18202b;
  --border: #233040;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #34d399;
  --accent-2: #60a5fa;
  --accent-ink: #04130d;
  --danger: #f87171;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03) inset, 0 10px 30px -12px rgba(0, 0, 0, 0.6);
  --radius: 14px;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f6f8fa;
  --bg-grid: rgba(5, 150, 105, 0.05);
  --surface: #ffffff;
  --surface-2: #f0f3f6;
  --border: #d8dee4;
  --text: #1f2328;
  --muted: #59636e;
  --accent: #059669;
  --accent-2: #2563eb;
  --accent-ink: #ffffff;
  --danger: #cf222e;
  --shadow: 0 1px 2px rgba(31, 35, 40, 0.06), 0 8px 24px -16px rgba(31, 35, 40, 0.2);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f6f8fa;
    --bg-grid: rgba(5, 150, 105, 0.05);
    --surface: #ffffff;
    --surface-2: #f0f3f6;
    --border: #d8dee4;
    --text: #1f2328;
    --muted: #59636e;
    --accent: #059669;
    --accent-2: #2563eb;
    --accent-ink: #ffffff;
    --danger: #cf222e;
    --shadow: 0 1px 2px rgba(31, 35, 40, 0.06), 0 8px 24px -16px rgba(31, 35, 40, 0.2);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 80px; }
body {
  margin: 0;
  font: 15px/1.5 var(--sans);
  color: var(--text);
  background:
    linear-gradient(var(--bg-grid) 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px) 0 0 / 24px 24px,
    var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
.mono { font-family: var(--mono); }
.dim { color: var(--muted); }
h2 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); margin: 0; font-weight: 600; }
button, input, select { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px clamp(16px, 3vw, 32px);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font: 700 1.1rem var(--mono); }
.brand-mark {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-size: 0.85rem;
}
.topbar-actions { display: flex; gap: 8px; }
.ico { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#book-toggle[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
@media (max-width: 560px) { .btn-text { display: none; } .topbar-actions .btn { padding-inline: 11px; } }

/* Layout */
/* <main>'s children join the page grid so the date book can sit between
   the hero and the formats on small screens. */
.layout {
  display: grid; gap: 20px;
  grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-areas: "controls book" "hero book" "content book";
  grid-template-rows: auto auto 1fr;
  max-width: 2400px; margin: 0 auto;
  padding: 20px clamp(16px, 3vw, 32px) 40px;
}
main { display: contents; }
.controls { grid-area: controls; }
.hero { grid-area: hero; }
.content { grid-area: content; min-width: 0; display: flex; flex-direction: column; gap: 20px; }
.sidebar { grid-area: book; position: sticky; top: 80px; align-self: start; max-height: calc(100vh - 100px); display: flex; }
@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "controls" "hero" "book" "content"; grid-template-rows: none; }
  .sidebar { position: static; max-height: none; }
}
/* Folded-away date book: formats take the full width. */
.layout.book-hidden { grid-template-columns: minmax(0, 1fr); grid-template-areas: "controls" "hero" "content"; }
.layout.book-hidden .sidebar { display: none; }
.layout:not(.book-hidden) .sidebar { animation: book-in 0.2s ease-out; }
@keyframes book-in { from { opacity: 0; transform: translateX(12px); } }
@media (max-width: 960px) { @keyframes book-in { from { opacity: 0; transform: translateY(-8px); } } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Buttons & inputs */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid transparent;
  background: var(--accent); color: var(--accent-ink); font-weight: 600; cursor: pointer;
  transition: transform 0.08s, background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); font-weight: 500; }
.btn.ghost:hover { background: var(--surface-2); filter: none; }
.btn.small { padding: 6px 10px; font-size: 0.85rem; }
.btn.icon { width: 38px; padding: 0; height: 38px; font-size: 1.1rem; }
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; }

input[type="text"], input[type="date"], input[type="time"], select {
  width: 100%; padding: 9px 12px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border);
  font-family: var(--mono); font-size: 0.95rem; min-height: 40px;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.input-row { display: flex; gap: 8px; align-items: center; }

/* Controls */
.controls { display: flex; flex-wrap: wrap; gap: 14px; padding: 16px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 6px; flex: 1 1 160px; min-width: 0; }
.field.grow { flex: 2 1 220px; }
.field.actions { flex: 0 0 auto; }
.field label { font-size: 0.78rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.08em; }
#now-btn { min-height: 40px; }
#now-btn[aria-pressed="false"] { background: transparent; color: var(--text); border-color: var(--border); }
.pulse { width: 8px; height: 8px; border-radius: 50%; background: currentColor; opacity: 0.5; }
#now-btn[aria-pressed="true"] .pulse { opacity: 1; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (max-width: 560px) { .field.actions, .field.actions .btn { width: 100%; flex-basis: 100%; } }

/* Hero */
.hero {
  padding: clamp(20px, 4vw, 40px);
  text-align: center;
  background:
    radial-gradient(120% 140% at 50% 0%, color-mix(in srgb, var(--accent) 14%, transparent), transparent 60%),
    var(--surface);
  overflow: hidden;
}
.hero-label { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.14em; font-weight: 600; }
.hero-hex {
  font-size: clamp(2.2rem, 9vw, 5.5rem); font-weight: 700; line-height: 1.1; margin: 8px 0 10px;
  color: var(--accent); letter-spacing: -0.02em; word-break: break-all;
  font-variant-numeric: tabular-nums;
}
.hero-sub { display: flex; flex-wrap: wrap; gap: 6px 12px; justify-content: center; font-size: clamp(0.9rem, 2.4vw, 1.15rem); }
.copy-inline { background: none; border: 0; padding: 2px 6px; border-radius: 6px; cursor: copy; font-family: var(--mono); }
.copy-inline:hover { background: var(--surface-2); }

/* Section nav */
.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chips a {
  flex: none; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border);
  color: var(--muted); text-decoration: none; font-size: 0.85rem; background: var(--surface);
}
.chips a:hover { color: var(--text); border-color: var(--accent); }

/* Format sections */
#sections { display: flex; flex-direction: column; gap: 28px; }
.section h2 { margin: 0 0 12px 2px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); }
.item {
  position: relative; padding: 14px 16px; display: flex; flex-direction: column; gap: 4px;
  min-width: 0; transition: border-color 0.15s;
}
.item:hover { border-color: color-mix(in srgb, var(--accent) 60%, var(--border)); }
.item-label { font-size: 0.8rem; color: var(--muted); font-weight: 500; padding-right: 56px; }
.item-value {
  font-family: var(--mono); font-size: 1.02rem; font-weight: 500; overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}
.item-value.na { color: var(--muted); font-style: italic; font-family: var(--sans); font-weight: 400; }
.item-note { font-size: 0.78rem; color: var(--muted); }
.copy-btn {
  position: absolute; top: 10px; right: 10px;
  padding: 3px 8px; border-radius: 6px; font-size: 0.72rem; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted);
  opacity: 0; transition: opacity 0.15s;
}
.item:hover .copy-btn, .copy-btn:focus-visible { opacity: 1; }
@media (hover: none) { .copy-btn { opacity: 1; } }
.swatch-row { display: flex; align-items: center; gap: 10px; }
.swatch { width: 28px; height: 28px; border-radius: 8px; border: 1px solid var(--border); flex: none; }
.progress { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin-top: 4px; }
.progress > span { display: block; height: 100%; background: linear-gradient(90deg, var(--accent-2), var(--accent)); }

/* Hero binary clock */
.bclock-wrap { margin: 6px auto 18px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.bclock-wrap[hidden] { display: none; }
.bclock-wrap figcaption { font-size: 0.78rem; }
.bclock {
  --cell: clamp(20px, 6.4vw, 34px);
  display: flex; gap: calc(var(--cell) * 0.32); align-items: flex-start;
  padding: calc(var(--cell) * 0.5) calc(var(--cell) * 0.6);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--bg) 55%, transparent);
  border: 1px solid var(--border);
}
.bc-col { display: flex; flex-direction: column; align-items: center; gap: calc(var(--cell) * 0.28); }
.bc-sep { width: calc(var(--cell) * 0.35); }
.bc-bit {
  display: block; width: var(--cell); height: var(--cell); border-radius: 28%;
  background: var(--surface-2); border: 1px solid var(--border);
  transition: background 0.18s, box-shadow 0.18s, border-color 0.18s;
}
.bc-bit.void { visibility: hidden; }
.bc-bit.on {
  background: var(--accent); border-color: var(--accent);
  box-shadow: 0 0 calc(var(--cell) * 0.55) color-mix(in srgb, var(--accent) 65%, transparent);
}
.bc-w, .bc-digit {
  height: var(--cell); display: grid; place-items: center;
  font: 500 calc(var(--cell) * 0.5) var(--mono); color: var(--muted);
}
.bc-weights { margin-right: calc(var(--cell) * 0.1); }
.bc-weights .bc-w { font-size: calc(var(--cell) * 0.4); opacity: 0.7; }
.bc-col:not(.bc-weights) .bc-digit { color: var(--text); font-weight: 700; }

/* Date book */
.book { width: 100%; padding: 16px; display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.book-head { display: flex; align-items: center; justify-content: space-between; margin-top: -4px; }
.book-head h2 { display: flex; align-items: center; gap: 8px; }
.badge { font: 600 0.75rem var(--mono); padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.book-add { display: flex; gap: 8px; }
.book-list { list-style: none; margin: 0 -6px; padding: 0; overflow-y: auto; min-height: 0; flex: 1 1 auto; }
@media (max-width: 960px) { .book-list { max-height: 260px; } }
.book-entry { display: flex; align-items: center; gap: 4px; border-radius: 10px; }
.book-entry:hover, .book-entry.active { background: var(--surface-2); }
.book-entry.active { box-shadow: inset 3px 0 0 var(--accent); }
.book-load {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; cursor: pointer;
  padding: 8px 10px; border-radius: 10px; display: flex; flex-direction: column; gap: 1px;
}
.book-load b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-load small { font: 0.78rem var(--mono); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-del {
  flex: none; width: 32px; height: 32px; margin-right: 4px; border-radius: 8px;
  background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 1rem;
}
.book-del:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.empty { color: var(--muted); font-size: 0.88rem; margin: 0; }
.error { color: var(--danger); font-size: 0.85rem; margin: 0; }
.sync { border-top: 1px solid var(--border); padding-top: 10px; font-size: 0.88rem; }
.sync summary { cursor: pointer; color: var(--muted); font-weight: 500; }
.sync p { color: var(--muted); margin: 8px 0; }
.sync .input-row { margin-top: 8px; }
.sync input { font-size: 0.8rem; }

.footer {
  display: flex; flex-wrap: wrap; gap: 6px 16px; justify-content: center;
  padding: 0 16px 32px; font-size: 0.82rem; color: var(--muted); text-align: center;
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  padding: 10px 16px; border-radius: 10px; background: var(--text); color: var(--bg);
  font-weight: 500; font-size: 0.9rem; opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; z-index: 50; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  html { scroll-behavior: auto; }
}

/* The book stacks vertically on narrow screens, so "fold away" points up. */
@media (max-width: 960px) { #book-close .ico { transform: rotate(-90deg); } }
