/*
 * Global stylesheet — Phase 9 (UI & Responsiveness).
 *
 * This app has no CSS framework and no build step (architecture.md §1/§3 —
 * dependency-free, plain PHP MVC). Every view already carries its own
 * inline `style=""` for LAYOUT (widths, padding, flex) — that convention is
 * kept as-is; this file exists to own everything views should NOT hand-roll
 * per-page: color (so light/dark theming is centralized, not per-view),
 * responsive breakpoints, the persistent nav, form-control sizing/touch
 * targets, and a safety net so no table can ever overflow a small screen
 * (CLAUDE.md §6 lists "major mobile layout failure" as a blocking
 * condition). See docs/build-log.md's "Phase 9" entry for the reasoning
 * behind what's centralized here vs. left as per-view inline styles.
 */

/* ---------- Theme tokens (light default, dark via prefers-color-scheme) ---------- */
:root {
    --bg: #ffffff;
    --bg-subtle: #f7f5f2;
    --bg-info: #eef5fd;
    --surface: #ffffff;
    --text: #1f2320;
    --muted: #55524d;
    --muted-light: #857f76;
    --border: #d8d3ca;
    --accent: #8a5a34;
    --accent-contrast: #ffffff;
    --link: #4a6fa5;
    --success: #1f7a3d;
    --success-bg: #e9f6ee;
    --danger: #b3261e;
    --danger-bg: #fdecea;
    --focus-ring: #4a6fa5;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #17181a;
        --bg-subtle: #1f2123;
        --bg-info: #17293b;
        --surface: #1f2123;
        --text: #ece9e4;
        --muted: #b8b3aa;
        --muted-light: #8d887f;
        --border: #3a3d40;
        --accent: #c9976a;
        --accent-contrast: #17181a;
        --link: #8fb2e8;
        --success: #6fcf8f;
        --success-bg: #163224;
        --danger: #e5847d;
        --danger-bg: #3a1d1b;
        --focus-ring: #8fb2e8;
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    }
}

/* An explicit in-app choice (future light/dark toggle, or an embedding
   host) always wins over the OS-level preference above. Not wired to a UI
   control yet (no such toggle exists in this app — see build-log), but the
   selector is here so one can be added later without a CSS rewrite. */
:root[data-theme="dark"] {
    --bg: #17181a; --bg-subtle: #1f2123; --bg-info: #17293b; --surface: #1f2123;
    --text: #ece9e4; --muted: #b8b3aa; --muted-light: #8d887f; --border: #3a3d40;
    --accent: #c9976a; --accent-contrast: #17181a; --link: #8fb2e8;
    --success: #6fcf8f; --success-bg: #163224; --danger: #e5847d; --danger-bg: #3a1d1b;
    --focus-ring: #8fb2e8; --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}
:root[data-theme="light"] {
    --bg: #ffffff; --bg-subtle: #f7f5f2; --bg-info: #eef5fd; --surface: #ffffff;
    --text: #1f2320; --muted: #55524d; --muted-light: #857f76; --border: #d8d3ca;
    --accent: #8a5a34; --accent-contrast: #ffffff; --link: #4a6fa5;
    --success: #1f7a3d; --success-bg: #e9f6ee; --danger: #b3261e; --danger-bg: #fdecea;
    --focus-ring: #4a6fa5; --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

/* ---------- Base / reset ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    line-height: 1.5;
}
img { max-width: 100%; }
a { color: var(--link); }
a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
}
h1, h2, h3 { line-height: 1.25; }

/* ---------- Form controls — theme-aware + touch-friendly (min 44px target, CLAUDE.md §5 mobile) ---------- */
input, select, textarea, button {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.5rem 0.6rem;
    min-height: 44px;
    max-width: 100%;
}
/* A <select> without an explicit width sizes to its widest <option> text
   (intrinsic content width) rather than its container — on a narrow phone
   a single long option label can force page-level horizontal overflow even
   though every other element behaves. max-width:100% here is a global
   safety net for every select in the app (~90 views), not just the ones
   that happen to set width:100% inline. Found via automated Phase 9
   testing on admin/sms/compose.php's template picker. */
textarea { min-height: 88px; }
button, input[type="submit"], input[type="button"] {
    background: var(--accent);
    color: var(--accent-contrast);
    border-color: var(--accent);
    cursor: pointer;
    min-width: 44px;
}
button:hover, input[type="submit"]:hover { filter: brightness(1.08); }
select { min-height: 44px; }
label { color: var(--text); }

/* ---------- Flash messages ---------- */
.flash {
    max-width: 1100px;
    margin: 1rem auto 0;
    padding: 0.75rem 1rem;
    border-radius: 4px;
    font-size: 0.95rem;
}
.flash-success { background: var(--success-bg); color: var(--success); border: 1px solid var(--success); }
.flash-error { background: var(--danger-bg); color: var(--danger); border: 1px solid var(--danger); }

/* ---------- Tables — theme-aware borders everywhere, no HTML changes needed ---------- */
table { border-color: var(--border); background: var(--surface); }
th, td { border-color: var(--border) !important; padding: 0.5rem 0.6rem; }
thead th { background: var(--bg-subtle); text-align: left; }

/*
 * Responsive safety net (CLAUDE.md §6 blocking condition: "major mobile
 * layout failure"; §5: "no broken tables"). Every <table> in this app
 * (~45 of them across list/report views) gets this automatically — no
 * per-view markup change required. At <=768px a table becomes a
 * horizontally-scrollable block instead of overflowing the viewport or
 * getting silently clipped.
 */
@media (max-width: 768px) {
    table:not(.table-cards) {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
    }
}

/*
 * True responsive "card" table layout — opt-in via class="table-cards" plus
 * a data-label="Column Name" attribute on every <td> (CLAUDE.md §5: "use
 * responsive card layouts on small screens for complex tables"). Applied to
 * the highest-traffic, widest tables (Clients, Bookings, Invoices — see
 * build-log "Phase 9" for why those three and not all ~45 tables). Every
 * other table falls back to the horizontal-scroll safety net above, which
 * still satisfies "no horizontal overflow / no cut-off controls" — it just
 * isn't the card treatment specifically.
 */
@media (max-width: 640px) {
    table.table-cards, table.table-cards thead, table.table-cards tbody, table.table-cards tr, table.table-cards td {
        display: block;
        width: 100%;
    }
    table.table-cards thead { position: absolute; left: -9999px; top: -9999px; }
    table.table-cards tr {
        margin-bottom: 0.75rem;
        border: 1px solid var(--border);
        border-radius: 6px;
        padding: 0.5rem 0.75rem;
        box-shadow: var(--shadow);
    }
    table.table-cards td {
        border: none !important;
        border-bottom: 1px solid var(--border) !important;
        padding: 0.4rem 0;
        text-align: right;
    }
    table.table-cards td:last-child { border-bottom: none !important; }
    table.table-cards td::before {
        content: attr(data-label);
        float: left;
        font-weight: 600;
        color: var(--muted);
        margin-right: 1rem;
    }
}

/* ---------- Persistent nav (CSS-only mobile hamburger — no JS dependency, matches the app's JS-free convention for structural chrome) ---------- */
.site-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.site-header-bar {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.site-brand { font-weight: 700; color: var(--text); text-decoration: none; font-size: 1.05rem; }
.nav-toggle-label {
    display: none;
    min-width: 44px;
    min-height: 44px;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    font-size: 1.3rem;
    line-height: 1;
}
#nav-toggle { display: none; }
.site-nav {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1rem 0.75rem;
}
.site-nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1rem;
}
.site-nav a {
    display: inline-block;
    padding: 0.4rem 0.2rem;
    color: var(--text);
    text-decoration: none;
    border-bottom: 2px solid transparent;
}
.site-nav a:hover, .site-nav a.active { border-bottom-color: var(--accent); color: var(--accent); }
.nav-badge {
    display: inline-block;
    background: var(--danger);
    color: #fff;
    border-radius: 10px;
    padding: 0 0.4rem;
    font-size: 0.75rem;
    margin-left: 0.25rem;
}
.nav-group-label {
    color: var(--muted-light);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 0.4rem 0.2rem 0 0;
    align-self: center;
}

@media (max-width: 768px) {
    .nav-toggle-label { display: inline-flex; }
    .site-nav {
        display: none;
        padding-top: 0.5rem;
        border-top: 1px solid var(--border);
    }
    .site-nav ul { flex-direction: column; gap: 0; }
    .site-nav a { padding: 0.6rem 0.2rem; border-bottom: 1px solid var(--border); }
    #nav-toggle:checked ~ .site-nav { display: block; }
}

/* ---------- Small layout helpers used by a handful of views ---------- */
.kpi-cards { display: flex; flex-wrap: wrap; gap: 1rem; }
.kpi-card {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 6px;
    padding: 0.75rem 1rem;
    min-width: 180px;
    box-shadow: var(--shadow);
}
.kpi-card .kpi-label { font-size: 0.85rem; color: var(--muted); }
.kpi-card .kpi-value { font-size: 1.4rem; font-weight: bold; }

/* ---------- Auth pages (login / forgot-password / reset-password) ----------
   Scoped to .auth-* classes only, so this never touches the shared
   input/select/textarea/button rules ~90 other views rely on (Phase 9
   regression discipline). Brand navy is intentionally constant across
   light/dark mode — it's the studio's mark, not a themed surface — while
   the card itself still uses var(--surface)/var(--text)/var(--muted) so
   these pages still respect the visitor's OS theme. */
.auth-page {
    --auth-brand: #13134e;
    min-height: 100vh;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
    background: var(--bg-subtle);
}
.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    overflow: hidden;
}
.auth-brand {
    background: var(--auth-brand);
    padding: 2.25rem 1.5rem 2rem;
    text-align: center;
}
.auth-logo {
    display: inline-block;
    width: 100%;
    max-width: 210px;
    height: auto;
}
.auth-body {
    padding: 2rem 1.75rem 2.25rem;
    box-sizing: border-box;
}
.auth-title {
    margin: 0 0 0.35rem;
    font-size: 1.5rem;
    text-align: center;
    color: var(--text);
}
.auth-subtitle {
    margin: 0 0 1.5rem;
    color: var(--muted);
    text-align: center;
    font-size: 0.95rem;
}
.auth-field { margin-bottom: 1.1rem; }
.auth-field label {
    display: block;
    margin-bottom: 0.35rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
}
.auth-field input { width: 100%; box-sizing: border-box; }
.auth-field .field-error { color: var(--danger); font-size: 0.85rem; margin: 0.3rem 0 0; }
.auth-submit {
    width: 100%;
    background: var(--auth-brand);
    border-color: var(--auth-brand);
    color: #ffffff;
    font-weight: 600;
    margin-top: 0.25rem;
}
.auth-links { text-align: center; margin: 1.5rem 0 0; font-size: 0.9rem; }
.auth-links + .auth-links { margin-top: 0.5rem; }

@media (max-width: 400px) {
    .auth-brand { padding: 1.75rem 1.25rem 1.5rem; }
    .auth-body { padding: 1.5rem 1.25rem 1.75rem; }
}
