:root {
  --bg: #ffffff;
  --bg-subtle: #f8fafc;
  --surface: #ffffff;
  --surface-muted: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-running: #737373;
  --accent: #0057ff;
  --accent-soft: rgba(0, 87, 255, 0.11);
  --upload: #10b981;
  --upload-soft: rgba(16, 185, 129, 0.14);
  --ok: #10b981;
  --warn: #f59e0b;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 12px 32px rgba(15, 23, 42, 0.08);
  --radius: 16px;
}

html[data-theme="dark"] {
  --bg: #090b10;
  --bg-subtle: #111520;
  --surface: #121722;
  --surface-muted: #1b2232;
  --border: #242e42;
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --text-running: #94a3b8;
  --accent: #3b82f6;
  --accent-soft: rgba(59, 130, 246, 0.18);
  --upload: #34d399;
  --upload-soft: rgba(52, 211, 153, 0.16);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.25s ease, color 0.25s ease;
}

.page-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 20px clamp(16px, 4vw, 32px) 28px;
  overflow-x: hidden;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  color: var(--text);
  border-radius: 12px;
  transition: background-color 0.25s ease;
}

.brand-logo {
  height: 36px;
  width: auto;
  display: block;
}

html[data-theme="dark"] .brand {
  background: rgba(248, 250, 252, 0.96);
  padding: 5px 12px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}

.icon-btn svg {
  width: 17px;
  height: 17px;
}

.icon-sun { display: none; }
.icon-moon { display: block; }
html[data-theme="dark"] .icon-sun { display: block; }
html[data-theme="dark"] .icon-moon { display: none; }

/* Main Hero Stage */
.main-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px 0 18px;
  gap: 28px;
}

.hero-card {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.eyebrow {
  margin: 0 0 12px;
  font-size: clamp(1rem, 2.3vw, 1.45rem);
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: -0.015em;
}

.speed-display {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 2.8vw, 28px);
  max-width: 100%;
  margin: 4px 0 10px;
}

.speed-number {
  font-size: clamp(4.75rem, min(23vw, 25vh), 12.5rem);
  font-weight: 800;
  line-height: 0.92;
  letter-spacing: -0.055em;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  transition: color 0.25s ease;
}

.speed-number.is-wide {
  font-size: clamp(3.6rem, min(17.5vw, 22vh), 10.5rem);
}

.speed-number.is-running {
  color: var(--text-running);
}

.speed-side {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 1.8vw, 14px);
}

.speed-unit {
  font-size: clamp(1.5rem, min(4.8vw, 5.2vh), 2.85rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  line-height: 1;
}

/* Action Ring Button (Fast.com spin/restart button) */
.action-ring {
  position: relative;
  width: clamp(42px, 5vw, 52px);
  height: clamp(42px, 5vw, 52px);
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: grid;
  place-items: center;
  border-radius: 50%;
  transition: transform 0.15s ease, color 0.2s ease;
}

.action-ring:hover {
  transform: scale(1.06);
}

.action-ring:active {
  transform: scale(0.96);
}

.ring-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-track {
  fill: none;
  stroke: var(--border);
  stroke-width: 3;
}

.ring-fill {
  fill: none;
  stroke: var(--accent);
  stroke-width: 3.2;
  stroke-linecap: round;
  stroke-dasharray: 125.66;
  stroke-dashoffset: 125.66;
  transition: stroke-dashoffset 0.18s linear, stroke 0.25s ease;
}

.action-ring.is-running .ring-svg {
  animation: spinSlow 2.4s linear infinite;
}

@keyframes spinSlow {
  to { transform: rotate(270deg); }
}

.ring-glyph-stop {
  display: none;
  width: 11px;
  height: 11px;
  border-radius: 2.5px;
  background: var(--accent);
}

.ring-glyph-restart {
  display: block;
  width: 19px;
  height: 19px;
  color: var(--text-muted);
}

.action-ring:hover .ring-glyph-restart {
  color: var(--text);
}

.action-ring.is-running .ring-glyph-stop {
  display: block;
}

.action-ring.is-running .ring-glyph-restart {
  display: none;
}

.action-ring:not(.is-running) .ring-fill {
  stroke: var(--ok);
  stroke-dashoffset: 0;
  opacity: 0.35;
}

/* Phase Strip */
.phase-strip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  margin-top: 6px;
}

.phase-steps {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

.phase-step {
  padding: 2px 8px;
  border-radius: 999px;
  transition: background-color 0.2s, color 0.2s;
}

.phase-step.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}

.phase-step.is-done {
  color: var(--text);
}

.phase-sep {
  opacity: 0.45;
}

.status-note {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  min-height: 1.25em;
}

/* Hero Sparkline */
.hero-sparkline-wrap {
  width: min(440px, 92%);
  height: 52px;
  margin-top: 14px;
  opacity: 0.9;
}

.hero-sparkline {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.hero-actions {
  margin-top: 16px;
}

.more-info-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 9px 18px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.2s, transform 0.15s, background-color 0.2s;
}

.more-info-btn:hover {
  border-color: var(--text-muted);
  background: var(--bg-subtle);
}

.chevron-icon {
  width: 16px;
  height: 16px;
  transition: transform 0.22s ease;
}

.more-info-btn[aria-expanded="true"] .chevron-icon {
  transform: rotate(180deg);
}

/* Expandable Drawer */
.details-drawer {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: fadeSlideUp 0.24s ease;
}

.details-drawer[hidden] {
  display: none;
}

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

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.metric-card {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}

.metric-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.metric-header h2 {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.grade-pill,
.sub-badge {
  font-size: 0.74rem;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.grade-pill[data-grade="A+"],
.grade-pill[data-grade="A"] {
  color: var(--ok);
  border-color: rgba(16, 185, 129, 0.35);
}

.grade-pill[data-grade="B"] {
  color: var(--warn);
  border-color: rgba(245, 158, 11, 0.35);
}

.grade-pill[data-grade="C"],
.grade-pill[data-grade="D"] {
  color: var(--accent);
  border-color: rgba(229, 9, 20, 0.35);
}

.metric-pairs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.metric-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.metric-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.metric-val-row {
  display: flex;
  align-items: baseline;
  gap: 5px;
}

.metric-val {
  font-size: clamp(1.5rem, 4.5vw, 2rem);
  font-weight: 800;
  letter-spacing: -0.04em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.metric-sub-unit {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-muted);
}

.metric-sub {
  font-size: 0.77rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Metadata Strip */
.meta-strip {
  display: grid;
  grid-template-columns: 1.45fr 1.2fr 1fr auto;
  align-items: center;
  gap: 14px 18px;
  padding: 14px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}

.meta-cell {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.meta-k {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.meta-v {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  overflow-wrap: anywhere;
}

.settings-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text);
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}

.settings-btn:hover {
  border-color: var(--text-muted);
  background: var(--surface-muted);
}

.settings-btn svg {
  width: 15px;
  height: 15px;
}

/* Detailed Chart */
.chart-panel,
.history-panel {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.chart-header,
.history-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.chart-title {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.chart-legend {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 600;
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.dot-dl,
.dot-ul {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

.dot-dl { background: var(--accent); }
.dot-ul { background: var(--upload); }

.chart-body {
  display: flex;
  align-items: stretch;
  gap: 10px;
  height: 136px;
}

.chart-y-axis {
  width: 60px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 2px 0 18px;
  text-align: right;
}

.chart-y-label {
  font-size: 0.69rem;
  font-weight: 600;
  line-height: 1;
  color: var(--text-muted);
  white-space: nowrap;
}

.detail-chart {
  flex: 1;
  min-width: 0;
  height: 100%;
  display: block;
}

.chart-grid-line {
  stroke: var(--border);
  stroke-width: 1;
  stroke-dasharray: 3 4;
}

.chart-line-dl {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chart-area-dl {
  fill: var(--accent-soft);
}

.chart-line-ul {
  fill: none;
  stroke: var(--upload);
  stroke-width: 2.3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chart-area-ul {
  fill: var(--upload-soft);
}

/* History */
.link-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  padding: 2px 6px;
}

.link-btn:hover {
  color: var(--accent);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-empty {
  margin: 0;
  font-size: 0.84rem;
  color: var(--text-muted);
}

.history-row {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 0.83rem;
}

.history-time {
  color: var(--text-muted);
  font-size: 0.78rem;
}

.history-val {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.history-mode {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--text-muted);
}

/* Settings Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal-card {
  width: min(460px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px 24px;
  box-shadow: var(--shadow-md);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-head h2 {
  margin: 0;
  font-size: 1.12rem;
  font-weight: 700;
}

.settings-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.field-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field input,
.field select {
  width: 100%;
  padding: 9px 11px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
}

.checkbox-field input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.modal-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.btn-primary,
.btn-secondary {
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
}

.btn-secondary {
  background: var(--bg-subtle);
  color: var(--text);
}

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

/* Tablet breakpoint */
@media (max-width: 900px) {
  .main-stage {
    padding: 18px 0 14px;
    gap: 22px;
  }

  .meta-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .meta-actions {
    justify-self: start;
  }
}

/* Mobile breakpoint */
@media (max-width: 600px) {
  .brand-logo {
    height: 30px;
  }

  .icon-btn {
    width: 40px;
    height: 40px;
  }

  .main-stage {
    padding: 14px 0 12px;
    gap: 20px;
  }

  .more-info-btn {
    min-height: 42px;
    padding: 10px 20px;
  }

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

  .metric-card,
  .chart-panel,
  .history-panel {
    padding: 15px 16px;
  }

  .meta-strip {
    grid-template-columns: 1fr;
    gap: 12px;
    padding: 14px 16px;
  }

  .meta-actions {
    width: 100%;
  }

  .settings-btn {
    width: 100%;
    justify-content: center;
    min-height: 42px;
  }

  .chart-body {
    height: 120px;
    gap: 8px;
  }

  .chart-y-axis {
    width: 52px;
  }

  .chart-y-label {
    font-size: 0.64rem;
  }

  .history-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px 8px;
    padding: 10px 12px;
  }

  .history-time {
    grid-column: 1 / 3;
  }

  .history-mode {
    grid-column: 3;
    grid-row: 1;
    justify-self: end;
  }

  .modal-card {
    padding: 18px;
    max-height: calc(100dvh - 28px);
    overflow-y: auto;
  }
}

/* Extra-small mobile */
@media (max-width: 400px) {
  .brand-logo {
    height: 26px;
  }

  .field-row {
    grid-template-columns: 1fr;
  }

  .modal-foot {
    flex-direction: column-reverse;
  }

  .modal-foot .btn-primary,
  .modal-foot .btn-secondary {
    width: 100%;
  }
}
