/* ═════════════════════════════════════════════════════════════════════════ */
/* FitBack — ISO 286 Fit Identification from Measured Bore and Shaft          */
/* Theme: inspection room — granite surface plate, gauge steel, CMM screen    */
/* ═════════════════════════════════════════════════════════════════════════ */

:root {
    --sil-light-ready: 1;

    /* Surfaces — granite through anodised fixture */
    --bg-primary: #121318;
    --bg-surface: #1b1d26;
    --bg-card: #1b1d26;
    --bg-hover: #252833;
    --bg-input: #1f2230;
    --bg-sunken: #0d0e12;

    /* Type */
    --text-primary: #f2f3f8;
    --text-secondary: #c8cbd8;
    --text-muted: #9598aa;

    /* Structure */
    --border: #2e3242;
    --border-hover: #454a60;

    /* Action — periwinkle, which nothing else on the site uses */
    --accent: #8b9dff;
    --accent-hover: #a5b3ff;
    --accent-glow: rgba(139, 157, 255, 0.18);
    --on-accent: #10121c;

    /* Semantic */
    --success: #5ec27a;
    --warning: #e0b23f;
    --error: #e46a5c;

    /* Domain state — where a measurement sits relative to its zone */
    --state-inside: #5ec27a;
    --state-marginal: #e0b23f;
    --state-outside: #e46a5c;

    /* The tolerance-zone diagram. Hole and shaft are kept apart in hue AND
       lightness so the two bands stay separable in both themes and to a
       red-green colour-blind reader. */
    --zone-hole: #5a8dd6;
    --zone-hole-fill: rgba(90, 141, 214, 0.22);
    --zone-shaft: #d9a441;
    --zone-shaft-fill: rgba(217, 164, 65, 0.22);
    --zero-line: #f2f3f8;
    --pin: #f2f3f8;
    --clearance-band: rgba(94, 194, 122, 0.20);
    --interference-band: rgba(228, 106, 92, 0.22);
    --grid-line: #262a38;

    --radius-lg: 16px;
    --radius-md: 10px;
    --radius-sm: 6px;
    --shadow-subtle: 0 4px 22px rgba(0, 0, 0, 0.5);
}

/* ── Light theme — same hues, lightness remapped. ── */
[data-theme="light"] {
    --bg-primary: #f5f6f9;
    --bg-surface: #ffffff;
    --bg-card: #ffffff;
    --bg-hover: #e9ebf2;
    --bg-input: #fafbfd;
    --bg-sunken: #eef0f5;

    --text-primary: #14161f;
    --text-secondary: #363b4d;
    --text-muted: #5d6377;

    --border: #d6d9e4;
    --border-hover: #a9aec2;

    --accent: #4a56c4;
    --accent-hover: #3a459f;
    --accent-glow: rgba(74, 86, 196, 0.12);
    --on-accent: #ffffff;

    --success: #2e7d43;
    --warning: #8a6206;
    --error: #b23a2c;

    --state-inside: #2e7d43;
    --state-marginal: #8a6206;
    --state-outside: #b23a2c;

    --zone-hole: #2a63ad;
    --zone-hole-fill: rgba(42, 99, 173, 0.16);
    --zone-shaft: #9c6f10;
    --zone-shaft-fill: rgba(156, 111, 16, 0.16);
    --zero-line: #14161f;
    --pin: #14161f;
    --clearance-band: rgba(46, 125, 67, 0.15);
    --interference-band: rgba(178, 58, 44, 0.16);
    --grid-line: #e2e5ee;

    --shadow-subtle: 0 4px 22px rgba(20, 22, 31, 0.10);
}

* { box-sizing: border-box; }

body {
    background-color: var(--bg-primary);
    color: var(--text-secondary);
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
    margin: 0;
    padding: 0;
    line-height: 1.6;
    -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4 {
    font-family: 'Outfit', system-ui, sans-serif;
    color: var(--text-primary);
    margin: 0 0 0.6rem;
}

h1 { font-size: 2.5rem; font-weight: 800; line-height: 1.1; letter-spacing: -0.02em; }
h2 { font-size: 1.75rem; font-weight: 700; line-height: 1.2; letter-spacing: -0.01em; }
h3 { font-size: 1.35rem; font-weight: 700; line-height: 1.3; }
h4 { font-size: 1.15rem; font-weight: 600; line-height: 1.4; }

p { margin: 0 0 1rem; }
a { color: var(--accent); }

/* ── Header ──────────────────────────────────────────────────────────────── */

.app-header {
    background: linear-gradient(180deg, var(--bg-surface) 0%, var(--bg-primary) 100%);
    border-bottom: 1px solid var(--border);
    padding: 2.5rem 1rem 2rem;
    text-align: center;
}

.header-container { max-width: 800px; margin: 0 auto; }

.brand-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--accent-glow);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.3rem 0.9rem;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 1rem;
}

.badge-icon { font-size: 1rem; }
.app-header p { color: var(--text-muted); font-size: 1.05rem; margin: 0; }

/* width:100% is load-bearing, not cosmetic. shared/global-ui.css makes <body> a
   column flex container and gives main `flex: 1 0 auto` — flex-shrink 0. Without
   an explicit width the flex base size resolves to max-content, so the widest
   descendant (the zone diagram, the reference tables) drags the whole page wider
   than the viewport and the body scrolls sideways on a phone. */
.app-main {
    width: 100%;
    max-width: 1140px;
    margin: 0 auto;
    padding: 1.75rem 1rem 3rem;
}

.ad-placeholder {
    background: var(--bg-sunken);
    border: 1px dashed var(--border);
    border-radius: var(--radius-md);
    color: var(--text-muted);
    font-size: 0.8rem;
    text-align: center;
    padding: 0.75rem;
    margin-bottom: 1.25rem;
}

.answer-block {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-left: 4px solid var(--accent);
    border-radius: var(--radius-md);
    padding: 1.1rem 1.25rem;
    margin-bottom: 1.75rem;
    color: var(--text-secondary);
}

.answer-block strong { color: var(--text-primary); }

/* ── Cards ───────────────────────────────────────────────────────────────── */

.calculator-card,
.content-section,
.reference-section,
.seo-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.6rem;
    margin-bottom: 1.75rem;
    box-shadow: var(--shadow-subtle);
}

.card-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-bottom: 1.1rem;
}

.card-head h2 { margin: 0; }

/* ── Verdict ─────────────────────────────────────────────────────────────── */

.verdict {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.35rem 0.85rem;
    font-size: 0.85rem;
    font-weight: 600;
    background: var(--bg-sunken);
    color: var(--text-secondary);
}

.verdict-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-muted); flex: 0 0 auto; }
.verdict-good { color: var(--state-inside); border-color: var(--state-inside); }
.verdict-good .verdict-dot { background: var(--state-inside); }
.verdict-warn { color: var(--state-marginal); border-color: var(--state-marginal); }
.verdict-warn .verdict-dot { background: var(--state-marginal); }
.verdict-bad  { color: var(--state-outside); border-color: var(--state-outside); }
.verdict-bad  .verdict-dot { background: var(--state-outside); }

/* ── Controls ────────────────────────────────────────────────────────────── */

.mode-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 1rem;
    padding-bottom: 1.1rem;
    margin-bottom: 1.1rem;
    border-bottom: 1px solid var(--border);
}

.control-group { display: flex; flex-direction: column; gap: 0.35rem; min-width: 0; }

.control-label {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.toggle-group {
    display: inline-flex;
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 3px;
    gap: 3px;
}

.toggle-btn {
    appearance: none;
    background: transparent;
    border: 0;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 600;
    padding: 0.45rem 0.85rem;
    cursor: pointer;
    min-height: 38px;
}

.toggle-btn:hover { color: var(--text-primary); background: var(--bg-hover); }
.toggle-btn.active { background: var(--accent); color: var(--on-accent); }

.control-select,
.field-input {
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: inherit;
    font-size: 1rem;
    padding: 0.55rem 0.7rem;
    min-height: 42px;
    width: 100%;
    min-width: 0;
}

.control-select:focus,
.field-input:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
    border-color: var(--accent);
}

.field-input.mono { font-family: 'JetBrains Mono', ui-monospace, monospace; }

.field-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 0.9rem 1rem;
    margin-bottom: 1.1rem;
}

.field { display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }
.field > label,
.field-label { font-size: 0.85rem; font-weight: 600; color: var(--text-secondary); }
.field-hint { font-size: 0.78rem; color: var(--text-muted); line-height: 1.4; }

.subsection {
    border-top: 1px solid var(--border);
    padding-top: 1.1rem;
    margin-top: 1.1rem;
}

.subsection h3 { font-size: 1.1rem; margin-bottom: 0.2rem; }
.subsection-lead { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 0.9rem; }

.row-tools { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-bottom: 1.1rem; }

.action-btn {
    appearance: none;
    background: var(--bg-hover);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 600;
    padding: 0.6rem 1.1rem;
    min-height: 44px;
}

.action-btn:hover { border-color: var(--border-hover); background: var(--bg-surface); }
.action-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.action-btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

/* ── Warnings ────────────────────────────────────────────────────────────── */

.input-warnings {
    background: var(--bg-sunken);
    border: 1px solid var(--warning);
    border-left: 4px solid var(--warning);
    border-radius: var(--radius-md);
    padding: 0.8rem 1rem;
    margin-bottom: 1.1rem;
    font-size: 0.9rem;
}

.input-warnings ul { margin: 0; padding-left: 1.1rem; }
.input-warnings li + li { margin-top: 0.35rem; }

/* ── Results ─────────────────────────────────────────────────────────────── */

.results-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(158px, 1fr));
    gap: 0.85rem;
    margin-bottom: 1.4rem;
}

.stat-card {
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 0.9rem 1rem;
    min-width: 0;
}

.stat-card.is-good  { border-color: var(--state-inside); }
.stat-card.is-warn  { border-color: var(--state-marginal); }
.stat-card.is-alert { border-color: var(--state-outside); }

.stat-label {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 0.3rem;
}

.stat-value {
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 1.5rem;
    font-weight: 500;
    color: var(--text-primary);
    line-height: 1.15;
    overflow-wrap: anywhere;
}

.stat-note { font-size: 0.78rem; color: var(--text-muted); margin-top: 0.25rem; line-height: 1.35; }

/* ── The tolerance-zone diagram ──────────────────────────────────────────── */

.visual-section { margin-bottom: 1.4rem; }
.visual-head h3 { margin-bottom: 0.3rem; }
.visual-head p { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 0.8rem; }

.zone-wrap {
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 0.75rem;
    overflow-x: auto;
}

#zoneDiagram {
    display: block;
    width: 880px;
    max-width: 100%;
    height: auto;
    touch-action: none;
}

#zoneDiagram .pin-handle { cursor: ns-resize; }
#zoneDiagram text { user-select: none; }

.canvas-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    margin-top: 0.7rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.legend-item { display: inline-flex; align-items: center; gap: 0.4rem; }

.legend-swatch {
    width: 14px;
    height: 14px;
    border-radius: 3px;
    border: 1px solid var(--border);
    flex: 0 0 auto;
}

/* ── Candidate list ──────────────────────────────────────────────────────── */

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

.candidate {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 0.75rem;
    align-items: center;
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 0.7rem 0.9rem;
}

.candidate.is-best { border-color: var(--accent); background: var(--accent-glow); }

.candidate-name {
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 1.05rem;
    font-weight: 500;
    color: var(--text-primary);
}

.candidate-detail { font-size: 0.82rem; color: var(--text-muted); }

.candidate-margin {
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 0.9rem;
    white-space: nowrap;
    color: var(--text-secondary);
}

/* ── Tables ──────────────────────────────────────────────────────────────── */

.table-scroll {
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-sunken);
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    min-width: 520px;
}

.data-table th,
.data-table td {
    padding: 0.55rem 0.7rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.data-table th {
    color: var(--text-primary);
    font-weight: 600;
    font-size: 0.82rem;
    background: var(--bg-hover);
    position: sticky;
    top: 0;
}

.data-table td { color: var(--text-secondary); }
.data-table td.num,
.data-table th.num { text-align: right; font-family: 'JetBrains Mono', ui-monospace, monospace; }
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover td { background: var(--bg-hover); }
.data-table .row-name { color: var(--text-primary); font-weight: 500; white-space: normal; min-width: 130px; }
.data-table tr.is-current td { background: var(--accent-glow); }

.table-note { font-size: 0.82rem; color: var(--text-muted); margin: 0.6rem 0 0; }

/* ── Prose ───────────────────────────────────────────────────────────────── */

.content-section h2 { margin-top: 0; }
.content-section h3 { margin-top: 1.6rem; }
.content-section p { color: var(--text-secondary); }
.content-section strong { color: var(--text-primary); }

.equation-block {
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 1rem 1.1rem;
    margin: 1rem 0 1.2rem;
    overflow-x: auto;
}

.equation-title {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: 0.45rem;
}

.equation {
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 1rem;
    color: var(--text-primary);
    margin: 0 0 0.9rem;
    white-space: nowrap;
}

.equation:last-child { margin-bottom: 0; }

.caveat {
    color: var(--text-muted);
    font-size: 0.92rem;
    border-left: 3px solid var(--border);
    padding-left: 0.9rem;
}

.callout {
    background: var(--bg-sunken);
    border: 1px solid var(--border);
    border-left: 4px solid var(--warning);
    border-radius: var(--radius-md);
    padding: 1rem 1.1rem;
    margin: 1.2rem 0;
}

.callout h4 { margin-bottom: 0.5rem; }
.callout p:last-child { margin-bottom: 0; }
.callout-note  { border-left-color: var(--accent); }
.callout-fence { border-left-color: var(--error); }

/* ── Reference ───────────────────────────────────────────────────────────── */

.ref-head {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.8rem;
}

.ref-head h2 { margin-bottom: 0.3rem; }
.ref-head p { color: var(--text-muted); font-size: 0.9rem; margin: 0; max-width: 60ch; }

.ref-controls { display: flex; flex-wrap: wrap; gap: 0.7rem; }
.ref-controls .control-group { min-width: 170px; }
.ref-count { font-size: 0.82rem; color: var(--text-muted); margin: 0 0 0.6rem; }
.source-note { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.9rem; line-height: 1.5; }
.source-note strong { color: var(--text-secondary); }

/* ── FAQ ─────────────────────────────────────────────────────────────────── */

.faq-section details {
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-sunken);
    padding: 0.85rem 1rem;
    margin-bottom: 0.7rem;
}

.faq-section summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--text-primary);
    font-size: 1rem;
    min-height: 30px;
}

.faq-section details p { margin: 0.7rem 0 0; color: var(--text-secondary); font-size: 0.94rem; }

/* ── Responsive ──────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
    h1 { font-size: 2rem; }
    h2 { font-size: 1.45rem; }
    .app-header { padding: 2rem 1rem 1.5rem; }
    .app-main { padding: 1.25rem 0.75rem 2.5rem; }
    .calculator-card,
    .content-section,
    .reference-section,
    .seo-content { padding: 1.1rem; }
    .mode-bar { flex-direction: column; align-items: stretch; }
    .toggle-group { flex-wrap: wrap; }
    .field-grid { grid-template-columns: 1fr; }
    .candidate { grid-template-columns: 1fr; gap: 0.3rem; }
    .candidate-margin { text-align: left; }
    .equation { white-space: normal; }
    .ref-controls { width: 100%; }
    .ref-controls .control-group { min-width: 0; flex: 1 1 140px; }
    .stat-value { font-size: 1.3rem; }
}
