/* ==========================================================================
   base.css — reset, typography, form controls, focus, tables
   Requires tokens.css.
   Mobile-first: unprefixed rules are the phone. Media queries only add.
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. RESET
   -------------------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  scroll-behavior: smooth;
  /* Anchor targets clear the sticky header. */
  scroll-padding-top: calc(var(--header-h, 3.5rem) + var(--sp-4));
  -moz-tab-size: 4;
  tab-size: 4;
}

body {
  min-height: 100svh;
  background-color: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  font-weight: var(--fw-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  overflow-wrap: break-word;
}

img,
picture,
svg,
video,
canvas {
  display: block;
  max-width: 100%;
  height: auto;
}

svg {
  fill: currentColor;
}

input,
button,
textarea,
select {
  font: inherit;
  color: inherit;
}

button {
  background: none;
  border: 0;
}

ul[class],
ol[class] {
  list-style: none;
  padding: 0;
}

::selection {
  background-color: var(--selection-bg);
  color: var(--selection-text);
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* --------------------------------------------------------------------------
   2. TYPOGRAPHY
   -------------------------------------------------------------------------- */
h1,
h2,
h3,
h4,
h5,
h6 {
  color: var(--text-strong);
  font-weight: var(--fw-semibold);
  line-height: var(--lh-tight);
  letter-spacing: var(--tracking-tight);
  text-wrap: balance;
}

h1 {
  font-size: var(--fs-3xl);
  font-weight: var(--fw-bold);
}

h2 {
  font-size: var(--fs-2xl);
}

h3 {
  font-size: var(--fs-xl);
  line-height: var(--lh-snug);
}

h4 {
  font-size: var(--fs-lg);
  line-height: var(--lh-snug);
}

h5,
h6 {
  font-size: var(--fs-base);
  line-height: var(--lh-snug);
}

p {
  text-wrap: pretty;
}

a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 0.16em;
  text-decoration-color: color-mix(in srgb, currentColor 40%, transparent);
  transition: color var(--dur) var(--ease),
    text-decoration-color var(--dur) var(--ease);
}

a:hover {
  color: var(--accent-hover);
  text-decoration-color: currentColor;
}

strong,
b {
  font-weight: var(--fw-semibold);
  color: var(--text-strong);
}

small {
  font-size: var(--fs-sm);
}

code,
kbd,
samp,
pre {
  font-family: var(--font-mono);
  font-size: 0.9375em;
}

code {
  background-color: var(--surface-sunken);
  border: var(--bw) solid var(--border);
  border-radius: var(--r-xs);
  padding: 0.1em 0.35em;
}

kbd {
  background-color: var(--surface-2);
  border: var(--bw) solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: var(--r-xs);
  padding: 0.1em 0.4em;
  font-size: 0.85em;
}

abbr[title] {
  text-decoration: underline dotted;
  cursor: help;
}

hr {
  border: 0;
  border-top: var(--bw) solid var(--divider);
  margin-block: var(--sp-8);
}

blockquote {
  border-left: 3px solid var(--accent-line);
  padding-left: var(--sp-4);
  color: var(--text-muted);
  font-style: normal;
}

/* Numbers anywhere they can change: fixed-width digits so a recalculating
   value does not reflow the line it sits on. */
.num,
[data-num],
time,
td[data-num],
.tabular {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

/* --------------------------------------------------------------------------
   3. FOCUS — visible, two-ring so it survives on any background.
   Never remove without replacement.
   -------------------------------------------------------------------------- */
:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}

/* Only suppress the default ring where :focus-visible is honoured. */
:where(a, button, input, select, textarea, summary, [tabindex]):focus:not(
    :focus-visible
  ) {
  outline: none;
}

/* On dark accent surfaces (the result card) invert the ring so it stays seen. */
.on-accent :focus-visible,
.result-card :focus-visible {
  outline-color: var(--result-text);
  box-shadow: 0 0 0 6px rgba(0, 0, 0, 0.35);
}

.skip-link {
  position: absolute;
  left: var(--sp-2);
  top: var(--sp-2);
  z-index: var(--z-toast);
  transform: translateY(-160%);
  background-color: var(--accent-fill);
  color: var(--accent-ink);
  padding: var(--sp-2) var(--sp-4);
  border-radius: var(--r-sm);
  font-weight: var(--fw-semibold);
  text-decoration: none;
  transition: transform var(--dur) var(--ease);
}

.skip-link:focus {
  transform: translateY(0);
}

.visually-hidden:not(:focus):not(:active) {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* --------------------------------------------------------------------------
   4. FORM CONTROLS
   Touch targets are >= 44px tall. Font-size is 16px on all text inputs so
   iOS Safari does not zoom the viewport on focus — a zoom is a layout shift
   the user feels even though it does not score as CLS.
   -------------------------------------------------------------------------- */
.field {
  display: flex;
  flex-direction: column;
  gap: var(--sp-2);
  min-width: 0;
}

.label,
label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
  letter-spacing: var(--tracking-wide);
}

.label--caps {
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  font-size: var(--fs-xs);
}

.hint {
  font-size: var(--fs-sm);
  color: var(--text-subtle);
}

.error-text {
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--err);
}

input[type="text"],
input[type="number"],
input[type="date"],
input[type="time"],
input[type="datetime-local"],
input[type="search"],
input[type="email"],
input[type="url"],
input[type="tel"],
select,
textarea {
  width: 100%;
  min-height: 2.875rem; /* 46px */
  padding: var(--sp-3) var(--sp-3);
  font-size: var(--fs-base);
  line-height: 1.3;
  color: var(--text-strong);
  background-color: var(--surface);
  border: var(--bw) solid var(--border-input);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--dur) var(--ease),
    box-shadow var(--dur) var(--ease), background-color var(--dur) var(--ease);
  appearance: none;
}

input[type="number"],
input[inputmode="numeric"],
input[inputmode="decimal"] {
  font-variant-numeric: tabular-nums;
}

/* Spinners steal width and are useless on a calculator; the keyboard and
   the +/- steppers do the job. */
input[type="number"] {
  -moz-appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

textarea {
  min-height: 6rem;
  resize: vertical;
  line-height: var(--lh-normal);
}

select {
  padding-right: var(--sp-8);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 1.15rem) 55%, calc(100% - 0.75rem) 55%;
  background-size: 0.4rem 0.4rem, 0.4rem 0.4rem;
  background-repeat: no-repeat;
  cursor: pointer;
}

input:hover:not(:disabled),
select:hover:not(:disabled),
textarea:hover:not(:disabled) {
  border-color: var(--border-strong);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 1px;
  border-color: var(--accent-line);
}

input::placeholder,
textarea::placeholder {
  color: var(--text-subtle);
  opacity: 1;
}

input:disabled,
select:disabled,
textarea:disabled {
  background-color: var(--surface-sunken);
  color: var(--text-subtle);
  cursor: not-allowed;
  box-shadow: none;
}

input[aria-invalid="true"],
select[aria-invalid="true"],
textarea[aria-invalid="true"] {
  border-color: var(--err);
  border-width: var(--bw-thick);
}

/* Native date/time pickers: make the indicator visible in dark mode. */
input[type="date"]::-webkit-calendar-picker-indicator,
input[type="time"]::-webkit-calendar-picker-indicator,
input[type="datetime-local"]::-webkit-calendar-picker-indicator {
  cursor: pointer;
  opacity: 0.7;
}
input[type="date"]:hover::-webkit-calendar-picker-indicator,
input[type="time"]:hover::-webkit-calendar-picker-indicator {
  opacity: 1;
}

/* --- checkbox / radio --- */
input[type="checkbox"],
input[type="radio"] {
  width: 1.25rem;
  height: 1.25rem;
  accent-color: var(--accent-fill);
  cursor: pointer;
  flex: none;
}

.choice {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-height: 2.75rem;
  cursor: pointer;
}

.choice label,
.choice > span {
  font-size: var(--fs-base);
  font-weight: var(--fw-normal);
  color: var(--text);
  letter-spacing: normal;
  cursor: pointer;
}

/* --- segmented control (unit switches: metric/imperial, 12h/24h) --- */
.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  background-color: var(--surface-sunken);
  border: var(--bw) solid var(--border);
  border-radius: var(--r-md);
}

.segmented input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.segmented label {
  flex: 1 1 0;
  display: grid;
  place-items: center;
  min-height: 2.375rem;
  padding-inline: var(--sp-3);
  border-radius: var(--r-sm);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
  letter-spacing: normal;
  cursor: pointer;
  user-select: none;
  transition: background-color var(--dur) var(--ease),
    color var(--dur) var(--ease);
}

.segmented input[type="radio"]:checked + label {
  background-color: var(--surface);
  color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.segmented input[type="radio"]:focus-visible + label {
  outline: 3px solid var(--focus-ring);
  outline-offset: 1px;
}

/* --------------------------------------------------------------------------
   5. BUTTONS
   -------------------------------------------------------------------------- */
.btn {
  --btn-bg: var(--surface);
  --btn-fg: var(--text-strong);
  --btn-border: var(--border-input);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  min-height: 2.875rem;
  padding: var(--sp-3) var(--sp-5);
  background-color: var(--btn-bg);
  color: var(--btn-fg);
  border: var(--bw) solid var(--btn-border);
  border-radius: var(--r-md);
  font-size: var(--fs-base);
  font-weight: var(--fw-semibold);
  line-height: 1;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  box-shadow: var(--shadow-sm);
  transition: background-color var(--dur) var(--ease),
    border-color var(--dur) var(--ease), transform var(--dur-fast) var(--ease);
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.btn--primary {
  --btn-bg: var(--accent-fill);
  --btn-fg: var(--accent-ink);
  --btn-border: transparent;
  box-shadow: var(--shadow-md);
}
.btn--primary:hover:not(:disabled) {
  --btn-bg: var(--accent-fill-hover);
}

.btn--secondary:hover:not(:disabled) {
  --btn-bg: var(--surface-2);
  --btn-border: var(--border-strong);
}

.btn--ghost {
  --btn-bg: transparent;
  --btn-border: transparent;
  --btn-fg: var(--text-muted);
  box-shadow: none;
}
.btn--ghost:hover:not(:disabled) {
  --btn-bg: var(--surface-sunken);
  --btn-fg: var(--text-strong);
}

.btn--block {
  width: 100%;
}

.btn--sm {
  min-height: 2.25rem;
  padding: var(--sp-2) var(--sp-3);
  font-size: var(--fs-sm);
}

.btn--icon {
  padding: var(--sp-2);
  min-width: 2.75rem;
  min-height: 2.75rem;
}

/* --------------------------------------------------------------------------
   6. TABLES — conversion charts, grade scales, sleep-time tables.
   Always wrap in .table-wrap so a wide table scrolls itself instead of the page.
   -------------------------------------------------------------------------- */
.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: var(--bw) solid var(--border);
  border-radius: var(--r-lg);
  background-color: var(--surface);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
}

caption {
  caption-side: top;
  padding: var(--sp-3) var(--sp-4);
  text-align: left;
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
}

th,
td {
  padding: var(--sp-3) var(--sp-4);
  text-align: left;
  border-bottom: var(--bw) solid var(--divider);
}

thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background-color: var(--surface-2);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  white-space: nowrap;
  border-bottom: var(--bw) solid var(--border);
}

tbody tr:last-child th,
tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr:hover {
  background-color: var(--surface-2);
}

td.num,
th.num {
  text-align: right;
}

/* --------------------------------------------------------------------------
   7. SMALL SHARED PIECES
   -------------------------------------------------------------------------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  padding: 0.15rem var(--sp-2);
  border-radius: var(--r-pill);
  background-color: var(--accent-soft);
  border: var(--bw) solid var(--accent-soft-border);
  color: var(--accent);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--tracking-wide);
  white-space: nowrap;
}

.note {
  padding: var(--sp-4);
  border-radius: var(--r-md);
  border: var(--bw) solid var(--accent-soft-border);
  background-color: var(--accent-soft);
  font-size: var(--fs-sm);
}

details {
  border: var(--bw) solid var(--border);
  border-radius: var(--r-md);
  background-color: var(--surface);
}

summary {
  padding: var(--sp-4);
  cursor: pointer;
  font-weight: var(--fw-semibold);
  color: var(--text-strong);
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--sp-3);
  min-height: 2.75rem;
}

summary::-webkit-details-marker {
  display: none;
}

summary::after {
  content: "";
  flex: none;
  width: 0.55rem;
  height: 0.55rem;
  border-right: 2px solid var(--text-subtle);
  border-bottom: 2px solid var(--text-subtle);
  transform: rotate(45deg) translate(-2px, -2px);
  transition: transform var(--dur) var(--ease);
}

details[open] > summary::after {
  transform: rotate(-135deg) translate(-2px, -2px);
}

/* Utilities used by templates. */
.u-flow > * + * {
  margin-top: var(--sp-4);
}
.u-center {
  text-align: center;
}
.u-muted {
  color: var(--text-muted);
}
.u-nowrap {
  white-space: nowrap;
}
.u-hide {
  display: none !important;
}

@media print {
  .site-header,
  .site-footer,
  .ad-slot,
  .related-tools,
  .theme-toggle,
  .copy-btn {
    display: none !important;
  }
  body {
    background: #fff;
    color: #000;
  }
}
