/*
 * Mateen Labs calculators (age, BMI, loan, date difference, unit converter).
 * Mobile-first. Uses the site tokens from /assets/style.css, so light and dark mode follow the site.
 */

.calc {
    display: grid;
    gap: 18px;
    margin: 14px 0 8px;
}

@media (min-width: 900px) {
    .calc-two {
        grid-template-columns: minmax(0, 5fr) minmax(0, 6fr);
        align-items: start;
    }
}

.calc-card {
    min-width: 0;
    padding: 18px;
    border: 1px solid var(--line);
    border-top: 4px solid var(--accent);
    border-radius: var(--radius);
    background: var(--surface);
}

.calc-card-result { border-top-color: var(--ok-text); }

.calc-card h2 {
    margin: 0 0 14px;
    font-size: 20px;
}

.calc-card h3 {
    margin: 18px 0 10px;
    font-size: 16px;
}

.calc-form {
    display: grid;
    gap: 16px;
}

/* ---------- Fields ---------- */

.calc-fields {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr));
    gap: 14px;
}

.calc-fields[hidden],
.calc-field[hidden] { display: none; }

.calc-field {
    display: grid;
    gap: 6px;
    align-content: start;
    min-width: 0;
}

.calc-field > label,
.calc-label {
    color: var(--text);
    font-size: 14px;
    font-weight: 700;
}

.calc-opt {
    margin-left: 4px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
}

.calc input[type="text"],
.calc input[type="date"],
.calc select {
    box-sizing: border-box;
    width: 100%;
    min-width: 0;
    min-height: 48px;
    padding: 8px 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    font: 600 16px var(--font);
}

.calc input[type="date"] {
    -webkit-appearance: none;
    appearance: none;
    display: block;
}

.calc input:focus-visible,
.calc select:focus-visible,
.calc button:focus-visible,
.calc summary:focus-visible,
.calc-scroll:focus-visible {
    outline: none;
    box-shadow: var(--ring);
}

.calc input[aria-invalid="true"] { border-color: var(--err-text); }

.calc-hint {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
}

.calc-err {
    margin: 0;
    color: var(--err-text);
    font-size: 14px;
    font-weight: 600;
}

.calc-unit-pair {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

/* ---------- Segmented control (radio buttons) ---------- */

.calc-seg {
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
}

.calc-seg legend {
    margin-bottom: 6px;
    padding: 0;
    color: var(--text);
    font-size: 14px;
    font-weight: 700;
}

.calc-seg-options {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 4px;
    border: 1px solid var(--line-strong);
    border-radius: 12px;
    background: var(--panel);
}

.calc-seg-options label {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
}

.calc-seg-options input {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
}

.calc-seg-options span {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 40px;
    padding: 4px 12px;
    border-radius: 9px;
    color: var(--text);
    font-size: 14px;
    font-weight: 650;
    text-align: center;
}

.calc-seg-options input:checked + span {
    background: var(--accent);
    color: var(--on-accent);
}

.calc-seg-options input:focus-visible + span { box-shadow: var(--ring); }

/* ---------- Checkboxes ---------- */

.calc-check {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
    color: var(--text);
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}

.calc-check input {
    flex: none;
    width: 22px;
    height: 22px;
    margin: 0;
    accent-color: var(--accent);
}

.calc-check input:focus-visible { outline: none; box-shadow: var(--ring); }

/* ---------- Buttons ---------- */

.calc .tool-actions { margin: 0; justify-content: flex-start; }

.calc-btn-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.calc-icon-btn {
    min-width: 48px;
}

/* ---------- Results ---------- */

.calc-big {
    margin: 0 0 4px;
    color: var(--accent-text);
    font-size: clamp(26px, 6vw, 36px);
    font-weight: 800;
    letter-spacing: -.02em;
    line-height: 1.15;
    overflow-wrap: anywhere;
}

.calc-sub {
    margin: 0 0 14px;
    color: var(--muted);
    font-size: 15px;
    overflow-wrap: anywhere;
}

.calc-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr));
    gap: 10px;
    margin: 0 0 14px;
}

.calc-stat {
    min-width: 0;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
}

.calc-stat dt {
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
}

.calc-stat dd {
    margin: 4px 0 0;
    color: var(--text);
    font-size: 18px;
    font-weight: 750;
    overflow-wrap: anywhere;
}

.calc-stat dd.calc-stat-note {
    margin-top: 2px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
}

.calc-note {
    margin: 0 0 14px;
    padding: 12px 14px;
    border: 1px solid var(--line);
    border-left: 4px solid var(--accent);
    border-radius: 10px;
    background: var(--panel);
    color: var(--text);
    font-size: 14px;
    line-height: 1.55;
}

.calc-note[hidden] { display: none; }

.calc-note-warn {
    border-color: var(--warn-line);
    border-left-color: var(--warn-text);
    background: var(--warn-bg);
    color: var(--warn-text);
}

.calc-result[hidden] { display: none; }

.calc .tool-status { margin-bottom: 14px; }

.calc-formula {
    margin: 0 0 14px;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--code-bg);
    color: var(--text);
    font: 14px/1.6 var(--mono);
    overflow-wrap: anywhere;
}

/* ---------- BMI scale ---------- */

.calc-scale {
    position: relative;
    margin: 6px 0 16px;
    padding-top: 20px;
}

.calc-scale-bar {
    display: flex;
    height: 14px;
    overflow: hidden;
    border-radius: 999px;
}

.calc-scale-bar span { height: 100%; }

.calc-scale-marker {
    position: absolute;
    top: 0;
    width: 0;
    height: 0;
    margin-left: -8px;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-top: 14px solid var(--text);
    transition: left .25s ease;
}

.calc-scale-labels {
    position: relative;
    height: 16px;
    margin-top: 6px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
}

.calc-scale-labels span {
    position: absolute;
    top: 0;
    transform: translateX(-50%);
}

.calc-c-under { background: #60a5fa; }
.calc-c-normal { background: #22c55e; }
.calc-c-over { background: #facc15; }
.calc-c-obese1 { background: #fb923c; }
.calc-c-obese2 { background: #f87171; }
.calc-c-obese3 { background: #b91c1c; }

.calc-legend {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr));
    gap: 4px 12px;
    margin: 0 0 14px;
    padding: 0;
    list-style: none;
    font-size: 13px;
}

.calc-legend li {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--text);
}

.calc-legend li.is-current { font-weight: 800; }

.calc-swatch {
    flex: none;
    width: 14px;
    height: 14px;
    border-radius: 4px;
}

/* ---------- Loan charts and schedule ---------- */

.calc-charts {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr));
    gap: 14px;
    margin-bottom: 14px;
}

.calc-chart {
    min-width: 0;
    margin: 0;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--panel);
}

.calc-chart svg {
    display: block;
    width: 100%;
    height: auto;
    max-height: 220px;
}

.calc-chart figcaption {
    margin-top: 8px;
    color: var(--muted);
    font-size: 13px;
}

.calc-schedule details {
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
}

.calc-schedule details + details { margin-top: 8px; }

.calc-schedule summary {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 4px 12px;
    min-height: 44px;
    align-items: center;
    padding: 8px 14px;
    border-radius: 12px;
    color: var(--text);
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
}

.calc-schedule summary span { color: var(--muted); font-weight: 600; }

.calc-scroll {
    max-width: 100%;
    overflow-x: auto;
    border-top: 1px solid var(--line);
}

.calc-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    font-variant-numeric: tabular-nums;
}

.calc-table th,
.calc-table td {
    padding: 7px 8px;
    border-bottom: 1px solid var(--line);
    text-align: right;
    white-space: nowrap;
}

.calc-table th:first-child,
.calc-table td:first-child { text-align: left; }

.calc-table thead th {
    background: var(--panel);
    color: var(--muted);
    font-weight: 700;
}

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

/* ---------- Unit converter ---------- */

.calc-convert {
    display: grid;
    gap: 12px;
}

@media (min-width: 640px) {
    .calc-convert {
        grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
        align-items: end;
    }
}

.calc-swap {
    justify-self: center;
}

.calc-cats {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.calc-cats .calc-seg-options { width: 100%; box-sizing: border-box; }

.calc-cats .calc-seg-options label { flex: 1 1 96px; }

.calc-sr {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 420px) {
    .calc-card { padding: 14px; }
    .calc-stat dd { font-size: 16px; }
}

/* ---------- SVG chart colours ---------- */

.calc-principal { background: var(--accent); }
.calc-interest { background: #f59e0b; }
.calc-principal-stroke { stroke: var(--accent); }
.calc-interest-stroke { stroke: #f59e0b; }

.calc-chart text { fill: currentColor; }
.calc-chart svg { color: var(--text); }
.calc-svg-big { font: 800 26px var(--font); }
.calc-svg-small { font: 600 12px var(--font); }
.calc-axis { stroke: var(--line-strong); stroke-width: 1; }
.calc-line { stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; }
.calc-area { fill: var(--accent); fill-opacity: .12; }

.calc [hidden] { display: none !important; }

/* ---------- Unit converter controls ---------- */

.calc-formula { white-space: pre-line; }

.calc-output {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    min-height: 48px;
    padding: 8px 12px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--panel);
    color: var(--text);
    font: 700 16px var(--font);
    overflow-wrap: anywhere;
}

.calc-swap {
    min-height: 48px;
    padding: 8px 16px;
    border: 1px solid var(--line-strong);
    border-radius: 11px;
    background: var(--surface);
    color: var(--text);
    font: 650 15px var(--font);
    cursor: pointer;
}

.calc-swap:hover { border-color: var(--accent); background: var(--panel); }

#uc-big:empty, #uc-formula:empty { display: none; }

/* Safari ignores min-height on native selects, so draw our own arrow (keeps 48px targets). */
.calc select {
    -webkit-appearance: none;
    appearance: none;
    padding-right: 38px;
    background-image:
        linear-gradient(45deg, transparent 50%, var(--muted) 50%),
        linear-gradient(135deg, var(--muted) 50%, transparent 50%);
    background-position: calc(100% - 20px) 52%, calc(100% - 14px) 52%;
    background-size: 6px 6px;
    background-repeat: no-repeat;
}
