:root {
  color-scheme: light dark;
  --bg: #f7efe6;
  --bg-accent: #f3dcc4;
  --surface: rgba(255, 255, 255, 0.86);
  --surface-strong: #ffffff;
  --text: #1f1a16;
  --muted: #5f6a6a;
  --border: rgba(120, 92, 63, 0.22);
  --grid: rgba(120, 92, 63, 0.08);
  --primary: #0d6b6b;
  --secondary: #e07a5f;
  --accent: #f2c14e;
  --danger: #b93838;
  --success: #1c7c54;
  --shadow: 0 24px 60px rgba(19, 24, 39, 0.12);
  --font-display: "Georgia", "Times New Roman", serif;
  --font-body: "Trebuchet MS", "Verdana", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1217;
    --bg-accent: #1a2027;
    --surface: rgba(18, 24, 32, 0.9);
    --surface-strong: #141a22;
    --text: #f1f4f6;
    --muted: #9aa7b4;
  --border: rgba(148, 163, 184, 0.25);
  --grid: rgba(148, 163, 184, 0.08);
    --primary: #3fb6b6;
    --secondary: #f09a72;
    --accent: #f7d774;
    --danger: #ff8a8a;
    --success: #6ee7b7;
    --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--text);
  background: linear-gradient(140deg, var(--bg), var(--bg-accent));
  min-height: 100vh;
  position: relative;
}

body::before,
body::after {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
}

body::before {
  background: radial-gradient(circle at 15% 20%, rgba(224, 122, 95, 0.18), transparent 45%),
    radial-gradient(circle at 85% 15%, rgba(13, 107, 107, 0.2), transparent 40%),
    radial-gradient(circle at 70% 85%, rgba(242, 193, 78, 0.15), transparent 45%);
}

body::after {
  background-image: linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 120px 120px;
  opacity: 0.6;
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 2.5rem;
}

.hero {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1.5rem;
  align-items: center;
  margin-bottom: 1.5rem;
  animation: floatIn 0.8s ease;
}

.eyebrow {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--muted);
  margin: 0 0 0.4rem;
}

.hero h1 {
  font-family: var(--font-display);
  font-size: clamp(2rem, 3vw, 2.6rem);
  margin: 0 0 0.4rem;
  display: flex;
  gap: 0.6rem;
  align-items: center;
}

.subtitle {
  font-size: 1.05rem;
  color: var(--muted);
  margin: 0 0 0.85rem;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 0.85rem;
}

.hero-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 1rem 1.2rem;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
}

.hero-card .card-title {
  font-size: 0.9rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 0.5rem;
}

.formula {
  font-family: var(--font-display);
  font-size: 1.25rem;
  margin: 0 0 0.6rem;
}

.card-note {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.layout {
  display: grid;
  gap: 1.5rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 1.2rem 1.25rem 1.35rem;
  box-shadow: var(--shadow);
  animation: rise 0.8s ease;
}

.card-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 1rem;
}

.card-head h2 {
  margin: 0 0 0.25rem;
  font-family: var(--font-display);
  font-size: 1.5rem;
}

.card-desc {
  margin: 0;
  color: var(--muted);
}

.toggle-group {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.toggle input {
  appearance: none;
  width: 44px;
  height: 24px;
  border-radius: 999px;
  background: rgba(120, 92, 63, 0.2);
  position: relative;
  outline: none;
  cursor: pointer;
  transition: background 0.2s ease;
}

.toggle input::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--surface-strong);
  position: absolute;
  top: 3px;
  left: 4px;
  transition: transform 0.2s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.toggle input:checked {
  background: var(--primary);
}

.toggle input:checked::after {
  transform: translateX(20px);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.9rem;
}

.grid.two {
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 0.85rem;
}

label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-weight: 600;
  color: var(--text);
}

input[type="number"],
input[type="text"],
select {
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: inherit;
  font-family: inherit;
}

input:disabled,
select:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: rgba(120, 92, 63, 0.08);
}

input[readonly] {
  background: rgba(13, 107, 107, 0.1);
}

input:focus,
select:focus,
button:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(13, 107, 107, 0.2);
}

.input-invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(185, 56, 56, 0.18);
}

.row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.row.quick {
  margin-top: 0.75rem;
  gap: 0.6rem;
}

.chip-btn {
  border: 1px solid var(--border);
  background: var(--surface-strong);
  color: inherit;
  border-radius: 999px;
  padding: 0.4rem 0.9rem;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
  transition: border-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

.chip-btn:hover {
  transform: translateY(-1px);
  border-color: var(--primary);
  color: var(--primary);
}

.chip-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.utilities {
  justify-content: space-between;
}

.btn {
  border: 1px solid var(--border);
  background: transparent;
  color: inherit;
  border-radius: 999px;
  padding: 0.6rem 1.1rem;
  cursor: pointer;
  font-weight: 600;
  transition: transform 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.btn:hover {
  transform: translateY(-1px);
  border-color: var(--primary);
  color: var(--primary);
}

.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn.ghost {
  background: rgba(13, 107, 107, 0.08);
}

.status {
  font-size: 0.9rem;
  color: var(--muted);
}

.status[data-type="success"] {
  color: var(--success);
}

.status[data-type="error"] {
  color: var(--danger);
}

.plot-card {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-strong);
  padding: 0.6rem;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}

.result-grid label input[readonly] {
  transition: background 0.3s ease, color 0.3s ease;
}

input[type="number"]:focus,
input[type="text"]:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(13, 107, 107, 0.2), 0 2px 8px rgba(13, 107, 107, 0.1);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

canvas {
  width: 100%;
  height: auto;
  display: block;
}

.footer {
  margin-top: 2rem;
  color: var(--muted);
}

.muted {
  color: var(--muted);
}

@keyframes floatIn {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* Tooltip */
.plot-card {
  position: relative;
  overflow: hidden;
}

.plot-tooltip {
  position: absolute;
  z-index: 10;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.7rem;
  font-size: 0.82rem;
  line-height: 1.6;
  pointer-events: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
  white-space: nowrap;
  backdrop-filter: blur(6px);
}

.plot-tooltip strong {
  color: var(--primary);
}

/* Compare section */
.compare-grid {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px dashed var(--border);
  align-items: end;
}

.compare-label {
  font-weight: 700;
  font-size: 0.9rem;
  color: var(--primary);
  grid-column: 1 / -1;
  margin-bottom: -0.2rem;
}

/* Compare table */
.compare-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
  margin-top: 1rem;
}

.compare-table th,
.compare-table td {
  padding: 0.45rem 0.7rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.compare-table th {
  font-weight: 700;
  color: var(--muted);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.compare-table td:first-child {
  font-weight: 600;
}

.compare-table .dist1 {
  color: var(--primary);
}

.compare-table .dist2 {
  color: var(--accent);
}

/* Z-score strip */
.zscore-strip {
  position: relative;
  height: 28px;
  margin-top: 0.5rem;
  background: linear-gradient(90deg,
    rgba(13, 107, 107, 0.04),
    rgba(13, 107, 107, 0.1) 50%,
    rgba(13, 107, 107, 0.04));
  border-radius: 6px;
  border: 1px solid var(--border);
}

.zscore-strip .zs-marker {
  position: absolute;
  top: 0;
  height: 100%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 2px;
}

.zscore-strip .zs-marker::before {
  content: '';
  width: 1px;
  height: 9px;
  background: var(--muted);
  margin-bottom: 1px;
}

.zscore-strip .zs-marker span {
  font-size: 0.62rem;
  font-weight: 600;
  color: var(--muted);
  line-height: 1;
}

/* Input label badges */
.grid > label[for="mu"] > span::before,
.grid > label[for="sigma"] > span::before,
.grid > label[for="x"] > span::before,
.grid > label[for="p"] > span::before,
.grid > label[for="confidence"] > span::before,
.grid > label[for="lower"] > span::before,
.grid > label[for="upper"] > span::before {
  content: '';
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 0.35rem;
  vertical-align: middle;
}

.grid > label[for="mu"] > span::before { background: var(--primary); }
.grid > label[for="sigma"] > span::before { background: var(--secondary); }
.grid > label[for="x"] > span::before { background: var(--accent); }
.grid > label[for="p"] > span::before { background: var(--success); }
.grid > label[for="confidence"] > span::before { background: #8b5cf6; }
.grid > label[for="lower"] > span::before { background: #6366f1; }
.grid > label[for="upper"] > span::before { background: #a78bfa; }

/* Result update animation */
@keyframes resultFlash {
  0% { background-color: rgba(13, 107, 107, 0.28); }
  100% { background-color: rgba(13, 107, 107, 0.1); }
}

.result-flash {
  animation: resultFlash 0.4s ease-out;
}

/* Quick button visual hierarchy */
.chip-btn[data-sigma] {
  background: linear-gradient(135deg, rgba(13, 107, 107, 0.1), rgba(13, 107, 107, 0.03));
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 700;
}

.chip-btn[data-sigma]:hover {
  background: var(--primary);
  color: #fff;
}

.chip-btn[data-confidence] {
  background: linear-gradient(135deg, rgba(224, 122, 95, 0.1), rgba(224, 122, 95, 0.03));
  border-color: var(--secondary);
  color: var(--secondary);
}

.chip-btn[data-confidence]:hover {
  background: var(--secondary);
  color: #fff;
}

/* Canvas container subtle grid */
.plot-card::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 20px 20px;
  opacity: 0.4;
  pointer-events: none;
  z-index: 0;
}

.plot-card canvas {
  position: relative;
  z-index: 1;
}

.plot-card .plot-tooltip {
  z-index: 10;
}

@media (max-width: 720px) {
  .hero {
    grid-template-columns: 1fr;
  }

  .utilities {
    justify-content: flex-start;
  }
}

@media (max-width: 480px) {
  body {
    padding: 8px;
  }

  .result-grid {
    grid-template-columns: 1fr;
  }

  .compare-table {
    font-size: 0.75rem;
  }

  .btn {
    min-height: 44px;
  }
}

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