/* =========================================================================
   Accessible Database Manager — global styles
   Priorities: strong colour contrast, always-visible focus, generous target
   sizes, text scaling, reduced-motion support, and clean semantics for
   screen readers / braille displays.
   ========================================================================= */

/* ---- Theme tokens ------------------------------------------------------- */
:root {
    --font-body: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

    --bg:            #ffffff;
    --bg-elevated:   #f4f6f9;
    --bg-sunken:     #eaeef3;
    --text:          #14181d;
    --text-muted:    #4a5560;
    --border:        #b7c0cb;
    --border-strong: #7a8794;
    --accent:        #12508f;   /* AA on white and on --bg-elevated */
    --accent-text:   #ffffff;
    --accent-hover:  #0d3d6e;
    --focus:         #b5178e;   /* high-contrast focus ring, distinct from accent */
    --danger:        #a1121f;
    --danger-hover:  #7d0e18;
    --success:       #0f6d3d;
    --warning-bg:    #fff4d6;
    --warning-text:  #5a4300;

    --radius: 8px;
    --space:  1rem;
    --tap:    2.75rem;          /* minimum interactive target ~44px */
    --maxw:   72rem;
}

/* Dark theme */
:root[data-theme="dark"],
:root[data-theme="system"] {
    color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
    :root[data-theme="system"] {
        --bg:            #10151b;
        --bg-elevated:   #1a2129;
        --bg-sunken:     #0b0f14;
        --text:          #eef2f6;
        --text-muted:    #aeb9c4;
        --border:        #38434f;
        --border-strong: #5a6875;
        --accent:        #7ab6f5;
        --accent-text:   #06121f;
        --accent-hover:  #a3ccf8;
        --focus:         #ff86d6;
        --danger:        #ff8a93;
        --danger-hover:  #ffb0b6;
        --success:       #6bdc9f;
        --warning-bg:    #3a2f00;
        --warning-text:  #ffe08a;
    }
}
:root[data-theme="dark"] {
    --bg:            #10151b;
    --bg-elevated:   #1a2129;
    --bg-sunken:     #0b0f14;
    --text:          #eef2f6;
    --text-muted:    #aeb9c4;
    --border:        #38434f;
    --border-strong: #5a6875;
    --accent:        #7ab6f5;
    --accent-text:   #06121f;
    --accent-hover:  #a3ccf8;
    --focus:         #ff86d6;
    --danger:        #ff8a93;
    --danger-hover:  #ffb0b6;
    --success:       #6bdc9f;
    --warning-bg:    #3a2f00;
    --warning-text:  #ffe08a;
}

/* High-contrast theme (manual) */
:root[data-theme="contrast"] {
    --bg:            #000000;
    --bg-elevated:   #000000;
    --bg-sunken:     #000000;
    --text:          #ffffff;
    --text-muted:    #ffffff;
    --border:        #ffffff;
    --border-strong: #ffffff;
    --accent:        #ffff00;
    --accent-text:   #000000;
    --accent-hover:  #ffffff;
    --focus:         #00ffff;
    --danger:        #ff6b6b;
    --danger-hover:  #ffffff;
    --success:       #00ff88;
    --warning-bg:    #000000;
    --warning-text:  #ffff00;
}

/* ---- Text-size preference ---------------------------------------------- */
:root { font-size: 100%; }
:root[data-text-size="small"]  { font-size: 90%; }
:root[data-text-size="large"]  { font-size: 118%; }
:root[data-text-size="xlarge"] { font-size: 136%; }

/* ---- Reset-ish ---------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    font-family: var(--font-body);
    line-height: 1.55;
    color: var(--text);
    background: var(--bg);
}
h1, h2, h3 { line-height: 1.2; }
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

/* ---- Focus visibility (never remove it) -------------------------------- */
:focus-visible {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
    border-radius: 3px;
}
/* Fallback for browsers without :focus-visible */
a:focus, button:focus, input:focus, select:focus, textarea:focus, [tabindex]:focus {
    outline: 3px solid var(--focus);
    outline-offset: 2px;
}

/* ---- Skip link ---------------------------------------------------------- */
.skip-link {
    position: absolute;
    left: 0.5rem;
    top: -3rem;
    z-index: 1000;
    padding: 0.6rem 1rem;
    background: var(--accent);
    color: var(--accent-text);
    border-radius: var(--radius);
    transition: top 0.15s ease;
}
.skip-link:focus { top: 0.5rem; }

/* ---- Screen-reader-only helpers ---------------------------------------- */
.visually-hidden {
    position: absolute !important;
    width: 1px; height: 1px;
    padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0);
    white-space: nowrap; border: 0;
}

/* ---- Layout ------------------------------------------------------------- */
.site-header {
    background: var(--bg-elevated);
    border-bottom: 2px solid var(--border);
}
.site-header__inner, .site-main, .site-footer > * {
    max-width: var(--maxw);
    margin: 0 auto;
    padding: 0.5rem var(--space);
}
.site-header__inner {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}
.site-main {
    display: block;
    padding-top: 1.5rem;
    padding-bottom: 3rem;
    min-height: 60vh;
}
.site-footer {
    border-top: 2px solid var(--border);
    color: var(--text-muted);
    background: var(--bg-elevated);
}
.site-footer p { max-width: var(--maxw); margin: 0 auto; padding: 1rem var(--space); }

/* ---- Brand + nav -------------------------------------------------------- */
.brand {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.15rem;
    color: var(--text);
    text-decoration: none;
}
.brand__mark { font-size: 1.4rem; }
.main-nav { display: flex; align-items: center; }
.nav-toggle {
    display: none;
    min-height: var(--tap);
    align-items: center;
    gap: 0.5rem;
}
.nav-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
    list-style: none;
    margin: 0;
    padding: 0;
}
.nav-list a {
    display: inline-flex;
    align-items: center;
    min-height: var(--tap);
    padding: 0.25rem 0.5rem;
    color: var(--text);
    text-decoration: none;
    border-radius: var(--radius);
}
.nav-list a:hover { background: var(--bg-sunken); text-decoration: underline; }
.nav-list a[aria-current="page"] {
    font-weight: 700;
    text-decoration: underline;
    text-decoration-thickness: 3px;
    text-underline-offset: 4px;
}

/* The sign-out control is a real form button styled to sit in the nav. */
.nav-signout { display: inline; }
.nav-signout__btn {
    display: inline-flex;
    align-items: center;
    min-height: var(--tap);
    padding: 0.25rem 0.5rem;
    font: inherit;
    color: var(--text);
    background: none;
    border: none;
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: underline;
}
.nav-signout__btn:hover { background: var(--bg-sunken); }

@media (max-width: 720px) {
    .nav-toggle { display: inline-flex; }
    .nav-list {
        display: none;
        flex-direction: column;
        width: 100%;
    }
    .main-nav[data-open="true"] .nav-list { display: flex; }
}

/* ---- Flash / notifications --------------------------------------------- */
.flash-stack { max-width: var(--maxw); margin: 1rem auto 0; padding: 0 var(--space); }
.flash {
    padding: 0.75rem 1rem;
    border: 2px solid var(--border-strong);
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
    background: var(--bg-elevated);
}
.flash--error   { border-color: var(--danger);  background: color-mix(in srgb, var(--danger) 12%, var(--bg)); }
.flash--success { border-color: var(--success); background: color-mix(in srgb, var(--success) 12%, var(--bg)); }
.flash--info    { border-color: var(--accent); }

/* ---- Typography helpers ------------------------------------------------- */
.page-title { margin: 0 0 0.25rem; font-size: 1.75rem; }
.page-lead  { color: var(--text-muted); margin: 0 0 1.5rem; }
.muted { color: var(--text-muted); }

/* ---- Cards / panels ----------------------------------------------------- */
.card {
    background: var(--bg-elevated);
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
}
.card + .card { margin-top: 1rem; }
.grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
}

/* ---- Talking menu ------------------------------------------------------- */
.talk-options { font-size: 1.2rem; line-height: 1.9; margin: 1rem 0; }
.talk-options li { margin-bottom: 0.25rem; }
.talk-fields { font-size: 1.15rem; line-height: 1.7; margin: 0.5rem 0 1rem; }
.talk-fields > div { padding: 0.2rem 0; border-bottom: 1px solid var(--border); }
.talk-form { display: flex; flex-wrap: wrap; align-items: end; gap: 0.75rem; margin: 1rem 0; }
.talk-form label { flex-basis: 100%; font-weight: 600; }
.talk-form .input { max-width: 8rem; font-size: 1.4rem; text-align: center; }

/* ---- Toggle switch (Simple mode) ---------------------------------------- */
.simple-toggle { display: flex; flex-direction: column; gap: 0.4rem; }
.switch {
    display: inline-flex;
    align-items: center;
    gap: 0.6rem;
    cursor: pointer;
    font-weight: 600;
    font-size: 1.1rem;
}
/* The real checkbox stays operable and focusable but visually replaced by the
   track/thumb; it keeps role="switch" semantics for screen readers. */
.switch input {
    position: absolute;
    opacity: 0;
    width: 3.2rem;
    height: 1.8rem;
    margin: 0;
    cursor: pointer;
}
.switch__track {
    position: relative;
    flex: 0 0 auto;
    width: 3.2rem;
    height: 1.8rem;
    border-radius: 1rem;
    background: var(--border);
    border: 2px solid var(--border);
    transition: background 0.15s ease;
}
.switch__thumb {
    position: absolute;
    top: 0.15rem;
    left: 0.15rem;
    width: 1.3rem;
    height: 1.3rem;
    border-radius: 50%;
    background: var(--bg-elevated);
    transition: transform 0.15s ease;
}
.switch input:checked + .switch__track { background: var(--accent, #2563eb); border-color: var(--accent, #2563eb); }
.switch input:checked + .switch__track .switch__thumb { transform: translateX(1.4rem); }
.switch input:focus-visible + .switch__track { outline: 3px solid var(--focus, Highlight); outline-offset: 2px; }
@media (forced-colors: active) {
    .switch__track { background: ButtonFace; }
    .switch input:checked + .switch__track { background: Highlight; }
    .switch__thumb { background: ButtonText; }
}

/* ---- Database summary --------------------------------------------------- */
.summary-spoken {
    font-size: 1.25rem;
    line-height: 1.6;
    margin: 0.5rem 0 1rem;
}
.summary-grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: repeat(auto-fill, minmax(18rem, 1fr));
    margin-top: 0.5rem;
}
.summary-grid .card { padding: 1rem; }
.summary-grid h3 { margin: 0 0 0.5rem; font-size: 1.05rem; }
.summary-dl { margin: 0; }
.summary-dl > div {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.35rem 0;
    border-bottom: 1px solid var(--border);
}
.summary-dl > div:last-child { border-bottom: 0; }
.summary-dl dt { color: var(--text-muted); margin: 0; }
.summary-dl dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 600; }
.summary-table { width: 100%; margin: 0; }
.summary-table td { font-variant-numeric: tabular-nums; }

/* ---- Reading mode (distraction-free linear records) --------------------- */
.reading { max-width: 44rem; }
.reading-record {
    padding: 1.25rem 0;
    border-bottom: 2px solid var(--border);
}
.reading-record__title { font-size: 1.4rem; line-height: 1.3; margin: 0 0 0.5rem; }
.reading-record__num { color: var(--text-muted); font-weight: 400; }
.reading-record__fields { margin: 0; }
.reading-line {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 0.6rem;
    padding: 0.3rem 0;
    font-size: 1.1rem;
    line-height: 1.5;
}
.reading-line dt { font-weight: 700; margin: 0; }
.reading-line dt::after { content: ":"; }
.reading-line dd { margin: 0; }

/* ---- Forms -------------------------------------------------------------- */
.form { max-width: 32rem; }
.form--wide { max-width: 48rem; }
.field { margin-bottom: 1.1rem; }
.field > label, .field > .field-label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.3rem;
}
.field .hint { display: block; color: var(--text-muted); font-size: 0.9rem; margin-bottom: 0.3rem; }
.field .error-text { display: block; color: var(--danger); font-weight: 600; margin-top: 0.3rem; }
.required-star { color: var(--danger); }

input[type="text"], input[type="email"], input[type="password"], input[type="number"],
input[type="date"], input[type="datetime-local"], input[type="url"], input[type="tel"],
input[type="search"], select, textarea {
    width: 100%;
    min-height: var(--tap);
    padding: 0.55rem 0.7rem;
    font: inherit;
    color: var(--text);
    background: var(--bg);
    border: 2px solid var(--border-strong);
    border-radius: var(--radius);
}
textarea { min-height: 6rem; resize: vertical; }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] {
    border-color: var(--danger);
}
fieldset { border: 2px solid var(--border); border-radius: var(--radius); padding: 1rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 0.4rem; }

/* ---- Buttons ------------------------------------------------------------ */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    min-height: var(--tap);
    padding: 0.5rem 1.1rem;
    font: inherit;
    font-weight: 600;
    color: var(--accent-text);
    background: var(--accent);
    border: 2px solid var(--accent);
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.btn--secondary {
    color: var(--text);
    background: var(--bg);
    border-color: var(--border-strong);
}
.btn--secondary:hover { background: var(--bg-sunken); color: var(--text); }
.btn--danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn--danger:hover { background: var(--danger-hover); border-color: var(--danger-hover); }
.btn--small { min-height: 2.25rem; padding: 0.25rem 0.7rem; font-size: 0.9rem; }
.btn-row { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; margin-top: 1rem; }

/* ---- Tables / data grid ------------------------------------------------- */
.table-wrap {
    overflow-x: auto;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    /* Keep keyboard-focused rows from hiding beneath the sticky header
       when the container scrolls (WCAG 2.4.11 Focus Not Obscured). */
    scroll-padding-top: 3.5rem;
}
table.data {
    width: 100%;
    border-collapse: collapse;
    background: var(--bg);
}
table.data caption { text-align: left; padding: 0.6rem; font-weight: 700; }
table.data th, table.data td {
    text-align: left;
    padding: 0.55rem 0.7rem;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
table.data thead th {
    background: var(--bg-sunken);
    border-bottom: 2px solid var(--border-strong);
    position: sticky;
    top: 0;
}
table.data tbody tr:hover { background: var(--bg-elevated); }
table.data th a { color: var(--text); text-decoration: underline; }

/* ---- Definition-style record view for very small screens --------------- */
.record-actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }

/* ---- Utility ------------------------------------------------------------ */
.stack > * + * { margin-top: 1rem; }
.cluster { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; }
.spread { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.pill {
    display: inline-block;
    padding: 0.1rem 0.55rem;
    border: 1px solid var(--border-strong);
    border-radius: 999px;
    font-size: 0.85rem;
}
.pill--read  { background: color-mix(in srgb, var(--accent) 12%, var(--bg)); }
.pill--write, .pill--readwrite { background: color-mix(in srgb, var(--success) 14%, var(--bg)); }
.warning-box {
    background: var(--warning-bg);
    color: var(--warning-text);
    border: 2px solid currentColor;
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
}
.otp-input {
    letter-spacing: 0.5em;
    text-align: center;
    font-family: var(--font-mono);
    font-size: 1.5rem;
    max-width: 12rem;
}
code, kbd { font-family: var(--font-mono); }
kbd {
    padding: 0.05rem 0.4rem;
    border: 1px solid var(--border-strong);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--bg-sunken);
    font-size: 0.9em;
}

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

/* ---- Forced-colors (Windows High Contrast) ------------------------------ */
@media (forced-colors: active) {
    .btn { border: 2px solid ButtonText; }
    :focus-visible { outline: 3px solid Highlight; }
}
