/* HuanLog Math Tools v2 — design system.
   Dependency-free, fast, mobile-first. Light + dark themes via [data-theme]. */
:root {
  color-scheme: light;
  --bg: #fbfbfe;
  --surface: #ffffff;
  --surface-2: #f4f5fa;
  --ink: #171a26;
  --muted: #5d6376;
  --faint: #8b91a5;
  --line: #e4e6ef;
  --accent: #4f46e5;
  --accent-ink: #4338ca;
  --accent-soft: #eef0fe;
  --good: #0e9f6e;
  --good-soft: #e6f7f0;
  --bad: #dc2626;
  --bad-soft: #fdecec;
  --warn: #b45309;
  --warn-soft: #fef3e2;
  --radius: 14px;
  --radius-s: 9px;
  --max: 1020px;
  --shadow: 0 1px 2px rgba(23, 26, 38, .05), 0 8px 28px rgba(23, 26, 38, .07);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          Arial, "Noto Sans", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1016;
  --surface: #161a24;
  --surface-2: #1d2230;
  --ink: #e9ebf4;
  --muted: #a6abc0;
  --faint: #7c8299;
  --line: #262c3d;
  --accent: #818cf8;
  --accent-ink: #a5b4fc;
  --accent-soft: #232a4d;
  --good: #34d399;
  --good-soft: #123a2c;
  --bad: #f87171;
  --bad-soft: #3d1a1a;
  --warn: #fbbf24;
  --warn-soft: #3a2c12;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 10px 32px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }
body {
  margin: 0; font-family: var(--font); color: var(--ink); background: var(--bg);
  line-height: 1.65; font-size: 16px;
}
a { color: var(--accent-ink); text-decoration: none; }
html[data-theme="dark"] a { color: var(--accent); }
a:hover { text-decoration: underline; }
::selection { background: var(--accent-soft); }

/* ---------- header ---------- */
.site-header {
  position: sticky; top: 0; z-index: 50;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.header-inner {
  display: flex; align-items: center; gap: 20px;
  max-width: var(--max); margin: 0 auto; padding: 10px 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800;
  font-size: 1.12rem; color: var(--ink); letter-spacing: -0.01em; }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 10px; font-size: 1.25rem;
  color: #fff; background: linear-gradient(135deg, #4f46e5, #7c3aed);
  box-shadow: 0 4px 12px rgba(79, 70, 229, .35);
}
.brand em { font-style: normal; font-weight: 600; color: var(--muted); font-size: .95rem; }
nav.main { margin-left: auto; display: flex; align-items: center; gap: 4px; }
nav.main a {
  color: var(--muted); font-weight: 600; font-size: .93rem;
  padding: 8px 12px; border-radius: 8px;
}
nav.main a:hover { color: var(--ink); background: var(--surface-2); text-decoration: none; }
nav.main a.active { color: var(--accent-ink); background: var(--accent-soft); }
.theme-toggle {
  border: 1px solid var(--line); background: var(--surface); color: var(--muted);
  width: 38px; height: 38px; border-radius: 10px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; margin: 0 0 0 6px; padding: 0;
}
.theme-toggle:hover { color: var(--ink); border-color: var(--faint); }
.theme-toggle svg { width: 18px; height: 18px; }
.theme-toggle .moon { display: none; }
html[data-theme="dark"] .theme-toggle .sun { display: none; }
html[data-theme="dark"] .theme-toggle .moon { display: block; }

/* ---------- layout ---------- */
.wrap { max-width: var(--max); margin: 0 auto; padding: 0 20px; }
.crumbs { font-size: .85rem; color: var(--faint); padding: 22px 0 0; }
.crumbs a { color: var(--muted); }

/* ---------- hero ---------- */
.hero { padding: 64px 0 30px; text-align: center; }
.hero h1 {
  font-size: clamp(2rem, 5vw, 3rem); margin: 0 0 14px; letter-spacing: -0.03em; line-height: 1.15;
}
.hero h1 .grad {
  background: linear-gradient(120deg, #4f46e5, #7c3aed 60%, #2563eb);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.hero p { color: var(--muted); font-size: 1.12rem; max-width: 660px; margin: 0 auto 26px; }
.tool-search { max-width: 480px; margin: 0 auto; position: relative; }
.tool-search input {
  width: 100%; padding: 13px 18px 13px 44px; font-size: 1rem;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--ink);
}
.tool-search svg { position: absolute; left: 16px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; color: var(--faint); }

/* ---------- tool grid & cards ---------- */
.section-title { font-size: 1.35rem; letter-spacing: -0.01em; margin: 44px 0 4px; }
.section-sub { color: var(--muted); margin: 0 0 20px; }
.tool-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 18px; padding: 6px 0 10px;
}
.card {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 22px; color: var(--ink);
  transition: transform .16s ease, box-shadow .16s ease, border-color .16s ease;
}
.card:hover { transform: translateY(-3px); box-shadow: var(--shadow);
  border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); text-decoration: none; }
.card-top { display: flex; align-items: center; gap: 12px; }
.card-icon {
  flex: 0 0 auto; width: 44px; height: 44px; border-radius: 12px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent-soft); color: var(--accent-ink);
}
html[data-theme="dark"] .card-icon { color: var(--accent); }
.card-icon svg { width: 24px; height: 24px; }
.card h2, .card h3 { font-size: 1.08rem; margin: 0; letter-spacing: -0.01em; }
.card p { color: var(--muted); font-size: .93rem; margin: 0; flex: 1; }
.card .tag {
  align-self: flex-start; font-size: .72rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  color: var(--accent-ink); background: var(--accent-soft);
  border-radius: 999px; padding: 3px 10px;
}
html[data-theme="dark"] .card .tag { color: var(--accent); }
.card .go { font-size: .88rem; font-weight: 700; color: var(--accent-ink); }
html[data-theme="dark"] .card .go { color: var(--accent); }

/* ---------- tool page head ---------- */
.tool-head { padding: 30px 0 6px; max-width: 760px; }
.tool-head h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); margin: 0 0 10px; letter-spacing: -0.02em; }
.tool-head p { color: var(--muted); margin: 0; font-size: 1.05rem; }

/* ---------- panels & forms ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 24px; margin: 20px 0; box-shadow: var(--shadow);
}
.panel h2 { font-size: 1.02rem; margin: 0 0 4px; }
.panel .hint { font-size: .88rem; color: var(--faint); margin: 0 0 8px; }
label { display: block; font-size: .88rem; font-weight: 650; margin: 14px 0 6px; color: var(--ink); }
input[type="text"], input[type="number"], select, textarea {
  width: 100%; padding: 10px 13px; font-size: 1rem; font-family: inherit;
  border: 1px solid var(--line); border-radius: var(--radius-s);
  background: var(--bg); color: var(--ink);
  transition: border-color .12s ease, box-shadow .12s ease;
}
textarea { font-family: var(--mono); font-size: .93rem; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}
.row { display: flex; gap: 14px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 170px; min-width: 0; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 18px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; cursor: pointer; font-family: inherit;
  background: linear-gradient(135deg, #4f46e5, #6d28d9); color: #fff;
  font-weight: 700; font-size: .98rem; padding: 11px 26px; border-radius: 11px;
  box-shadow: 0 4px 14px rgba(79, 70, 229, .32);
  transition: transform .1s ease, box-shadow .15s ease, filter .15s ease;
  margin: 0;
}
button:hover, .btn:hover { filter: brightness(1.07); box-shadow: 0 6px 18px rgba(79, 70, 229, .4); }
button:active, .btn:active { transform: translateY(1px); }
button.secondary {
  background: var(--surface); color: var(--ink); border: 1px solid var(--line); box-shadow: none;
}
button.secondary:hover { border-color: var(--faint); filter: none; }
button.ghost { background: transparent; color: var(--accent-ink); box-shadow: none; padding: 11px 14px; }
html[data-theme="dark"] button.ghost { color: var(--accent); }
button.small { padding: 8px 16px; font-size: .88rem; border-radius: 9px; }

/* matrix tables */
table.matrix { border-collapse: separate; border-spacing: 4px; margin: 8px 0; }
table.matrix input {
  width: 62px; text-align: center; padding: 9px 4px; font-family: var(--mono); font-size: .95rem;
}
.matrix-wrap { overflow-x: auto; }

/* ---------- results ---------- */
.result {
  border: 1px solid var(--line); border-left: 5px solid var(--accent);
  border-radius: var(--radius-s); background: var(--surface);
  padding: 18px 20px; margin: 18px 0; box-shadow: var(--shadow); overflow-x: auto;
}
.result h3 { margin: 0 0 10px; font-size: 1rem; display: flex; align-items: center; gap: 8px; }
.result .ok { color: var(--good); }
.result .err { color: var(--bad); }
.result pre {
  margin: 10px 0 0; font-family: var(--mono); font-size: .93rem; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word; color: var(--ink);
}
.result table.data { border-collapse: collapse; margin: 10px 0; font-size: .95rem; }
.result table.data th, .result table.data td {
  border: 1px solid var(--line); padding: 7px 14px; text-align: right; font-family: var(--mono);
}
.result table.data th { background: var(--surface-2); font-family: var(--font); text-align: left; }
.result table.data td:first-child, .result table.data th:first-child { text-align: left; }
.steps { font-size: .92rem; color: var(--muted); }
.steps ol, .steps ul { margin: 8px 0; padding-left: 22px; }
.steps li { margin: 4px 0; }
.steps code, .result code { font-family: var(--mono); font-size: .9em; }

/* canvas plots */
.plot-wrap {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin: 18px 0; box-shadow: var(--shadow);
}
.plot-wrap canvas { width: 100%; height: 380px; display: block; border-radius: 8px; cursor: crosshair; }
.plot-legend { display: flex; gap: 18px; flex-wrap: wrap; font-size: .88rem; color: var(--muted); padding: 8px 4px 0; }
.plot-legend i { display: inline-block; width: 22px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

/* ---------- explain / faq ---------- */
.explain { padding: 10px 0 30px; max-width: 780px; }
.explain h2 { font-size: 1.25rem; margin: 34px 0 10px; letter-spacing: -0.01em; }
.explain p, .explain li { color: var(--muted); }
.explain ul { padding-left: 22px; }
.explain li { margin: 6px 0; }
details.faq {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-s);
  margin: 10px 0; padding: 0;
}
details.faq summary {
  cursor: pointer; font-weight: 650; padding: 14px 18px; list-style: none;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
details.faq summary::-webkit-details-marker { display: none; }
details.faq summary::after { content: "+"; font-size: 1.3rem; color: var(--faint); flex: 0 0 auto; }
details.faq[open] summary::after { content: "−"; }
details.faq .a { padding: 0 18px 16px; color: var(--muted); font-size: .95rem; }

/* related tools */
.related { margin: 10px 0 30px; }
.related-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }

/* ---------- footer ---------- */
.site-footer { border-top: 1px solid var(--line); margin-top: 40px; background: var(--surface); }
.footer-grid {
  display: grid; grid-template-columns: 1.4fr 1fr 1fr 1fr; gap: 28px;
  padding: 44px 20px 10px; max-width: var(--max); margin: 0 auto;
}
.footer-grid h4 { font-size: .82rem; text-transform: uppercase; letter-spacing: .06em;
  color: var(--faint); margin: 0 0 12px; }
.footer-grid ul { list-style: none; margin: 0; padding: 0; }
.footer-grid li { margin: 8px 0; font-size: .93rem; }
.footer-grid a { color: var(--muted); }
.footer-grid a:hover { color: var(--accent-ink); }
.footer-brand p { color: var(--muted); font-size: .9rem; max-width: 300px; }
.footer-base {
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 22px 20px 30px; max-width: var(--max); margin: 0 auto;
  color: var(--faint); font-size: .86rem; border-top: 1px solid var(--line); margin-top: 24px;
}

.kbd { font-family: var(--mono); background: var(--surface-2);
  border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 6px;
  padding: 1px 8px; font-size: .86em; white-space: nowrap; }
.notice {
  border-radius: var(--radius-s); padding: 12px 16px; font-size: .92rem; margin: 14px 0;
  background: var(--warn-soft); color: var(--warn); border: 1px solid color-mix(in srgb, var(--warn) 30%, transparent);
}
.skip { position: absolute; left: -9999px; }
.skip:focus { left: 12px; top: 12px; z-index: 99; background: var(--surface); padding: 8px 14px; border-radius: 8px; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  .footer-grid { grid-template-columns: 1fr 1fr; }
  nav.main a:not(.keep) { display: none; }
  .hero { padding: 44px 0 24px; }
  .plot-wrap canvas { height: 300px; }
  table.matrix input { width: 50px; padding: 8px 2px; }
  .panel { padding: 18px; }
}
@media (max-width: 460px) {
  .footer-grid { grid-template-columns: 1fr; }
  .brand em { display: none; }
}
