/* ---------- Base ---------- */
:root {
  --bg: #f6f8fb;
  --bg-elev: #ffffff;
  --ink: #0b1220;
  --ink-2: #1f2a3d;
  --muted: #5a6478;
  --muted-2: #8a93a6;
  --line: #e6ebf2;
  --line-strong: #d6deea;

  --brand: #1e3a8a;
  --brand-2: #3b82f6;
  --brand-soft: #eaf1ff;

  --accent: #0ea5e9;
  --good: #16a34a;
  --warn: #f59e0b;
  --bad:  #dc2626;

  --grade-7: #16a34a;
  --grade-6: #22c55e;
  --grade-5: #84cc16;
  --grade-4: #eab308;
  --grade-3: #f59e0b;
  --grade-2: #f97316;
  --grade-1: #dc2626;

  --radius: 16px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 1px rgba(15, 23, 42, 0.03);
  --shadow-md: 0 8px 24px -12px rgba(15, 23, 42, 0.18), 0 2px 6px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 24px 48px -24px rgba(15, 23, 42, 0.25), 0 8px 16px -8px rgba(15, 23, 42, 0.08);

  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button { font-family: inherit; }

.bg-grid {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(59,130,246,0.10), transparent 60%),
    radial-gradient(900px 500px at -10% 10%, rgba(30,58,138,0.08), transparent 55%),
    var(--bg);
}

/* ---------- Top bar ---------- */
.topbar {
  max-width: 1100px;
  margin: 0 auto;
  padding: 22px 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 36px;
  height: 36px;
  filter: drop-shadow(0 6px 12px rgba(30, 58, 138, 0.25));
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}

.brand-name {
  font-weight: 700;
  font-size: 16px;
  color: var(--ink);
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.trust-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 13px;
  color: var(--ink-2);
  font-weight: 500;
  box-shadow: var(--shadow-sm);
}

.trust-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 4px rgba(34,197,94,0.15);
  animation: pulse 2.4s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 4px rgba(34,197,94,0.15); }
  50%      { box-shadow: 0 0 0 8px rgba(34,197,94,0.05); }
}

/* ---------- Layout ---------- */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 28px 80px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.hero {
  padding: 16px 4px 8px;
}

.hero h1 { word-wrap: break-word; overflow-wrap: anywhere; }

.hero h1 {
  font-size: clamp(28px, 4.2vw, 44px);
  letter-spacing: -0.02em;
  font-weight: 800;
  margin: 0 0 12px;
  color: var(--ink);
  background: linear-gradient(180deg, #0b1220 0%, #25324a 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.hero .lede {
  font-size: clamp(15px, 1.5vw, 17px);
  color: var(--muted);
  margin: 0;
  max-width: 720px;
}

.hero strong {
  color: var(--ink-2);
  font-weight: 600;
}

/* ---------- Card ---------- */
.card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow-md);
}

/* ---------- Calculator ---------- */
.calc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
  flex-wrap: wrap;
}

.calc-head h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* Segmented control */
.seg {
  position: relative;
  display: inline-flex;
  background: #eef2f7;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px;
  isolation: isolate;
}

.seg-btn {
  position: relative;
  z-index: 2;
  border: 0;
  background: transparent;
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  transition: color 0.2s ease;
}

.seg-btn.active {
  color: #fff;
}

.seg-thumb {
  position: absolute;
  z-index: 1;
  top: 4px;
  left: 4px;
  height: calc(100% - 8px);
  width: 50%;
  background: linear-gradient(135deg, #1e3a8a, #3b82f6);
  border-radius: 999px;
  transition: transform 0.28s cubic-bezier(0.65, 0, 0.35, 1), width 0.28s ease;
  box-shadow: 0 4px 14px rgba(30, 58, 138, 0.35);
}

.seg.is-sl .seg-thumb { transform: translateX(100%); }

/* Inputs */
.inputs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

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

.field {
  background: #fbfcfe;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 14px 16px 16px;
  transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}

.field:focus-within {
  border-color: var(--brand-2);
  box-shadow: 0 0 0 4px rgba(59,130,246,0.12);
  background: #ffffff;
}

.field-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 8px;
}

.field-label label {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
}

.field-label .meta {
  font-size: 12px;
  color: var(--muted-2);
  font-weight: 500;
}

.input-wrap {
  position: relative;
}

.input-wrap input {
  width: 100%;
  border: 0;
  outline: none;
  background: transparent;
  font-family: var(--font-mono);
  font-size: 26px;
  font-weight: 600;
  color: var(--ink);
  letter-spacing: -0.01em;
  padding: 4px 0;
}

.input-wrap input::placeholder {
  color: #c5cbd6;
}

.input-wrap input::-webkit-outer-spin-button,
.input-wrap input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.input-wrap input[type=number] { -moz-appearance: textfield; }

.input-wrap .suffix {
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--muted-2);
  font-weight: 500;
}

/* progress bar inside field */
.bar {
  margin-top: 10px;
  height: 4px;
  background: #eef1f6;
  border-radius: 999px;
  overflow: hidden;
}

.bar > span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--brand-2), var(--brand));
  border-radius: 999px;
  transition: width 0.35s cubic-bezier(0.65, 0, 0.35, 1);
}

/* P3 hidden in SL */
#p3-field.hidden { display: none; }

/* Actions */
.actions {
  margin-top: 22px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.btn-primary,
.btn-ghost {
  border: 0;
  cursor: pointer;
  padding: 14px 22px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 600;
  transition: transform 0.05s ease, box-shadow 0.18s ease, background 0.18s ease, color 0.18s ease;
}

.btn-primary {
  background: linear-gradient(135deg, #1e3a8a, #3b82f6);
  color: #fff;
  box-shadow: 0 10px 24px -10px rgba(30, 58, 138, 0.55);
}

.btn-primary:hover { box-shadow: 0 14px 28px -10px rgba(30, 58, 138, 0.7); }
.btn-primary:active { transform: translateY(1px); }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line-strong);
}

.btn-ghost:hover { color: var(--ink); background: #f3f5f9; }

/* ---------- Result ---------- */
.result {
  position: relative;
  overflow: hidden;
}

.result::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(600px 200px at 100% 0%, rgba(59,130,246,0.07), transparent 60%),
    radial-gradient(500px 220px at 0% 100%, rgba(34,197,94,0.05), transparent 60%);
  pointer-events: none;
}

.result-grid {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 32px;
  align-items: center;
}

@media (max-width: 820px) {
  .result-grid { grid-template-columns: 1fr; gap: 24px; }
}

.result-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.grade-display {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 8px 0 18px;
}

.grade-number {
  font-size: clamp(72px, 12vw, 112px);
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
  background: linear-gradient(180deg, #0b1220, #3b82f6);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  transition: filter 0.4s ease;
}

.grade-number.is-7 { background: linear-gradient(180deg, #052e16, #16a34a); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-6 { background: linear-gradient(180deg, #14532d, #22c55e); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-5 { background: linear-gradient(180deg, #3f6212, #84cc16); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-4 { background: linear-gradient(180deg, #713f12, #eab308); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-3 { background: linear-gradient(180deg, #7c2d12, #f59e0b); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-2 { background: linear-gradient(180deg, #7c2d12, #f97316); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.grade-number.is-1 { background: linear-gradient(180deg, #7f1d1d, #dc2626); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }

.grade-of {
  font-size: 22px;
  font-weight: 600;
  color: var(--muted-2);
}

.result-meta {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin-top: 8px;
  max-width: 520px;
}

.result-meta > div {
  background: #f5f7fb;
  border: 1px solid var(--line);
  padding: 12px 14px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.meta-label {
  font-size: 11.5px;
  color: var(--muted);
  font-weight: 500;
  margin-bottom: 4px;
  letter-spacing: -0.005em;
}

.meta-value {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meta-value.pos { color: #16a34a; }
.meta-value.neg { color: #dc2626; }

.disclaimer-small {
  margin-top: 18px;
  font-size: 12.5px;
  color: var(--muted);
  max-width: 420px;
  line-height: 1.55;
}

/* Scale visualization */
.scale {
  padding: 8px 22px 4px;
}

.scale-track {
  position: relative;
  height: 16px;
  background: #eef2f7;
  border-radius: 999px;
  overflow: visible;
}

.scale-fill {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: linear-gradient(90deg,
    #dc2626 0%,
    #f97316 9%,
    #f59e0b 20%,
    #eab308 32%,
    #84cc16 46%,
    #22c55e 60%,
    #16a34a 100%
  );
  opacity: 0.15;
}

.scale-ticks {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.scale-ticks .tick {
  position: absolute;
  top: -4px;
  width: 2px;
  height: 24px;
  background: rgba(15,23,42,0.18);
  border-radius: 1px;
}

.scale-ticks .tick-label {
  position: absolute;
  top: 26px;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
}

.scale-marker {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  left: 0%;
  transition: left 0.7s cubic-bezier(0.65, 0, 0.35, 1);
}

.scale-marker-line {
  display: block;
  width: 4px;
  height: 32px;
  background: var(--ink);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(15,23,42,0.25);
}

.scale-marker-pill {
  position: absolute;
  top: -38px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 6px;
  white-space: nowrap;
}

/* Edge-aware pill alignment: when marker is near 0% or 100%, shift the pill
   so it stays inside the card without clipping. JS toggles these classes. */
.scale-marker.at-start .scale-marker-pill { left: 0; transform: translateX(0); }
.scale-marker.at-end   .scale-marker-pill { left: auto; right: 0; transform: translateX(0); }

.scale-marker-pill::after {
  content: "";
  position: absolute;
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 8px;
  height: 8px;
  background: var(--ink);
}

.scale-marker.at-start .scale-marker-pill::after { left: 6px; }
.scale-marker.at-end   .scale-marker-pill::after { left: auto; right: 6px; }

.scale-legend {
  display: flex;
  justify-content: space-between;
  margin-top: 56px;
  padding: 0 2px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted-2);
  font-weight: 500;
}

.scale-legend span {
  display: inline-block;
}

/* ---------- Boundaries table ---------- */
.boundaries-head h2 {
  margin: 0 0 6px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.boundaries-head p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 14.5px;
  max-width: 760px;
}

.bnd-table-wrap {
  overflow-x: auto;
  border-radius: 12px;
  border: 1px solid var(--line);
}

.bnd-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.bnd-table thead th {
  background: #f3f6fb;
  text-align: left;
  padding: 12px 16px;
  font-size: 12px;
  letter-spacing: 0.05em;
  color: var(--muted);
  text-transform: uppercase;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
}

.bnd-table tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  color: var(--ink-2);
  font-family: var(--font-mono);
  font-weight: 500;
}

.bnd-table tbody tr:last-child td { border-bottom: 0; }

.bnd-table tbody tr.is-current {
  background: linear-gradient(90deg, rgba(59,130,246,0.08), rgba(59,130,246,0.0));
}

.bnd-table .grade-cell {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-sans);
  font-weight: 600;
}

.grade-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

.grade-chip.g7 { background: var(--grade-7); }
.grade-chip.g6 { background: var(--grade-6); }
.grade-chip.g5 { background: var(--grade-5); }
.grade-chip.g4 { background: var(--grade-4); }
.grade-chip.g3 { background: var(--grade-3); }
.grade-chip.g2 { background: var(--grade-2); }
.grade-chip.g1 { background: var(--grade-1); }

.bnd-table .avg-cell {
  font-weight: 700;
  color: var(--ink);
}

/* ---------- Explainer ---------- */
.explainer h2 {
  margin: 0 0 18px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.explain-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

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

.explain-grid article {
  background: #fbfcfe;
  border: 1px solid var(--line);
  padding: 18px;
  border-radius: 12px;
}

.explain-grid h3 {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 700;
  color: var(--ink-2);
}

.explain-grid ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.explain-grid li {
  padding: 6px 0;
  font-size: 14px;
  color: var(--muted);
  border-bottom: 1px dashed var(--line);
}

.explain-grid li:last-child { border-bottom: 0; }

.sources li {
  color: var(--ink-2);
  font-weight: 500;
}

/* ---------- Footer ---------- */
.footnote {
  margin-top: 8px;
  text-align: center;
  color: var(--muted-2);
  font-size: 12.5px;
  max-width: 720px;
  margin-left: auto;
  margin-right: auto;
  line-height: 1.6;
}

/* Reveal animations */
.card {
  animation: fadeUp 0.5s ease-out both;
}
.card:nth-of-type(2) { animation-delay: 0.05s; }
.card:nth-of-type(3) { animation-delay: 0.1s; }
.card:nth-of-type(4) { animation-delay: 0.15s; }

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

/* ====================================================================
   MOBILE
   ==================================================================== */

/* Tablets and small laptops */
@media (max-width: 760px) {
  .container { padding: 16px 16px 64px; gap: 18px; }
  .topbar    { padding: 16px 16px 8px; }
  .card      { padding: 20px; border-radius: 14px; }

  .calc-head  { margin-bottom: 16px; }
  .hero       { padding: 8px 2px; }
  .hero h1    { font-size: clamp(26px, 7vw, 36px); margin-bottom: 8px; }
  .hero .lede { font-size: 14.5px; line-height: 1.55; }

  .grade-display { margin: 6px 0 14px; }
  .grade-number  { font-size: clamp(80px, 22vw, 104px); }
  .grade-of      { font-size: 20px; }

  .result-label      { font-size: 12px; }
  .disclaimer-small  { font-size: 12px; margin-top: 14px; }

  .boundaries-head p { font-size: 13.5px; }

  .scale-marker-pill { font-size: 11px; padding: 3px 7px; top: -34px; }
  .scale-marker-line { height: 28px; }
  .scale-ticks .tick { height: 22px; }
  .scale-ticks .tick-label { font-size: 10px; top: 24px; }
  .scale-legend      { margin-top: 48px; font-size: 10px; }
}

/* Phones */
@media (max-width: 560px) {
  .container { padding: 12px 14px 56px; gap: 14px; }
  .topbar    { padding: 14px 14px 4px; }
  .card      { padding: 18px 16px; }

  /* Topbar shrinks: hide pill text, keep dot + short label */
  .brand-sub        { display: none; }
  .trust-pill       { padding: 6px 12px; font-size: 12px; }

  /* Calc head: title above toggle, toggle full-width */
  .calc-head {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  .calc-head h2 { font-size: 18px; }
  .seg          { width: 100%; justify-content: stretch; }
  .seg-btn      { flex: 1; padding: 10px 14px; font-size: 13px; }

  /* Inputs single column with tighter padding */
  .inputs       { gap: 12px; }
  .field        { padding: 12px 14px 14px; }
  .field-label label { font-size: 13.5px; }
  .field-label .meta { font-size: 11.5px; }
  .input-wrap input  { font-size: 22px; padding: 2px 0; }
  .input-wrap .suffix { font-size: 13px; }
  .bar          { margin-top: 8px; }

  /* Buttons stretch full width and stack */
  .actions      { flex-direction: column; gap: 10px; margin-top: 18px; }
  .btn-primary,
  .btn-ghost    { width: 100%; padding: 13px 18px; font-size: 14.5px; }

  /* Result: keep grade huge, but stack tiles in a clean 3-row layout */
  .grade-number    { font-size: clamp(96px, 30vw, 120px); }
  .grade-of        { font-size: 22px; }
  .result-meta {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .result-meta > div {
    display: grid;
    grid-template-columns: 1fr auto;
    align-items: center;
    padding: 10px 14px;
  }
  .meta-label { margin-bottom: 0; }
  .meta-value { text-align: right; font-size: 13.5px; }

  /* Scale: keep marker pill within card edges */
  .scale          { padding: 4px 10px 4px; }
  .scale-marker-pill {
    font-size: 10.5px;
    padding: 3px 6px;
    top: -32px;
  }

  /* Boundary table: hide redundant "Grade X" text, keep colored chips */
  .bnd-table thead th       { padding: 10px 12px; font-size: 11px; }
  .bnd-table tbody td       { padding: 10px 12px; font-size: 13px; }
  .bnd-table .grade-cell    { gap: 0; font-size: 0; }
  .bnd-table .grade-chip    { width: 28px; height: 28px; font-size: 13px; }

  /* Explainer cards stack neatly */
  .explain-grid { gap: 12px; }
  .explain-grid article { padding: 14px 16px; }

  .footnote { font-size: 11.5px; padding: 0 4px; }
}

/* Tiny phones (iPhone SE etc) */
@media (max-width: 360px) {
  .container          { padding: 10px 10px 48px; }
  .topbar             { padding: 12px 10px 4px; gap: 8px; }
  .card               { padding: 16px 14px; }
  .brand-mark         { width: 32px; height: 32px; }
  .brand-name         { font-size: 15px; }
  .trust-pill         { font-size: 11.5px; padding: 5px 10px; }
  .hero h1            { font-size: 24px; }
  .grade-number       { font-size: 90px; }
  .input-wrap input   { font-size: 20px; }
  .bnd-table thead th { padding: 8px 10px; }
  .bnd-table tbody td { padding: 8px 10px; font-size: 12.5px; }
}

/* Honor users who reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}
