/* ==========================================================================
   DentCare — base.css
   Design tokens, reset, typography, layout helpers and shared components.
   Loaded on every page (public site, patient portal, admin panel).

   Layout rule (Pages doc §2): pages use the FULL width at every size.
   No page-level max-width wrapper; bigger screens get more columns, not empty sides.
   ========================================================================== */

/* ---------- 1. Design tokens ---------- */
:root {
    /* Brand */
    --brand-50: #f0fdfa;
    --brand-100: #ccfbf1;
    --brand-200: #99f6e4;
    --brand-500: #14b8a6;
    --brand-600: #0d9488;
    --brand-700: #0f766e;
    --brand-800: #115e59;
    --brand-900: #134e4a;

    /* Neutrals */
    --ink: #0f172a;
    --ink-2: #1e293b;
    --muted: #475569;
    --subtle: #64748b;
    --line: #e2e8f0;
    --line-strong: #cbd5e1;
    --surface: #ffffff;
    --surface-2: #f8fafc;
    --surface-3: #f1f5f9;

    /* Status */
    --success: #15803d;
    --success-bg: #dcfce7;
    --warning: #b45309;
    --warning-bg: #fef3c7;
    --danger: #b91c1c;
    --danger-bg: #fee2e2;
    --info: #1d4ed8;
    --info-bg: #dbeafe;

    /* Type */
    --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    --text-sm: clamp(0.8125rem, 0.1vw + 0.78rem, 0.875rem);
    --text-base: clamp(1rem, 0.15vw + 0.96rem, 1.0625rem);
    --text-lg: clamp(1.125rem, 0.3vw + 1.05rem, 1.25rem);
    --text-xl: clamp(1.25rem, 0.6vw + 1.1rem, 1.5rem);
    --text-2xl: clamp(1.5rem, 1.2vw + 1.2rem, 2rem);
    --text-3xl: clamp(1.875rem, 2.2vw + 1.3rem, 2.75rem);
    --text-4xl: clamp(2.25rem, 3.5vw + 1.3rem, 4rem);

    /* Space & layout (fluid, full width) */
    --gutter: clamp(16px, 4vw, 96px);
    --section-y: clamp(48px, 7vw, 128px);
    --gap: clamp(16px, 2vw, 32px);
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;

    /* Shape */
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow: 0 4px 16px -4px rgba(15, 23, 42, 0.12);
    --shadow-lg: 0 20px 40px -16px rgba(15, 23, 42, 0.25);
    --focus: 0 0 0 3px rgba(20, 184, 166, 0.45);

    /* Motion */
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
    --fast: 150ms;
}

/* ---------- 2. Reset ---------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scroll-behavior: smooth; }

body {
    margin: 0;
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--ink);
    background: var(--surface);
    -webkit-font-smoothing: antialiased;
    overflow-wrap: break-word;
}

img, svg, video, canvas { display: block; max-width: 100%; height: auto; }
input, button, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--brand-700); text-underline-offset: 3px; }
a:hover { color: var(--brand-800); }
table { border-collapse: collapse; width: 100%; }
[hidden] { display: none !important; }

:focus-visible { outline: none; box-shadow: var(--focus); border-radius: 4px; }

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

/* ---------- 3. Typography ---------- */
h1, h2, h3, h4 { margin: 0 0 0.5em; line-height: 1.2; letter-spacing: -0.02em; color: var(--ink); }
h1 { font-size: var(--text-3xl); }
h2 { font-size: var(--text-2xl); }
h3 { font-size: var(--text-xl); }
h4 { font-size: var(--text-lg); }
p { margin: 0 0 1em; }
small, .text-sm { font-size: var(--text-sm); }
.text-muted { color: var(--muted); }
.eyebrow { margin: 0 0 var(--space-2); font-size: 0.8rem; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--brand-700); }
.lead { font-size: var(--text-lg); color: var(--muted); }
.prose { max-width: 70ch; }
code, kbd { font-family: var(--font-mono); font-size: 0.9em; }

/* ---------- 4. Layout helpers ---------- */
.section { padding: var(--section-y) var(--gutter); }
.gutter { padding-inline: var(--gutter); }
.stack > * + * { margin-top: var(--stack, var(--space-4)); }
.cluster { display: flex; flex-wrap: wrap; gap: var(--cluster-gap, var(--space-3)); align-items: center; }

/* Auto-fit grid: minimum card width + maximum number of columns (Pages doc §2 R3) */
.grid {
    --min: 280px;
    --cols: 6;
    display: grid;
    gap: var(--gap);
    grid-template-columns: repeat(auto-fill,
        minmax(min(100%, max(var(--min), (100% - (var(--cols) - 1) * var(--gap)) / var(--cols))), 1fr));
}

/* Few items (e.g. dashboard widgets): stretch to fill the row instead of leaving empty columns */
.grid--fit {
    grid-template-columns: repeat(auto-fit,
        minmax(min(100%, max(var(--min), (100% - (var(--cols) - 1) * var(--gap)) / var(--cols))), 1fr));
}

/* Split: stacked below 1024px, side by side from 1024px */
.split { display: grid; gap: var(--gap); }
@media (min-width: 1024px) {
    .split { grid-template-columns: 5fr 7fr; align-items: center; }
    .split--even { grid-template-columns: 1fr 1fr; }
}

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

.skip-link {
    position: absolute; left: var(--space-4); top: -100px; z-index: 1000;
    padding: var(--space-3) var(--space-4); background: var(--ink); color: #fff; border-radius: var(--radius-sm);
}
.skip-link:focus { top: var(--space-4); color: #fff; }

/* ---------- 5. Icons ---------- */
.icon { width: 1.25em; height: 1.25em; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon--sm { width: 1em; height: 1em; }
.icon--lg { width: 1.5em; height: 1.5em; }

/* ---------- 6. Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
    min-height: 44px; padding: 0 var(--space-5);
    border: 2px solid transparent; border-radius: var(--radius);
    font-weight: 600; line-height: 1.2; text-decoration: none; white-space: nowrap;
    transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease), color var(--fast) var(--ease);
}
.btn-primary { background: var(--brand-700); color: #fff; }
.btn-primary:hover { background: var(--brand-800); color: #fff; }
.btn-secondary { background: var(--surface); color: var(--brand-800); border-color: var(--brand-700); }
.btn-secondary:hover { background: var(--brand-50); color: var(--brand-900); }
.btn-ghost { background: transparent; color: var(--ink-2); }
.btn-ghost:hover { background: var(--surface-3); color: var(--ink); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #991b1b; color: #fff; }
.btn-sm { min-height: 36px; padding: 0 var(--space-4); font-size: var(--text-sm); border-radius: var(--radius-sm); }
.btn-block { display: flex; width: 100%; }
.btn[disabled], .btn.is-disabled { opacity: 0.55; pointer-events: none; }
.btn.is-loading { position: relative; color: transparent !important; pointer-events: none; }
.btn.is-loading::after {
    content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.5); border-top-color: #fff; animation: spin 0.7s linear infinite;
}
.btn-secondary.is-loading::after, .btn-ghost.is-loading::after { border-color: var(--line-strong); border-top-color: var(--brand-700); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- 7. Forms ---------- */
.form { display: grid; gap: var(--space-5); }
.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-2); align-content: start; min-width: 0; }
.label { font-weight: 600; font-size: var(--text-sm); color: var(--ink-2); }
.label-row { display: flex; justify-content: space-between; align-items: baseline; gap: var(--space-3); }
.hint { font-size: var(--text-sm); color: var(--subtle); }

.input, .select, .textarea {
    width: 100%; min-height: 48px; padding: 10px 14px;
    background: var(--surface); border: 1.5px solid var(--line-strong); border-radius: var(--radius);
    transition: border-color var(--fast) var(--ease), box-shadow var(--fast) var(--ease);
}
.textarea { min-height: 120px; resize: vertical; }
.input::placeholder, .textarea::placeholder { color: #94a3b8; }
.input:hover, .select:hover, .textarea:hover { border-color: #94a3b8; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--brand-600); box-shadow: var(--focus); }
.input[readonly], .input:disabled { background: var(--surface-3); color: var(--muted); }
.is-invalid, .is-invalid:hover { border-color: var(--danger); }
.is-invalid:focus { box-shadow: 0 0 0 3px rgba(185, 28, 28, 0.2); }
.field-error { display: flex; gap: 6px; align-items: flex-start; margin: 0; font-size: var(--text-sm); color: var(--danger); }

.input-group { position: relative; }
.input-group .input { padding-right: 52px; }
.input-group__btn {
    position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
    display: grid; place-items: center; width: 44px; height: 40px;
    border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--subtle);
}
.input-group__btn:hover { background: var(--surface-3); color: var(--ink); }

.check { display: flex; gap: var(--space-3); align-items: flex-start; cursor: pointer; }
.check input { width: 20px; height: 20px; margin-top: 3px; accent-color: var(--brand-700); }

/* Form field columns: 1 → 2 (768px) → 3 (1440px) (Pages doc §2 R10) */
.form-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-5); }
@media (min-width: 768px) { .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .form-grid .span-all { grid-column: 1 / -1; } }
@media (min-width: 1440px) { .form-grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

/* ---------- 8. Cards ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); }
.card__header { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); }
.card__header > div { flex: 1 1 260px; min-width: 0; }
.card__title { margin: 0; font-size: var(--text-lg); }
.card__body { padding: var(--space-5); }
.card__footer { padding: var(--space-4) var(--space-5); border-top: 1px solid var(--line); background: var(--surface-2); border-radius: 0 0 var(--radius-lg) var(--radius-lg); }

/* ---------- 9. Alerts & flash messages ---------- */
.alert {
    display: flex; gap: var(--space-3); align-items: flex-start;
    padding: var(--space-3) var(--space-4); border-radius: var(--radius); border: 1px solid transparent;
    font-size: var(--text-sm); line-height: 1.5;
}
.alert .icon { margin-top: 1px; }
.alert__text { flex: 1; }
.alert__close { margin: -6px -8px -6px 0; padding: 6px; border: 0; background: transparent; color: inherit; opacity: 0.7; border-radius: var(--radius-sm); }
.alert__close:hover { opacity: 1; }
.alert-success { background: var(--success-bg); color: #14532d; border-color: #bbf7d0; }
.alert-error { background: var(--danger-bg); color: #7f1d1d; border-color: #fecaca; }
.alert-warning { background: var(--warning-bg); color: #78350f; border-color: #fde68a; }
.alert-info { background: var(--info-bg); color: #1e3a8a; border-color: #bfdbfe; }
.flash-stack { display: grid; gap: var(--space-3); }

/* ---------- 10. Badges ---------- */
.badge {
    display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px;
    font-size: 0.75rem; font-weight: 700; line-height: 1.6; white-space: nowrap;
    background: var(--surface-3); color: var(--ink-2);
}
.badge-brand { background: var(--brand-100); color: var(--brand-800); }
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-info { background: var(--info-bg); color: var(--info); }

/* ---------- 11. Tables (cards below 768px — Pages doc §2 R9) ---------- */
.table-wrap { width: 100%; }
.table { font-size: var(--text-sm); }
.table th { text-align: left; font-weight: 600; color: var(--muted); background: var(--surface-2); }
.table th, .table td { padding: 12px var(--space-4); border-bottom: 1px solid var(--line); vertical-align: top; }
.table tbody tr:hover { background: var(--surface-2); }
.col-p2, .col-p3 { display: none; }
@media (min-width: 1024px) { .col-p2 { display: table-cell; } }
@media (min-width: 1440px) { .col-p3 { display: table-cell; } }
@media (max-width: 767.98px) {
    .table--cards thead { display: none; }
    .table--cards, .table--cards tbody, .table--cards tr, .table--cards td { display: block; width: 100%; }
    .table--cards tr { margin-bottom: var(--space-3); border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
    .table--cards td { display: flex; justify-content: space-between; gap: var(--space-4); border-bottom: 1px solid var(--line); }
    .table--cards td:last-child { border-bottom: 0; }
    .table--cards td::before { content: attr(data-label); font-weight: 600; color: var(--muted); }
    .table--cards .col-p2, .table--cards .col-p3 { display: flex; }
}

/* ---------- 12. Empty states ---------- */
.empty { display: grid; justify-items: center; gap: var(--space-3); padding: var(--space-7) var(--space-5); text-align: center; color: var(--muted); }
.empty .icon { width: 40px; height: 40px; color: var(--brand-600); }

/* ---------- 13. Demo bar ---------- */
.demo-bar {
    display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 4px 12px;
    padding: 8px var(--gutter); background: var(--ink); color: #e2e8f0; font-size: var(--text-sm); text-align: center;
}
.demo-bar a { color: var(--brand-200); font-weight: 600; }

/* ---------- 14. Toasts (created by core.js) ---------- */
.toast-region {
    position: fixed; z-index: 900; left: 16px; right: 16px; bottom: 16px;
    display: grid; gap: var(--space-2); pointer-events: none;
}
@media (min-width: 768px) { .toast-region { left: auto; top: 16px; bottom: auto; width: min(420px, calc(100vw - 32px)); } }
.toast { pointer-events: auto; box-shadow: var(--shadow-lg); animation: toast-in 200ms var(--ease); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- 15. Day 3 additions: form helpers shared by all areas ---------- */
.req { color: var(--danger); font-weight: 700; }
.field-meta { display: flex; justify-content: space-between; align-items: baseline; gap: var(--space-3); }
.field-meta .hint { margin: 0; }
.counter { flex: none; margin-left: auto; font-size: 0.75rem; font-variant-numeric: tabular-nums; color: var(--subtle); }
.counter.is-over { color: var(--warning); font-weight: 700; }

/* Input with a fixed prefix/suffix: "+91 [ 98765… ]", "[ 45 ] min" */
.affix { display: flex; align-items: stretch; min-width: 0; }
.affix .input, .affix .select { min-width: 0; flex: 1; }
.affix__text {
    display: flex; align-items: center; padding: 0 12px; flex: none; white-space: nowrap;
    background: var(--surface-3); border: 1.5px solid var(--line-strong); color: var(--muted); font-size: var(--text-sm); font-weight: 600;
}
.affix__text:first-child { border-right: 0; border-radius: var(--radius) 0 0 var(--radius); }
.affix__text:last-child { border-left: 0; border-radius: 0 var(--radius) var(--radius) 0; }
.affix__text:first-child + .input, .affix__text:first-child + .select { border-top-left-radius: 0; border-bottom-left-radius: 0; }
.affix .input:has(+ .affix__text), .affix .select:has(+ .affix__text) { border-top-right-radius: 0; border-bottom-right-radius: 0; }

.select {
    appearance: none; -webkit-appearance: none; padding-right: 40px; cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23475569' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 12px center; background-size: 18px;
}
.textarea--sm { min-height: 84px; }
.input--time { min-height: 44px; padding: 6px 10px; font-variant-numeric: tabular-nums; }

.check--sm { font-size: var(--text-sm); }
.check small { display: block; color: var(--subtle); font-size: 0.8rem; }

/* Toggle switch (a styled checkbox — still a real checkbox for forms and screen readers) */
.switch { position: relative; display: inline-flex; align-items: center; gap: var(--space-3); flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 44px; height: 26px; margin: 0; cursor: pointer; }
.switch__track {
    position: relative; flex: none; width: 44px; height: 26px; border-radius: 999px; background: var(--line-strong);
    transition: background-color var(--fast) var(--ease);
}
.switch__track::after {
    content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff;
    box-shadow: var(--shadow-sm); transition: transform var(--fast) var(--ease);
}
.switch input:checked + .switch__track { background: var(--brand-700); }
.switch input:checked + .switch__track::after { transform: translateX(18px); }
.switch input:focus-visible + .switch__track { box-shadow: var(--focus); }
.switch input:disabled + .switch__track { opacity: 0.5; }

.toggle-row { display: flex; gap: var(--space-4); align-items: flex-start; cursor: pointer; }
.toggle-row > span:last-child { display: grid; gap: 2px; }
.toggle-row small { color: var(--subtle); font-size: var(--text-sm); }
.toggle-row.is-disabled { cursor: default; }

.icon-btn {
    display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
    border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); color: var(--muted);
}
.icon-btn:hover { background: var(--surface-3); color: var(--ink); }
.icon-btn--danger:hover { background: var(--danger-bg); color: var(--danger); border-color: #fecaca; }
.icon-btn[disabled] { opacity: 0.4; pointer-events: none; }

.nowrap { white-space: nowrap; }
.color-dot { display: inline-block; width: 12px; height: 12px; flex: none; vertical-align: middle; }
@media (max-width: 479.98px) {
    .card__header, .card__body { padding-inline: var(--space-4); }
}

/* ---------- 16. Day 5: star ratings (stars_svg() helper — public site and admin) ---------- */
.stars { display: inline-block; flex: none; width: 6.25em; height: 1.25em; max-width: none; vertical-align: -0.22em; overflow: visible; }
.stars--sm { width: 5em; height: 1em; }
.stars__empty { fill: #e7ecef; stroke: #c3cdd4; stroke-width: 1.4; stroke-linejoin: round; }
.stars__fill { fill: #f4b740; stroke: #d99a17; stroke-width: 1.4; stroke-linejoin: round; }
.stars--on-dark .stars__empty { fill: rgba(255, 255, 255, 0.12); stroke: rgba(255, 255, 255, 0.35); }
/* Rating bars (5 → 1 stars) with <meter> */
.rating-bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.rating-bars li { display: grid; grid-template-columns: 2.6em minmax(0, 1fr) 2.2em; align-items: center; gap: var(--space-3); font-size: var(--text-sm); color: var(--muted); }
.rating-bars li > span:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.rating-bars meter {
    display: block; width: 100%; height: 10px; border: 0; border-radius: 999px; background: var(--surface-3);
    -webkit-appearance: none; appearance: none;
}
.rating-bars meter::-webkit-meter-bar { height: 10px; border: 0; border-radius: 999px; background: var(--surface-3); }
.rating-bars meter::-webkit-meter-optimum-value,
.rating-bars meter::-webkit-meter-suboptimum-value,
.rating-bars meter::-webkit-meter-even-less-good-value { border-radius: 999px; background: #f4b740; }
.rating-bars meter::-moz-meter-bar { border-radius: 999px; background: #f4b740; }
