:root {
    /* Brand colors — SafeStep Uganda's own palette, not a generic default. */
    --bg: #F5EFED;
    --text: #0F0A0A;
    --accent: #04724D;
    --accent-dark: color-mix(in srgb, #04724D 82%, black);
    --accent-tint: color-mix(in srgb, #04724D 12%, white);
    --error: #D64933;
    --error-dark: color-mix(in srgb, #D64933 82%, black);
    --highlight: #BDBF09;
    --muted: color-mix(in srgb, #0F0A0A 58%, #F5EFED);
    --border: color-mix(in srgb, #0F0A0A 16%, #F5EFED);
    --surface: #ffffff;
    --focus-ring: #04724D;

    /* 8pt spacing grid */
    --space-1: 8px;
    --space-2: 16px;
    --space-3: 24px;
    --space-4: 32px;
    --space-5: 40px;
    --space-6: 48px;

    /* Type scale: 12/14/16/20/24/32 */
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.25rem;
    --text-xl: 1.5rem;
    --text-2xl: 2rem;

    --radius: 8px;
    --touch: 44px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    /* System font stack, not a Google Fonts import — SafeStep's users often have
       limited/costly data connectivity (see architecture.md); Roboto is what most
       Android devices already render this stack as, at zero extra network cost. */
    font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: var(--text-base);
    line-height: 1.55;
    -webkit-text-size-adjust: 100%;
}

a { color: var(--accent-dark); text-decoration: underline; }
a:hover { color: var(--accent); }

:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
    border-radius: 4px;
}

.site-header {
    max-width: 640px;
    margin: 0 auto;
    padding: var(--space-2) var(--space-2) 0;
}

.brand { display: inline-block; }
.brand img { height: 56px; width: auto; display: block; }

.container {
    max-width: 640px;
    margin: 0 auto;
    padding: var(--space-2) var(--space-2) var(--space-6);
}

.container.wide { max-width: 960px; }

.admin-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: var(--space-2);
    margin: 0 0 var(--space-3);
    padding: var(--space-2);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.admin-nav { display: flex; align-items: center; gap: var(--space-1); flex-wrap: wrap; }

.admin-nav a {
    color: var(--text);
    text-decoration: none;
    font-weight: 600;
    padding: var(--space-1) var(--space-2);
    min-height: var(--touch);
    display: inline-flex;
    align-items: center;
    border-radius: 6px;
}
.admin-nav a:hover { background: var(--accent-tint); }
.admin-nav a.active { background: var(--accent); color: #fff; }

.admin-user {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
}
.admin-user-name { color: var(--muted); font-size: var(--text-sm); }
.admin-user .button { margin-top: 0; }

.role-badge {
    display: inline-block;
    padding: 2px var(--space-2);
    border-radius: 999px;
    font-size: var(--text-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.role-badge.role-coordinator { background: var(--accent-tint); color: var(--accent-dark); }
.role-badge.role-partner { background: color-mix(in srgb, var(--highlight) 25%, white); color: #4a4900; }
.role-badge.role-developer { background: var(--border); color: var(--text); }

.admin-footer {
    margin-top: var(--space-6);
    padding-top: var(--space-3);
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: var(--text-sm);
}
.admin-footer p { margin: 0 0 var(--space-1); }

/* --- Filter chips (status filters) --- */
.filters {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
    margin: 0 0 var(--space-3);
    padding: 0;
    list-style: none;
}
.filters a {
    display: inline-flex;
    align-items: center;
    min-height: var(--touch);
    padding: var(--space-1) var(--space-2);
    border-radius: 999px;
    border: 1px solid var(--border);
    color: var(--text);
    text-decoration: none;
    font-size: var(--text-sm);
    font-weight: 600;
    background: var(--surface);
}
.filters a:hover { border-color: var(--accent); }
.filters a.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* --- Tables (desktop) / cards (mobile) --- */
.table-scroll { overflow-x: auto; margin: var(--space-2) 0; }
table { width: 100%; border-collapse: collapse; margin: 0; }
table .button { margin-top: 0; }
table form { margin: 0; display: inline-block; }

.row-actions {
    display: flex;
    gap: var(--space-1);
    flex-wrap: wrap;
    justify-content: flex-end;
}
th, td { text-align: left; padding: var(--space-1) var(--space-1); border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); }
tr.urgent td:first-child { border-left: 3px solid var(--error); }
tr.urgent { background: color-mix(in srgb, var(--error) 7%, white); }
td a { font-weight: 600; }

.badge {
    display: inline-block;
    padding: 2px var(--space-2);
    border-radius: 999px;
    font-size: var(--text-xs);
    font-weight: 700;
}
.badge.received { background: color-mix(in srgb, var(--highlight) 25%, white); color: #4a4900; }
.badge.assigned { background: color-mix(in srgb, var(--accent) 18%, white); color: var(--accent-dark); }
.badge.in_progress { background: color-mix(in srgb, var(--accent) 30%, white); color: var(--accent-dark); }
.badge.closed { background: var(--border); color: var(--text); }
.badge.proposed { background: color-mix(in srgb, var(--highlight) 25%, white); color: #4a4900; }
.badge.confirmed { background: color-mix(in srgb, var(--accent) 18%, white); color: var(--accent-dark); }
.badge.completed { background: color-mix(in srgb, var(--accent) 30%, white); color: var(--accent-dark); }
.badge.declined, .badge.no_show, .badge.cancelled { background: color-mix(in srgb, var(--error) 15%, white); color: var(--error-dark); }
.badge.urgent { background: var(--error); color: #fff; }
.badge.normal { background: var(--border); color: var(--muted); }
.badge.reschedule_requested { background: var(--border); color: var(--text); }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: var(--space-2) var(--space-3);
    margin: var(--space-2) 0;
}

h1 { font-size: var(--text-2xl); line-height: 1.25; margin: 0 0 var(--space-1); }
h2 { font-size: var(--text-lg); margin: var(--space-4) 0 var(--space-1); }

p { margin: var(--space-1) 0 var(--space-2); }
p.muted { color: var(--muted); font-size: var(--text-sm); }
p.error {
    color: var(--error-dark);
    font-weight: 600;
    background: color-mix(in srgb, var(--error) 10%, white);
    border: 1px solid color-mix(in srgb, var(--error) 35%, white);
    border-radius: var(--radius);
    padding: var(--space-2);
}

fieldset {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin: var(--space-3) 0;
    padding: var(--space-2);
}

/* Anti-spam honeypot — invisible to a real visitor, present in the DOM for bots. */
.hp-field {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    left: -9999px;
}

legend { font-weight: 700; padding: 0 6px; }

label {
    display: block;
    margin: var(--space-2) 0 var(--space-1);
    font-weight: 600;
}

/* Checkbox/radio rows get a bigger tap target via padding on the whole label,
   not by resizing the native control (which looks broken on most browsers). */
fieldset label {
    display: flex;
    align-items: center;
    min-height: var(--touch);
    padding: var(--space-1) var(--space-1);
    margin: 0;
    font-weight: 400;
    border-radius: 6px;
}
fieldset label:hover { background: color-mix(in srgb, var(--accent) 6%, transparent); }

input[type="text"], input[type="email"], input[type="password"], select, textarea {
    width: 100%;
    min-height: var(--touch);
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: var(--text-base);
    font-family: inherit;
    background: var(--surface);
    color: var(--text);
}

input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 3px var(--accent-tint);
}

textarea { min-height: calc(var(--touch) * 2); }

input[type="checkbox"], input[type="radio"] {
    width: 20px;
    height: 20px;
    min-height: 0;
    margin-right: var(--space-2);
    accent-color: var(--accent);
    flex-shrink: 0;
}

button, .button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: var(--touch);
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-base);
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
    margin-top: var(--space-2);
    transition: background-color 0.15s ease;
}

button:hover, .button:hover { background: var(--accent-dark); }
button:active, .button:active { background: color-mix(in srgb, var(--accent) 70%, black); }
button:disabled, .button:disabled { background: var(--border); color: var(--muted); cursor: not-allowed; }

.button.secondary {
    background: transparent;
    color: var(--accent-dark);
    border: 1px solid var(--accent);
}
.button.secondary:hover { background: var(--accent-tint); }

.actions { display: flex; flex-direction: column; gap: var(--space-2); max-width: 320px; }
.actions .button { width: 100%; }

.access-code {
    font-size: var(--text-2xl);
    letter-spacing: 0.08em;
    font-weight: 700;
    background: color-mix(in srgb, var(--highlight) 12%, white);
    border: 2px dashed var(--accent);
    border-radius: var(--radius);
    padding: var(--space-3);
    text-align: center;
    margin: var(--space-2) 0;
    word-break: break-word;
}

.updates { padding-left: var(--space-3); margin: var(--space-1) 0; }
.updates li { margin-bottom: var(--space-1); }

/* --- Chat thread --- */
.chat-thread {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin: var(--space-2) 0;
}
.chat-message {
    max-width: 85%;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
}
.chat-message.mine {
    align-self: flex-end;
    background: var(--accent-tint);
    border-color: var(--accent);
}
.chat-message.theirs { align-self: flex-start; }
.chat-meta {
    font-size: var(--text-xs);
    color: var(--muted);
    margin-bottom: 2px;
}
.chat-body { white-space: pre-line; }

#quick-exit {
    position: fixed;
    top: var(--space-1);
    right: var(--space-1);
    z-index: 1000;
    min-height: var(--touch);
    background: var(--error);
    color: #fff;
    border: none;
    border-radius: 6px;
    padding: var(--space-1) var(--space-2);
    font-weight: 700;
    cursor: pointer;
    margin: 0;
}

#quick-exit:hover { background: var(--error-dark); }

/* --- Mobile: tables become stacked cards below 640px --- */
@media (max-width: 640px) {
    h1 { font-size: var(--text-xl); }

    .table-scroll table thead { display: none; }
    .table-scroll table, .table-scroll table tbody, .table-scroll table tr, .table-scroll table td {
        display: block;
        width: 100%;
    }
    .table-scroll table {
        border: none;
    }
    .table-scroll table tr {
        border: 1px solid var(--border);
        border-radius: var(--radius);
        background: var(--surface);
        margin-bottom: var(--space-2);
        padding: var(--space-2);
    }
    .table-scroll table tr.urgent {
        border-left: 4px solid var(--error);
    }
    .table-scroll table td {
        border-bottom: none;
        padding: 6px 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: var(--space-2);
        text-align: right;
    }
    .table-scroll table td::before {
        content: attr(data-label);
        font-size: var(--text-xs);
        text-transform: uppercase;
        letter-spacing: 0.03em;
        color: var(--muted);
        font-weight: 700;
        text-align: left;
    }
    .table-scroll table td:empty,
    .table-scroll table td:has(> a:only-child),
    .table-scroll table td:has(.row-actions) {
        justify-content: flex-end;
    }
    .table-scroll table td a.button {
        width: 100%;
        margin-top: 0;
    }
    .table-scroll table td .row-actions {
        flex-direction: column;
        width: 100%;
    }
    .table-scroll table td .row-actions .button {
        width: 100%;
    }
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; }
}
