*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --bg: oklch(17% 0.018 258);
  --surface: oklch(22% 0.022 258);
  --card: oklch(25.5% 0.026 257);
  --border: oklch(33% 0.032 257);

  --indigo: oklch(70% 0.16 275);
  --green: oklch(74% 0.16 152);
  --rose: oklch(72% 0.17 20);
  --amber: oklch(80% 0.15 80);

  --text: oklch(93% 0.012 258);
  --muted: oklch(70% 0.022 258);

  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,.4);
  --modal-shadow: 0 24px 64px rgba(0,0,0,.55);
  --ease-out-expo: cubic-bezier(0.16, 1, 0.3, 1);
}
html[data-theme="light"] {
  --bg: oklch(96.5% 0.008 250);
  --surface: oklch(99% 0.004 250);
  --card: oklch(100% 0 0);
  --border: oklch(87% 0.014 250);

  --indigo: oklch(52% 0.15 275);
  --green: oklch(54% 0.13 152);
  --rose: oklch(58% 0.16 20);
  --amber: oklch(58% 0.15 70);

  --text: oklch(24% 0.02 258);
  --muted: oklch(46% 0.024 258);
  --shadow: 0 2px 16px rgba(28,32,51,.08);
  --modal-shadow: 0 24px 64px rgba(28,32,51,.22);
}

html { color-scheme: light dark; }
html, body { overflow-x: hidden; }
body {
  font-family: 'Segoe UI Variable', 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--text);
  min-height: 100vh; padding: 56px 16px 32px;
  display: flex; flex-direction: column; align-items: center;
  transition: background-color .2s, color .2s;
  -webkit-font-smoothing: antialiased;
}
::selection { background: color-mix(in oklch, var(--indigo) 30%, transparent); }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible, a:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px color-mix(in oklch, var(--indigo) 35%, transparent);
  border-radius: var(--radius-sm);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

.theme-toggle {
  position: fixed; top: 14px; right: 14px; z-index: 100;
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--border); border-radius: 50%;
  color: var(--muted);
  transition: background-color .2s, border-color .2s, color .15s;
}
.theme-toggle:hover { color: var(--text); }
.theme-toggle svg { width: 17px; height: 17px; }
.icon-sun { display: none; }
.icon-moon { display: block; }
html[data-theme="light"] .icon-sun { display: block; }
html[data-theme="light"] .icon-moon { display: none; }

header { text-align: center; margin-bottom: 28px; max-width: 640px; animation: riseIn .5s var(--ease-out-expo) both; }
header h1 { font-size: clamp(1.5rem, 4.5vw, 1.9rem); font-weight: 800; letter-spacing: -0.6px; }
header p { color: var(--muted); font-size: .92rem; margin-top: 10px; line-height: 1.55; }

main {
  width: 100%; max-width: 1080px;
  display: flex; flex-direction: column; gap: 18px;
  animation: riseIn .5s .05s var(--ease-out-expo) both;
}

.controls {
  width: 100%; max-width: 700px; margin: 0 auto;
  display: flex; flex-direction: column; gap: 12px;
}

.search-bar {
  position: relative; display: flex; align-items: center;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 4px 6px 4px 16px;
  transition: border-color .15s, box-shadow .15s;
}
.search-bar:focus-within {
  border-color: var(--indigo);
  box-shadow: 0 0 0 3px color-mix(in oklch, var(--indigo) 35%, transparent);
}
.search-icon { width: 18px; height: 18px; color: var(--muted); flex-shrink: 0; }
.search-bar input {
  flex: 1; min-width: 0; border: none; background: transparent; outline: none;
  padding: 13px 10px; font-size: 1rem; color: var(--text);
}
/* The whole pill rings on focus-within instead — a ring on the bare input
   would draw a second, smaller box nested inside the pill. */
.search-bar input:focus-visible { box-shadow: none; }
.search-bar input::placeholder { color: var(--muted); }
.clear-btn {
  border: none; background: var(--surface); color: var(--muted);
  width: 38px; height: 38px; border-radius: 50%; font-size: 1.2rem; line-height: 1;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: background-color .15s, color .15s;
}
.clear-btn:hover { color: var(--text); background: var(--border); }

.filter-row { display: flex; flex-wrap: wrap; gap: 8px; }
.scroll-fade { position: relative; margin: 0 -16px; padding: 0 16px; }
.scroll-arrow {
  display: none; /* shown only for mouse/trackpad pointers, see media query below */
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 2;
  width: 30px; height: 30px; border-radius: 50%;
  align-items: center; justify-content: center;
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  box-shadow: var(--shadow);
  transition: background-color .15s, transform .12s var(--ease-out-expo);
}
.scroll-arrow svg { width: 15px; height: 15px; }
.scroll-arrow:hover { background: var(--surface); }
.scroll-arrow:active { transform: translateY(-50%) scale(.92); }
.scroll-arrow-left { left: 2px; }
.scroll-arrow-right { right: 2px; }
@media (hover: hover) and (pointer: fine) {
  .scroll-arrow { display: flex; }
}
.filter-row.scroll-x {
  flex-wrap: nowrap; overflow-x: auto; overscroll-behavior-x: contain;
  padding-bottom: 2px; padding-top: 2px;
  scrollbar-width: none; -ms-overflow-style: none;
  /* Fade the last ~10% of chips toward transparent so a scrollable row never
     looks like it's abruptly cut off at the viewport edge. */
  -webkit-mask-image: linear-gradient(to right, transparent, black 16px, black calc(100% - 40px), transparent);
  mask-image: linear-gradient(to right, transparent, black 16px, black calc(100% - 40px), transparent);
}
.filter-row.scroll-x::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  min-height: 40px;
  padding: 8px 16px; border-radius: 999px; font-size: .84rem; font-weight: 600;
  white-space: nowrap;
  transition: transform .12s var(--ease-out-expo), border-color .15s, background-color .15s, color .15s;
}
.chip:hover { color: var(--text); border-color: color-mix(in oklch, var(--indigo) 40%, var(--border)); }
.chip:active { transform: scale(.96); }
.chip.active {
  background: color-mix(in oklch, var(--indigo) 16%, var(--card));
  border-color: var(--indigo); color: var(--indigo);
}
.chip .count { opacity: .7; font-weight: 500; margin-left: 4px; }

.status-line { font-size: .8rem; color: var(--muted); padding: 0 2px; min-height: 1em; }

.results-list {
  display: grid; grid-template-columns: 1fr; gap: 12px;
  width: 100%; max-width: 700px; margin: 0 auto;
}

.rule-card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 16px 18px; text-align: left; width: 100%; height: 100%;
  display: flex; flex-direction: column; gap: 6px;
  transition: transform .15s var(--ease-out-expo), border-color .15s, box-shadow .15s;
  animation: riseIn .35s var(--ease-out-expo) both;
}
.rule-card:hover { border-color: color-mix(in oklch, var(--indigo) 35%, var(--border)); box-shadow: var(--shadow); transform: translateY(-1px); }
.rule-card:active { transform: translateY(0) scale(.995); }

.rule-card-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.badge {
  font-size: .68rem; font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 6px; flex-shrink: 0;
}
.badge-ppr { background: color-mix(in oklch, var(--indigo) 18%, transparent); color: var(--indigo); }
.badge-csr { background: color-mix(in oklch, var(--green) 18%, transparent); color: var(--green); }
.rule-no { font-size: .78rem; font-weight: 700; color: var(--muted); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.rule-chapter {
  font-size: .74rem; color: var(--muted); margin-left: auto;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 50%;
}

.rule-title { font-size: 1rem; font-weight: 700; line-height: 1.35; }
.rule-snippet {
  font-size: .87rem; color: var(--muted); line-height: 1.5;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.rule-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 4px; }
.tag-pill {
  font-size: .68rem; color: var(--muted); background: var(--surface);
  border: 1px solid var(--border); padding: 2px 8px; border-radius: 999px;
}

.load-more-wrap { display: flex; justify-content: center; padding: 8px 0 4px; }
.btn {
  padding: 12px 24px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-weight: 600; font-size: .9rem; min-height: 44px;
  transition: border-color .15s, background-color .15s, transform .12s var(--ease-out-expo);
}
.btn:hover { border-color: color-mix(in oklch, var(--indigo) 40%, var(--border)); }
.btn:active { transform: scale(.97); }

.empty-state {
  text-align: center; padding: 40px 20px; color: var(--muted); font-size: .92rem;
  max-width: 700px; margin: 0 auto; width: 100%;
}

.skeleton-card {
  height: 92px; border-radius: var(--radius-sm); background: var(--card);
  border: 1px solid var(--border); position: relative; overflow: hidden;
}
.skeleton-card::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in oklch, var(--border) 60%, transparent), transparent);
  animation: shimmer 1.3s infinite;
}

footer { max-width: 700px; text-align: center; margin-top: 36px; padding: 0 12px; }
footer p { font-size: .74rem; color: var(--muted); line-height: 1.6; }

/* Detail overlay */
.detail-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: color-mix(in oklch, black 55%, transparent);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; overflow-y: auto;
  animation: fadeIn .18s ease both;
}
.detail-panel {
  position: relative; width: 100%; max-width: 660px;
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--modal-shadow);
  padding: 32px 28px 28px;
  animation: riseIn .3s var(--ease-out-expo) both;
}
.detail-close {
  position: fixed; top: 14px; right: 14px; z-index: 210;
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--card); color: var(--muted); font-size: 1.3rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow);
}
.detail-close:hover { color: var(--text); }

.detail-content .rule-card-top { margin-bottom: 10px; }
.detail-content h2 { font-size: 1.25rem; font-weight: 800; line-height: 1.35; margin-bottom: 4px; }
.detail-content .breadcrumb { font-size: .78rem; color: var(--muted); margin-bottom: 18px; }

.detail-block { margin-bottom: 20px; }
.detail-block-label {
  font-size: .7rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 8px; display: flex; align-items: center; gap: 6px;
}
.detail-block-label.original::before { content: "§"; color: var(--indigo); font-weight: 800; }
.detail-block-label.explain::before { content: "💡"; }
.detail-original {
  font-size: .92rem; line-height: 1.7; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 16px 18px; white-space: pre-wrap; font-family: 'Georgia', 'Cambria', serif;
}
.detail-explain { font-size: .94rem; line-height: 1.7; color: var(--text); }

.detail-links { margin-top: 8px; }
.detail-links a {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: .84rem; font-weight: 600; color: var(--indigo);
  text-decoration: none; padding: 8px 0;
}
.detail-links a:hover { text-decoration: underline; }

@keyframes riseIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* Tablet and up: two-column result grid, roomier controls */
@media (min-width: 760px) {
  .results-list { grid-template-columns: repeat(2, 1fr); max-width: 1080px; }
  .empty-state { max-width: 1080px; }
}

/* Wide desktop: three-column result grid */
@media (min-width: 1180px) {
  .results-list { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 480px) {
  body { padding: 48px 12px 28px; }
  header h1 { font-size: 1.5rem; }
  .scroll-fade { margin: 0 -12px; padding: 0 12px; }

  /* Full-screen sheet for the detail view on small screens */
  .detail-overlay { padding: 0; align-items: stretch; }
  .detail-panel {
    max-width: 100%; min-height: 100%; border-radius: 0;
    padding: 64px 18px 32px;
  }
}
