:root {
  --bg: #fafafa;
  --bg-secondary: #f4f4f5;
  --bg-tertiary: #e4e4e7;
  --border: #d4d4d8;
  --text: #18181b;
  --text-secondary: #52525b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-subtle: #dbeafe;
  --success: #16a34a;
  --success-subtle: #dcfce7;
  --radius: 0.5rem;
  --radius-lg: 0.75rem;
  --shadow: 0 1px 3px rgba(0 0 0 / 0.08), 0 1px 2px rgba(0 0 0 / 0.06);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #09090b;
    --bg-secondary: #18181b;
    --bg-tertiary: #27272a;
    --border: #3f3f46;
    --text: #fafafa;
    --text-secondary: #a1a1aa;
    --accent: #60a5fa;
    --accent-hover: #93c5fd;
    --accent-subtle: #1e3a5f;
    --success: #4ade80;
    --success-subtle: #052e16;
    --shadow: 0 1px 3px rgba(0 0 0 / 0.3);
  }
}

[data-theme="light"] {
  --bg: #fafafa;
  --bg-secondary: #f4f4f5;
  --bg-tertiary: #e4e4e7;
  --border: #d4d4d8;
  --text: #18181b;
  --text-secondary: #52525b;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-subtle: #dbeafe;
  --success: #16a34a;
  --success-subtle: #dcfce7;
  --shadow: 0 1px 3px rgba(0 0 0 / 0.08), 0 1px 2px rgba(0 0 0 / 0.06);
}

[data-theme="dark"] {
  --bg: #09090b;
  --bg-secondary: #18181b;
  --bg-tertiary: #27272a;
  --border: #3f3f46;
  --text: #fafafa;
  --text-secondary: #a1a1aa;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
  --accent-subtle: #1e3a5f;
  --success: #4ade80;
  --success-subtle: #052e16;
  --shadow: 0 1px 3px rgba(0 0 0 / 0.3);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  padding: 1rem;
  line-height: 1.5;
}

.container {
  width: 100%;
  max-width: 40rem;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Header */
header {
  position: relative;
  padding-block: 0.5rem;

  & h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.02em;
  }

  & .subtitle {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
  }
}

.theme-toggle {
  position: absolute;
  top: 0.5rem;
  right: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.375rem 0.625rem;
  cursor: pointer;
  color: var(--text);
  font-size: 1rem;
  transition: background 0.15s, border-color 0.15s;

  &:hover {
    background: var(--bg-tertiary);
    border-color: var(--accent);
  }
}

/* Card */
.card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  box-shadow: var(--shadow);

  & h2 {
    font-size: 0.9375rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text);
  }
}

/* Input row */
.input-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;

  & input, & select {
    height: 2.75rem;
  }
}

.distance-input {
  flex: 1 1 0;
  min-width: 0;
  padding: 0 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
  -moz-appearance: textfield;

  &::-webkit-outer-spin-button,
  &::-webkit-inner-spin-button {
    -webkit-appearance: none;
  }

  &:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
  }

  &::placeholder {
    color: var(--text-secondary);
  }
}

.unit-select {
  flex: 0 0 auto;
  padding: 0 2rem 0 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.875rem;
  font-family: inherit;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2371717a' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.625rem center;
  transition: border-color 0.15s;

  &:focus {
    outline: none;
    border-color: var(--accent);
  }
}

/* Speed section */
.speed-section {
  & h2 {
    margin-bottom: 0;
  }
}

.speed-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0;

  & .optional {
    font-size: 0.75rem;
    font-weight: 400;
    color: var(--text-secondary);
  }
}

.speed-controls {
  margin-top: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.25s ease, opacity 0.2s ease, margin-top 0.2s ease;
  pointer-events: none;

  &.open {
    max-height: 12rem;
    opacity: 1;
    pointer-events: auto;
  }
}

/* Toggle switch */
.toggle-label {
  display: flex;
  align-items: center;
  cursor: pointer;

  & input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
  }
}

.toggle-track {
  display: block;
  width: 2.5rem;
  height: 1.375rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 9999px;
  position: relative;
  transition: background 0.2s, border-color 0.2s;

  .toggle-label input:checked ~ & {
    background: var(--accent);
    border-color: var(--accent);
  }
}

.toggle-thumb {
  display: block;
  position: absolute;
  top: 0.1875rem;
  left: 0.1875rem;
  width: 0.875rem;
  height: 0.875rem;
  background: var(--bg);
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 2px rgba(0 0 0 / 0.2);

  .toggle-label input:checked ~ .toggle-track & {
    transform: translateX(1.125rem);
  }
}

/* Presets */
.presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  align-items: center;
}

.presets-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.preset-btn {
  padding: 0.25rem 0.625rem;
  font-size: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s, color 0.15s;

  &:hover {
    background: var(--accent-subtle);
    border-color: var(--accent);
    color: var(--accent);
  }

  &.active {
    background: var(--accent-subtle);
    border-color: var(--accent);
    color: var(--accent);
  }
}

/* Results section */
.results-section {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.results-empty {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius-lg);
}

.hidden { display: none !important; }

.results-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;

  & > h2 {
    font-size: 0.9375rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-size: 0.75rem;
  }
}

/* Results grid */
.results-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.result-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.75rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  cursor: default;
  transition: border-color 0.15s, background 0.15s;

  &:hover {
    border-color: var(--accent);
  }

  &.highlight {
    background: var(--accent-subtle);
    border-color: var(--accent);
  }

  & .result-value {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
    word-break: break-all;
    line-height: 1.3;
  }

  & .result-unit {
    font-size: 0.8125rem;
    color: var(--text-secondary);
  }
}

/* Time results */
.time-results {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;

  & h2 {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
  }
}

.time-display {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.time-card {
  background: var(--success-subtle);
  border: 1px solid color-mix(in srgb, var(--success) 35%, transparent);
  border-radius: var(--radius-lg);
  padding: 1rem;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0.5rem;

  & .time-main {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--success);
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
  }

  & .time-breakdown {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
  }

  & .time-speed-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: right;
  }
}

/* Responsive */
@media (min-width: 480px) {
  .results-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 640px) {
  body { padding: 2rem; }

  header {
    & h1 { font-size: 1.875rem; }
  }

  .card { padding: 1.25rem; }

  .results-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .speed-controls.open {
    max-height: 8rem;
  }
}
