:root {
    --ink: #163a3a;
    --muted: #5c7877;
    --line: #d9e8e6;
    --bg: #f2f9f8;
    --card: #ffffff;
    --brand: #0f766e;
    --brand-hover: #0b5e58;
    --brand-light: #ccf0ea;
    --brand-lighter: #e6f7f4;
    --success-bg: #e2f7ee;
    --success-ink: #0f7a54;
    --error-bg: #fdecec;
    --error-ink: #b3261e;
    --warn-bg: #fff6e0;
    --warn-ink: #8a6116;
    --tap: 48px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}
html, body { overflow-x: hidden; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    font-size: 17px;
    line-height: 1.45;
    touch-action: manipulation;
}

/* Sidebar layout */
.sidebar-toggle-checkbox { position: absolute; opacity: 0; pointer-events: none; }
.mobile-topbar { display: none; }
.sidebar-overlay { display: none; }
.mobile-tabbar { display: none; }

.app-shell { display: flex; min-height: 100vh; min-height: 100dvh; align-items: flex-start; }

.sidebar {
    width: 250px; flex-shrink: 0; position: sticky; top: 0; height: 100vh; height: 100dvh;
    background: var(--card); border-right: 1px solid var(--line);
    display: flex; flex-direction: column; padding: 20px 16px;
    overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; color: var(--ink); text-decoration: none; font-size: 1.05rem; padding: 0 6px 16px; }
.brand-mark {
    width: 28px; height: 28px; border-radius: 22%;
    display: inline-block; flex-shrink: 0; object-fit: contain;
}
.sidebar-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.sidebar-nav a, .sidebar-footer a, .sidebar-footer .link-button {
    color: var(--muted); text-decoration: none; padding: 11px 12px; border-radius: 8px; font-size: 1rem; font-weight: 500;
    display: block; min-height: 44px;
}
.sidebar-nav a:hover, .sidebar-footer a:hover, .sidebar-footer .link-button:hover { color: var(--ink); background: var(--brand-lighter); }
.sidebar-nav a.active, .sidebar-footer a.active { color: var(--brand); background: var(--brand-lighter); font-weight: 700; }
.sidebar-group {
    margin: 16px 0 4px; padding: 0 12px; text-transform: uppercase; letter-spacing: 0.05em;
    font-size: 0.72rem; font-weight: 700; color: var(--muted); opacity: 0.75;
}
.sidebar-footer { border-top: 1px solid var(--line); padding-top: 10px; margin-top: 10px; }
.sidebar-footer .link-button { width: 100%; text-align: left; }

.inline-form { display: inline; }
.link-button { background: none; border: none; cursor: pointer; font: inherit; }

.main-area { flex: 1; min-width: 0; }
.page { max-width: 1180px; margin: 0; padding: 28px 32px 60px; }
.page-header {
    display: flex; align-items: flex-end; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 20px;
}
h1 { font-size: 1.7rem; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { letter-spacing: -0.01em; }
.muted { color: var(--muted); }
.eyebrow { text-transform: uppercase; letter-spacing: 0.06em; font-size: 0.75rem; font-weight: 700; color: var(--brand); margin: 0 0 6px; }

/* Cards */
.card {
    background: var(--card); border: 1px solid var(--line); border-radius: 12px;
    padding: 22px; margin-top: 18px; box-shadow: 0 1px 2px rgba(15, 118, 110, 0.04);
}
.card h2 { margin-top: 0; font-size: 1.15rem; }
.card-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; margin-top: 18px; }

/* Stat tiles */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; margin-top: 18px; }
.stat-tile {
    background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px;
}
.stat-tile .stat-label { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; }
.stat-tile .stat-value { font-size: 1.5rem; font-weight: 700; margin-top: 4px; color: var(--ink); }
.stat-tile.accent { background: var(--brand); border-color: var(--brand); }
.stat-tile.accent .stat-label { color: var(--brand-light); }
.stat-tile.accent .stat-value { color: #fff; }

/* Login / setup — always a centered card, never stretched */
.auth-page {
    min-height: 100vh; min-height: 100dvh;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    padding: 24px 16px calc(24px + var(--safe-bottom));
    background:
        radial-gradient(ellipse at top, #d8f3ef 0%, transparent 55%),
        var(--bg);
}
.auth-card {
    width: 100%;
    max-width: 400px;
    margin: 0 auto;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 32px 28px;
    box-shadow: 0 8px 32px rgba(15, 118, 110, 0.1);
}
.auth-card .auth-logo {
    height: 56px; width: auto; display: block; margin-bottom: 18px;
}
.auth-card h1 { text-align: left; font-size: 1.55rem; }
.auth-card .btn-primary {
    width: 100%; min-height: 52px; font-size: 1.1rem; margin-top: 6px;
}

/* Forms */
form label {
    display: block; margin-bottom: 16px; font-weight: 600; font-size: 0.95rem; color: var(--ink);
}
form label.checkbox { display: flex; align-items: center; gap: 10px; font-weight: 400; min-height: 44px; }
form label.checkbox input { width: 20px; height: 20px; flex-shrink: 0; }
form input[type=text], form input[type=email], form input[type=password], form input[type=number],
form input[type=date], form input[type=file], form select, form textarea {
    display: block; width: 100%; margin-top: 6px; padding: 12px 14px;
    border: 1px solid var(--line); border-radius: 10px; font-size: 17px; font-weight: 400;
    background: #fff; color: var(--ink); font-family: inherit;
    min-height: var(--tap); max-width: 100%;
}
form textarea { resize: vertical; min-height: 88px; }
form input:focus, form select:focus, form textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 4px 20px; }
.form-actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; flex-wrap: wrap; }

/* Buttons */
.btn-primary, .btn-secondary, .btn-danger, .btn-sm {
    display: inline-flex; align-items: center; justify-content: center;
    border: none; border-radius: 10px; padding: 12px 20px;
    font-size: 1rem; font-weight: 600; cursor: pointer; text-decoration: none;
    min-height: var(--tap); line-height: 1.2;
}
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-hover); }
.btn-secondary { background: var(--brand-lighter); color: var(--brand-hover); }
.btn-secondary:hover { background: var(--brand-light); }
.btn-danger { background: #fff; color: var(--error-ink); border: 1px solid #f2c6c3; }
.btn-danger:hover { background: var(--error-bg); }
.btn-sm { padding: 8px 14px; font-size: 0.92rem; min-height: 40px; }

.alert { padding: 14px 16px; border-radius: 10px; margin-bottom: 16px; font-size: 1rem; }
.alert-success { background: var(--success-bg); color: var(--success-ink); }
.alert-error { background: var(--error-bg); color: var(--error-ink); }
.alert-warn { background: var(--warn-bg); color: var(--warn-ink); }

.checklist { list-style: none; padding: 0; margin: 14px 0 0; }
.checklist li { padding: 6px 0 6px 28px; position: relative; color: var(--muted); }
.checklist li::before { content: "○"; position: absolute; left: 0; color: var(--line); }
.checklist li.done { color: var(--ink); }
.checklist li.done::before { content: "●"; color: var(--brand); }

.steps { padding-left: 20px; }
.steps li { margin-bottom: 8px; }
code { background: var(--brand-lighter); color: var(--brand-hover); padding: 2px 6px; border-radius: 4px; font-size: 0.9em; }

/* Tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-top: 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); }
table.data-table { width: 100%; border-collapse: collapse; font-size: 0.98rem; }
table.data-table th {
    text-align: left; padding: 12px 16px; background: var(--brand-lighter); color: var(--brand-hover);
    font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700;
    border-bottom: 1px solid var(--line);
}
table.data-table td { padding: 12px 16px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr:hover td { background: #fafefd; }
table.data-table a { color: var(--brand); text-decoration: none; font-weight: 600; }
table.data-table a:hover { text-decoration: underline; }

/* Badges */
.badge {
    display: inline-block; padding: 4px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 700;
    background: var(--brand-lighter); color: var(--brand-hover); text-transform: capitalize;
}
.badge-green { background: var(--success-bg); color: var(--success-ink); }
.badge-red { background: var(--error-bg); color: var(--error-ink); }
.badge-amber { background: var(--warn-bg); color: var(--warn-ink); }
.badge-gray { background: #eef2f2; color: var(--muted); }

/* Empty state */
.empty-state { text-align: center; padding: 50px 20px; color: var(--muted); }
.empty-state h3 { color: var(--ink); margin-bottom: 6px; }

/* Section toolbar */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 4px; }

/* Documents list */
.doc-list { list-style: none; padding: 0; margin: 10px 0 0; }
.doc-list li {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 12px 14px; border: 1px solid var(--line); border-radius: 8px; margin-bottom: 8px; font-size: 0.95rem;
}
.doc-list .doc-name { font-weight: 600; }
.doc-list .doc-meta { color: var(--muted); font-size: 0.85rem; }

.field-note { font-size: 0.82rem; color: var(--muted); font-weight: 400; display: block; margin-top: 3px; }

.quick-actions { display: flex; flex-wrap: wrap; gap: 10px; }

.stat-grid > a { text-decoration: none; color: inherit; display: block; }

.inline-filter {
    display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
}
.inline-filter input[type=number] { width: 120px; }

@media print {
    .mobile-topbar, .mobile-tabbar, .sidebar, .sidebar-overlay, .no-print { display: none !important; }
    .page { padding: 0 !important; max-width: 100% !important; }
    body { background: #fff !important; }
}

/* Phone / tablet */
@media (max-width: 900px) {
    .mobile-topbar {
        display: flex; align-items: center; gap: 10px;
        position: sticky; top: 0; z-index: 20;
        background: var(--card); border-bottom: 1px solid var(--line);
        padding: 10px 14px;
        padding-top: max(10px, env(safe-area-inset-top, 0px));
        min-height: 56px;
    }
    .mobile-menu-btn {
        display: flex; align-items: center; gap: 8px;
        font-weight: 700; color: var(--ink); cursor: pointer; font-size: 1rem;
        padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px;
        background: var(--bg); min-height: 44px;
    }
    .mobile-topbar-brand {
        font-weight: 700; color: var(--ink); text-decoration: none; font-size: 1.05rem;
        white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    }
    .hamburger, .hamburger::before, .hamburger::after {
        width: 18px; height: 2px; background: var(--ink); display: block; position: relative;
    }
    .hamburger::before, .hamburger::after { content: ""; position: absolute; left: 0; }
    .hamburger::before { top: -6px; }
    .hamburger::after { top: 6px; }

    .app-shell { display: block; }
    .sidebar {
        position: fixed; top: 0; left: 0; width: min(300px, 86vw); height: 100vh; height: 100dvh;
        z-index: 50; box-shadow: 8px 0 32px rgba(0,0,0,0.18);
        transform: translateX(-110%); transition: transform 0.2s ease;
        padding-top: max(20px, env(safe-area-inset-top, 0px));
        padding-bottom: max(20px, var(--safe-bottom));
    }
    .sidebar-toggle-checkbox:checked ~ .app-shell .sidebar { transform: translateX(0); }
    .sidebar-overlay {
        display: block; position: fixed; inset: 0; background: rgba(15, 40, 40, 0.45);
        z-index: 40; opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
    }
    .sidebar-toggle-checkbox:checked ~ .sidebar-overlay {
        opacity: 1; pointer-events: auto;
    }
    .main-area { width: 100%; }

    .mobile-tabbar {
        display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
        background: var(--card); border-top: 1px solid var(--line);
        padding: 6px 4px var(--safe-bottom);
        justify-content: space-around;
        box-shadow: 0 -4px 16px rgba(15, 118, 110, 0.06);
    }
    .mobile-tabbar a, .mobile-tabbar label {
        flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
        gap: 3px; min-height: 56px; color: var(--muted); text-decoration: none;
        font-size: 0.72rem; font-weight: 700; cursor: pointer; border-radius: 10px;
        padding: 6px 2px;
    }
    .mobile-tabbar a.active { color: var(--brand); background: var(--brand-lighter); }
    .mobile-tabbar svg { width: 24px; height: 24px; }

    .page { padding: 16px 16px calc(96px + var(--safe-bottom)); }
}

@media (max-width: 700px) {
    .auth-card { padding: 26px 20px; }
    .page-header { flex-direction: column; align-items: stretch; }
    .page-header > a.btn-primary,
    .page-header > a.btn-secondary,
    .page-header .form-actions,
    .page-header .quick-actions { width: 100%; }
    .page-header .form-actions a,
    .page-header .form-actions button,
    .page-header .quick-actions a,
    .page-header > a.btn-primary,
    .page-header > a.btn-secondary {
        width: 100%;
    }
    .quick-actions { display: grid; grid-template-columns: 1fr 1fr; }
    h1 { font-size: 1.45rem; }
    .card { padding: 16px; }
    .stat-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
    .stat-tile { padding: 14px; }
    .stat-tile .stat-value { font-size: 1.25rem; }
    .form-grid { grid-template-columns: 1fr; }
    .form-actions { flex-direction: column; align-items: stretch; }
    .form-actions .btn-primary,
    .form-actions .btn-secondary,
    .form-actions .btn-danger,
    .form-actions a,
    .form-actions button { width: 100%; }
    .form-actions form[style*="margin-left:auto"] { margin-left: 0 !important; width: 100%; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .toolbar .btn-sm, .toolbar a { width: 100%; text-align: center; }
    .doc-list li { flex-direction: column; align-items: stretch; }
    .doc-list .form-actions { flex-direction: row; }

    /* Stack tables into readable cards instead of tiny sideways scrolling */
    .table-wrap {
        overflow: visible;
        border: none;
        background: transparent;
        border-radius: 0;
        margin-top: 12px;
    }
    table.data-table thead { display: none; }
    table.data-table, table.data-table tbody { display: block; width: 100%; }
    table.data-table tr {
        display: block;
        background: var(--card);
        border: 1px solid var(--line);
        border-radius: 12px;
        padding: 6px 14px 10px;
        margin-bottom: 12px;
        box-shadow: 0 1px 2px rgba(15, 118, 110, 0.04);
    }
    table.data-table tr:hover td { background: transparent; }
    table.data-table td {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 14px;
        padding: 10px 0;
        border-bottom: 1px solid var(--line);
        text-align: right;
        font-size: 1rem;
    }
    table.data-table td:last-child { border-bottom: none; }
    table.data-table td::before {
        content: attr(data-label);
        font-weight: 700;
        color: var(--muted);
        text-align: left;
        flex: 0 0 42%;
        font-size: 0.88rem;
    }
    table.data-table td:not([data-label])::before,
    table.data-table td[data-label=""]::before { content: none; }
    table.data-table td:not([data-label]),
    table.data-table td[data-label=""] {
        justify-content: flex-end;
        padding-top: 12px;
    }
    table.data-table td input,
    table.data-table td select {
        width: auto;
        min-width: 0;
        flex: 1;
        min-height: 44px;
    }
}

@media (max-width: 420px) {
    .stat-grid { grid-template-columns: 1fr; }
    .quick-actions { grid-template-columns: 1fr; }
}
