/* Penal code site. Tokens first, then layout, then components. */
:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --ink: #16202e;
  --muted: #566176;
  --line: #d8dee8;
  --accent: #23477a;
  --accent-ink: #ffffff;
  --focus: #1b6fd1;
  --c-infraction: #1f6f8f;
  --c-misdemeanor: #96620f;
  --c-felony: #b0293a;
  --c-federal: #5b3a8c;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: "Source Serif 4", Georgia, "Times New Roman", serif;
  --topbar: 58px;
  --side: 288px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e141d;
    --surface: #151d29;
    --ink: #e6ebf2;
    --muted: #97a3b6;
    --line: #263143;
    --accent: #86abe0;
    --accent-ink: #0e141d;
    --focus: #8ab8f5;
    --c-infraction: #5cb8d9;
    --c-misdemeanor: #e0a94a;
    --c-felony: #ee7482;
    --c-federal: #b79ae6;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { scroll-padding-top: calc(var(--topbar) + 16px); }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 400 16px/1.55 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 3px; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
p { margin: 0 0 1em; }
code { font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace; background: color-mix(in srgb, var(--ink) 8%, transparent); padding: 0.1em 0.35em; border-radius: 4px; }
kbd { font: 500 0.72rem var(--sans); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 1px 6px; color: var(--muted); background: var(--surface); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: 8px; top: -50px; background: var(--surface); padding: 8px 12px; z-index: 100; border-radius: 6px; }
.skip:focus { top: 8px; }
.nowrap { white-space: nowrap; }

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 30; height: var(--topbar);
  display: flex; align-items: center; gap: 20px; padding: 0 20px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-weight: 600; font-size: 0.95rem; }
.brand-text span { font-size: 0.78rem; color: var(--muted); }
.topnav { display: flex; gap: 18px; margin-left: 12px; }
.topnav a { color: var(--muted); text-decoration: none; font-size: 0.92rem; padding: 6px 0; }
.topnav a:hover { color: var(--ink); }
.search-btn {
  margin-left: auto; display: inline-flex; align-items: center; gap: 8px;
  min-width: 210px; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--muted); text-decoration: none; font-size: 0.9rem;
}
.search-btn:hover { border-color: var(--muted); }
.search-btn kbd { margin-left: auto; }
.icon-btn { display: none; place-items: center; width: 40px; height: 40px; border: 0; background: transparent; color: var(--ink); border-radius: 8px; cursor: pointer; }
.icon-btn:hover { background: var(--bg); }

/* Shell */
.shell { display: flex; align-items: flex-start; max-width: 1360px; margin: 0 auto; }
.sidebar {
  position: sticky; top: var(--topbar); flex: 0 0 var(--side); width: var(--side);
  height: calc(100vh - var(--topbar)); overflow-y: auto; padding: 18px 12px 40px 20px;
  border-right: 1px solid var(--line); font-size: 0.9rem;
}
.main { flex: 1; min-width: 0; padding: 32px 40px 72px; max-width: 980px; }
.no-sidebar .shell { justify-content: center; }
.no-sidebar .main { margin: 0 auto; width: 100%; }
.is-wide .main { max-width: 1160px; }

/* Sidebar tree */
.side-list { list-style: none; margin: 0; padding: 0; }
.side-list .side-list { margin-left: 12px; padding-left: 10px; border-left: 1px solid var(--line); }
.side-link, .side-art { display: flex; justify-content: space-between; gap: 8px; padding: 6px 8px; border-radius: 6px; color: var(--ink); text-decoration: none; }
.side-link:hover, .side-art:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.side-link.is-current, .side-art.is-current { background: color-mix(in srgb, var(--accent) 16%, transparent); font-weight: 600; }
.side-top { font-weight: 600; margin-bottom: 4px; }
.side-count { color: var(--muted); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
.sidebar details > summary { list-style: none; cursor: pointer; }
.sidebar details > summary::-webkit-details-marker { display: none; }
.sidebar details > summary a { padding-left: 22px; position: relative; }
.sidebar details > summary a::before {
  content: ""; position: absolute; left: 8px; top: 50%; width: 5px; height: 5px;
  border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted);
  transform: translateY(-70%) rotate(-45deg); transition: transform 0.15s;
}
.sidebar details[open] > summary a::before { transform: translateY(-70%) rotate(45deg); }
.side-articles { list-style: none; margin: 2px 0 6px 12px; padding: 0 0 0 10px; border-left: 2px solid var(--accent); }
.side-art { font-size: 0.86rem; color: var(--muted); padding: 4px 8px; }

/* Page head */
.page-head { margin-bottom: 28px; }
.page-head h1 { font-family: var(--serif); font-weight: 600; font-size: clamp(1.9rem, 3.2vw, 2.6rem); letter-spacing: -0.01em; }
.lead { margin-top: 10px; font-size: 1.08rem; color: var(--muted); max-width: 62ch; }
.byline { display: flex; gap: 16px; margin: 10px 0 0; color: var(--muted); font-size: 0.88rem; }
.crumbs ol { display: flex; flex-wrap: wrap; gap: 6px; list-style: none; margin: 0 0 18px; padding: 0; font-size: 0.86rem; color: var(--muted); }
.crumbs li + li::before { content: "/"; margin-right: 6px; opacity: 0.6; }
.crumbs a { color: var(--muted); }

/* Home search field and legend */
.search-field {
  display: flex; align-items: center; gap: 12px; margin-top: 22px; padding: 14px 16px; max-width: 640px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  color: var(--muted); text-decoration: none; box-shadow: 0 1px 0 var(--line);
}
.search-field:hover { border-color: var(--accent); }
.search-field kbd { margin-left: auto; }
.legend { display: flex; flex-wrap: wrap; gap: 8px 22px; list-style: none; margin: 18px 0 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 8px; }
.legend-n { color: var(--muted); font-size: 0.88rem; }

/* Chips and class colors */
.cls-infraction { --cls: var(--c-infraction); }
.cls-misdemeanor { --cls: var(--c-misdemeanor); }
.cls-felony { --cls: var(--c-felony); }
.cls-federal { --cls: var(--c-federal); }
.chip {
  display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; white-space: nowrap;
  color: var(--cls); background: color-mix(in srgb, var(--cls) 13%, transparent); border: 1px solid color-mix(in srgb, var(--cls) 35%, transparent);
}

/* Index blocks */
.cat-block { margin-bottom: 40px; }
.cat-block h2 { font-family: var(--serif); font-size: 1.5rem; font-weight: 600; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.cat-block h3 { font-size: 1.05rem; margin: 22px 0 8px; font-weight: 600; }
.cat-block h2 a, .cat-block h3 a { color: var(--ink); text-decoration: none; }
.cat-block h2 a:hover, .cat-block h3 a:hover { text-decoration: underline; }
.count { margin-left: 10px; font: 400 0.82rem var(--sans); color: var(--muted); font-variant-numeric: tabular-nums; }
.cat-desc { margin: 10px 0 0; color: var(--muted); max-width: 64ch; }
.charge-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 6px 14px; }
.charge-list.single { grid-template-columns: 1fr; }
.charge-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px; background: var(--surface);
  border: 1px solid var(--line); border-left: 4px solid var(--cls, var(--line)); border-radius: 6px;
  color: var(--ink); text-decoration: none;
}
.charge-row:hover { border-color: var(--cls, var(--accent)); background: color-mix(in srgb, var(--cls, var(--accent)) 6%, var(--surface)); }
.charge-row .code { flex: 0 0 auto; min-width: 2.6em; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.88rem; }
.charge-row .name { flex: 1; min-width: 0; }
.subcats { list-style: none; margin: 0 0 28px; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 8px; }
.subcats a { display: flex; justify-content: space-between; padding: 10px 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); text-decoration: none; }
.subcats a:hover { border-color: var(--accent); }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
table.charges { width: 100%; border-collapse: collapse; font-size: 0.94rem; }
.charges th { text-align: left; font-weight: 600; font-size: 0.84rem; color: var(--muted); padding: 10px 14px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--ink) 3%, var(--surface)); }
.charges td { padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.charges tr:last-child td { border-bottom: 0; }
.charges td.code { color: var(--muted); font-variant-numeric: tabular-nums; width: 4.5em; }
.charges tbody tr:hover { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.charges tbody tr.cls-infraction td:first-child, .charges tbody tr.cls-misdemeanor td:first-child,
.charges tbody tr.cls-felony td:first-child, .charges tbody tr.cls-federal td:first-child { box-shadow: inset 4px 0 0 var(--cls); }

/* Article and charge sheet */
.article { max-width: 760px; }
.charge-sheet {
  display: block; margin: 0 0 26px; background: var(--surface); border: 1px solid var(--line);
  border-left: 8px solid var(--cls); border-radius: 8px; overflow: hidden;
}
.sheet-top { display: flex; justify-content: space-between; align-items: center; padding: 12px 18px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--cls) 7%, var(--surface)); }
.sheet-code { font-weight: 600; font-variant-numeric: tabular-nums; }
.sheet-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); margin: 0; }
.sheet-grid > div { padding: 14px 18px; border-right: 1px solid var(--line); }
.sheet-grid > div:last-child { border-right: 0; }
.sheet-grid dt { font-size: 0.82rem; color: var(--muted); }
.sheet-grid dd { margin: 2px 0 0; font-size: 1.2rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.summary { font-family: var(--serif); font-size: 1.2rem; line-height: 1.5; color: var(--ink); margin-bottom: 1.4em; }
.prose { font-family: var(--serif); font-size: 1.06rem; line-height: 1.7; max-width: 70ch; }
.prose h3 { font-family: var(--sans); font-size: 1.1rem; margin: 1.8em 0 0.5em; }
.prose ul, .prose ol { padding-left: 1.4em; margin: 0 0 1em; }
.prose li { margin-bottom: 0.35em; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 44px; max-width: 760px; }
.pager a { display: flex; flex-direction: column; gap: 2px; padding: 12px 16px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); text-decoration: none; }
.pager a:hover { border-color: var(--accent); }
.pager small { color: var(--muted); }
.pager .next { text-align: right; grid-column: 2; }
.pager .prev { grid-column: 1; }

/* Misc */
.empty { padding: 28px; border: 1px dashed var(--line); border-radius: 10px; color: var(--muted); background: var(--surface); }
.empty h2 { color: var(--ink); font-size: 1.15rem; margin-bottom: 6px; }
.result-count { color: var(--muted); }
.search-form { display: flex; gap: 10px; margin-top: 18px; max-width: 560px; }
.footer { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; max-width: 1360px; margin: 0 auto; padding: 20px; border-top: 1px solid var(--line); color: var(--muted); font-size: 0.85rem; }

/* Buttons and fields */
.btn {
  display: inline-block; padding: 9px 16px; border: 1px solid var(--accent); border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font: 500 0.92rem var(--sans); text-decoration: none; cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }
.btn-quiet { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-quiet:hover { border-color: var(--muted); filter: none; }
input[type=text], input[type=search], input[type=password], input[type=number], select, textarea {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink); font: inherit;
}
textarea { resize: vertical; line-height: 1.5; }

/* Search palette */
dialog#palette { width: min(640px, calc(100vw - 24px)); margin: 12vh auto auto; padding: 0; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35); }
dialog#palette::backdrop { background: rgba(8, 12, 20, 0.55); }
.palette-field { display: flex; align-items: center; gap: 10px; padding: 0 16px; border-bottom: 1px solid var(--line); color: var(--muted); }
.palette-field input { border: 0; background: transparent; padding: 16px 0; font-size: 1.05rem; box-shadow: none; }
.palette-field input:focus-visible { outline: none; }
#palette-results { list-style: none; margin: 0; padding: 6px; max-height: 52vh; overflow-y: auto; }
#palette-results li a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 8px; text-decoration: none; color: var(--ink); }
#palette-results li.is-active a, #palette-results li a:hover { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.pal-code { min-width: 2.6em; color: var(--muted); font-size: 0.86rem; font-variant-numeric: tabular-nums; }
.pal-main { flex: 1; min-width: 0; }
.pal-title { display: block; }
.pal-sub { display: block; color: var(--muted); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pal-empty { padding: 18px 14px; color: var(--muted); }
.palette-hint { margin: 0; padding: 10px 16px; border-top: 1px solid var(--line); font-size: 0.8rem; color: var(--muted); }

/* Calculator */
.calc { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 28px; align-items: start; }
.calc-filter { margin-bottom: 16px; }
.calc-group { border: 0; padding: 0; margin: 0 0 22px; }
.calc-group legend { font-weight: 600; padding: 0 0 8px; }
.calc-item {
  display: grid; grid-template-columns: auto 1fr auto; gap: 2px 12px; align-items: center; padding: 9px 12px; margin-bottom: 5px;
  background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--cls); border-radius: 6px; cursor: pointer;
}
.calc-item:hover { background: color-mix(in srgb, var(--cls) 6%, var(--surface)); }
.calc-item input { grid-row: span 2; width: 18px; height: 18px; accent-color: var(--accent); }
.calc-item .chip { grid-row: span 2; grid-column: 3; }
.calc-pen { color: var(--muted); font-size: 0.84rem; }
.calc-item:has(input:checked) { border-color: var(--cls); background: color-mix(in srgb, var(--cls) 10%, var(--surface)); }
.calc-panel { position: sticky; top: calc(var(--topbar) + 16px); padding: 18px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); }
.calc-panel h2 { font-family: var(--serif); font-size: 1.3rem; margin-bottom: 10px; }
.calc-empty { color: var(--muted); }
.calc-selected { margin: 0 0 14px; padding-left: 1.3em; font-size: 0.92rem; max-height: 220px; overflow-y: auto; }
.calc-totals { margin: 0 0 16px; border-top: 1px solid var(--line); }
.calc-totals > div { display: flex; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.calc-totals dt { color: var(--muted); }
.calc-totals dd { margin: 0; font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.calc-actions { display: flex; gap: 8px; }
.calc-status { margin: 10px 0 0; color: var(--muted); font-size: 0.86rem; min-height: 1.3em; }

/* Admin */
.admin { max-width: 1060px; margin: 0 auto; }
.admin.narrow { max-width: 460px; }
.admin h1 { font-family: var(--serif); font-size: 1.9rem; margin-bottom: 16px; }
.admin-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 22px; border-bottom: 1px solid var(--line); }
.admin-nav { display: flex; gap: 6px; }
.admin-nav a { padding: 10px 14px; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.admin-nav a[aria-current] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.admin-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.admin-head h1 { margin: 0; }
.filters { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.filters input, .filters select { width: auto; flex: 1 1 200px; }
.row-actions { display: flex; gap: 14px; justify-content: flex-end; align-items: center; white-space: nowrap; }
.row-actions form { margin: 0; }
.link-danger { border: 0; background: none; color: var(--c-felony); font: inherit; cursor: pointer; text-decoration: underline; padding: 0; }
.flash { padding: 10px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); margin-bottom: 16px; }
.flash-ok { border-color: color-mix(in srgb, #2e8b57 50%, var(--line)); }
.flash-error { border-color: var(--c-felony); color: var(--c-felony); }
.stack { display: grid; gap: 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 20px; align-items: start; }
.field { display: grid; gap: 5px; }
.field > span, .penalty-fields legend { font-weight: 600; font-size: 0.9rem; }
.field small { color: var(--muted); font-size: 0.82rem; }
.field-wide { grid-column: 1 / -1; }
.penalty-fields { border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px 16px; display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; }
.penalty-fields legend { padding: 0 6px; }
.form-actions { display: flex; gap: 10px; }
.code-block { padding: 14px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); overflow-x: auto; font: 0.85rem ui-monospace, Menlo, monospace; white-space: pre-wrap; word-break: break-all; }

/* Responsive */
.scrim { display: none; }
@media (max-width: 1000px) {
  .calc { grid-template-columns: 1fr; }
  .calc-panel { position: static; order: -1; }
}
@media (max-width: 860px) {
  .icon-btn { display: grid; }
  .topnav { display: none; }
  .topbar { padding: 0 12px; gap: 10px; }
  .search-btn { min-width: 0; }
  .search-btn span, .search-btn kbd { display: none; }
  .sidebar {
    position: fixed; z-index: 40; left: 0; top: var(--topbar); bottom: 0; height: auto; width: min(320px, 86vw);
    background: var(--surface); transform: translateX(-102%); transition: transform 0.2s ease;
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: var(--topbar) 0 0 0; z-index: 35; background: rgba(8, 12, 20, 0.5); }
  .main { padding: 22px 16px 56px; }
  .charge-list { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .sheet-grid > div { border-right: 0; border-bottom: 1px solid var(--line); }
  .sheet-grid > div:last-child { border-bottom: 0; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

.charge-row .code-none { display: none; }

/* Admin panels */
.panel { padding: 20px; margin-bottom: 20px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); max-width: 720px; }
.panel h2 { font-family: var(--serif); font-size: 1.25rem; margin-bottom: 8px; }
.radio { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 8px; cursor: pointer; }
.radio input { margin-top: 5px; accent-color: var(--accent); }
input[type=file] { padding: 8px; border: 1px dashed var(--line); border-radius: 8px; background: var(--bg); width: 100%; color: var(--ink); }
