@import url('https://fonts.googleapis.com/css2?family=Audiowide&display=swap');

* { box-sizing: border-box; }

body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    margin: 0;
    background: #f6f7f9;
    color: #1f2328;
}

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 2rem;
    background: #fff;
    border-bottom: 1px solid #e3e6ea;
}

header h1 { font-size: 1.1rem; margin: 0; display: flex; align-items: baseline; gap: 0.75rem; }
header h1 .brand {
    font-family: 'Audiowide', system-ui, sans-serif;
    font-size: 1.5rem;
    font-style: italic;
    letter-spacing: 0.04em;
    color: #c8102e;
}
header h1 .subtitle {
    font-weight: 400;
    color: #666;
    font-size: 0.95rem;
}

main { padding: 2rem; max-width: 960px; margin: 0 auto; }
main.wide { max-width: min(1850px, 97vw); }

main.login {
    max-width: 360px;
    margin: 4rem auto;
    background: #fff;
    padding: 2rem;
    border: 1px solid #e3e6ea;
    border-radius: 8px;
}
main.login > h1 { font-size: 1.1rem; margin: 0 0 1rem; }

label {
    display: block;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

input {
    display: block;
    width: 100%;
    margin-top: 0.25rem;
    padding: 0.5rem 0.6rem;
    border: 1px solid #cdd2d8;
    border-radius: 4px;
    font: inherit;
}

button {
    padding: 0.55rem 1rem;
    background: #1f6feb;
    color: #fff;
    border: 0;
    border-radius: 4px;
    cursor: pointer;
    font: inherit;
}

button:hover { background: #1858c4; }

pre {
    background: #fff;
    border: 1px solid #e3e6ea;
    padding: 1rem;
    border-radius: 4px;
    overflow: auto;
}

.error { color: #b3261e; margin-top: 0.5rem; }

.message { margin: 0.75rem 0; padding: 0.75rem; border-radius: 4px; font-size: 0.9rem; }
.message:empty { display: none; }
.message.success { background: #e6f4ea; color: #1e4620; border: 1px solid #b7dcc2; }
.message.error   { background: #fdecea; color: #5f2120; border: 1px solid #f5c2c0; }

.info-box    { background: #fff3cd; color: #856404; border: 1px solid #ffc107; border-radius: 4px; padding: 0.75rem; margin-bottom: 1rem; }
.pending-box { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; border-radius: 4px; padding: 0.75rem; margin-bottom: 1rem; }
.muted       { color: #666; font-size: 0.85rem; }
.auth-links  { margin-top: 1rem; font-size: 0.9rem; }
.auth-links a { color: #1f6feb; text-decoration: none; }
.auth-links a:hover { text-decoration: underline; }

.spinner {
    display: inline-block; width: 16px; height: 16px;
    border: 2px solid #ccc; border-top-color: #1f6feb; border-radius: 50%;
    animation: spin 0.8s linear infinite; vertical-align: middle; margin-right: 0.5rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

.phase { display: none; }
.phase.active { display: block; }

nav { display: flex; align-items: center; gap: 1rem; }
nav a { color: #1f6feb; text-decoration: none; }
nav a:hover { text-decoration: underline; }

table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border: 1px solid #e3e6ea;
    border-radius: 4px;
}

th, td {
    text-align: left;
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #eef0f3;
    vertical-align: top;
    font-size: 0.9rem;
}

th { background: #f6f7f9; font-weight: 600; }

#messages td:nth-child(4) { max-width: 32rem; word-break: break-word; white-space: pre-wrap; }

.thumb {
    max-width: 160px;
    max-height: 160px;
    border-radius: 4px;
    object-fit: cover;
}

.section { margin-bottom: 2.5rem; }
.section h2 { font-size: 1rem; margin: 0 0 0.75rem; }

.admin-sub { font-size: 0.95rem; margin: 1.25rem 0 0.5rem; }
.admin-form {
    display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: end;
    margin-bottom: 0.75rem; font-size: 0.9rem;
}
.admin-form label { display: flex; flex-direction: column; gap: 0.2rem; }
.admin-form input, .admin-form select { padding: 0.3rem 0.5rem; font-size: 0.9rem; }
.reset-pwd { padding: 0.25rem 0.4rem; font-size: 0.85rem; width: 12rem; }
.sec-check { display: inline-flex; align-items: center; gap: 0.2rem; margin-right: 0.6rem; font-size: 0.85rem; }
.sec-check input { width: auto; display: inline-block; margin: 0; }
.sec-status { margin-left: 0.4rem; font-size: 0.8rem; }

.activity-wrap { overflow-x: auto; }
#activity-table { font-size: 0.85rem; min-width: 800px; }
#activity-table .act-ua {
    max-width: 22rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.act-login         { color: #1e4620; }
.act-login_failed  { color: #b3261e; font-weight: 600; }
.act-logout        { color: #444; }

.schedule-controls {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
    font-size: 0.9rem;
}
.schedule-controls input { width: auto; display: inline-block; margin: 0; }
.schedule-controls #schedule-year { width: 6em; }
.schedule-controls #schedule-week { width: 5em; }

.schedule-wrap { overflow-x: auto; }

/* Fixed layout so column widths come from the colgroup below, not from cell
   content. Without this, weeks with more/longer entries widen columns and the
   whole grid shifts horizontally, making week-to-week comparison confusing. */
#schedule { font-size: 0.85rem; min-width: 104rem; table-layout: fixed; }
#schedule col.col-route    { width: 3.5rem; }
#schedule col.col-hospital { width: 7rem; }
#schedule col.col-interval { width: 9.5rem; }
#schedule col.col-day      { width: 12rem; }
#schedule .no-slot { color: #999; }
#schedule th { text-align: center; }
#schedule .date-sub { font-weight: 400; color: #666; font-size: 0.78rem; }
#schedule .holiday-tag { font-weight: 400; color: #8a5a00; font-size: 0.72rem; }
#schedule th.holiday { background: #fff8e1; }

/* Visually group rows by Rit: the Route cell spans 3 rows (centered, bold,
   light tint), the start of each group gets a thicker dark top border, and
   Rit 2 sits on a slightly tinted background so the three groups read as
   three distinct bands at a glance. Rit 1 and Rit 3 keep the default white
   background; only the middle band shifts. */
#schedule td.route {
    text-align: center;
    vertical-align: middle;
    font-weight: 600;
    background: #f6f7f9;
    border-right: 1px solid #e3e6ea;
}
#schedule tbody tr.rit-start:not(:first-child) > td {
    border-top: 3px solid #6b7077;
}
#schedule tbody tr.rit-2 > td { background: #eef2f7; }
#schedule tbody tr.rit-2 > td.route { background: #e2e7ee; }
#schedule th, #schedule td { white-space: nowrap; }
#schedule td.day { white-space: normal; }
/* Merged interval cell: weekday window on the first line, and for Rit 2 the
   later Sat-Sun window on a second, muted line (label + time). */
#schedule td.interval .tf-line { display: block; }
#schedule td.interval .tf-weekend { color: #555; font-size: 0.8rem; }
#schedule td.interval .tf-label { color: #8a8f96; }
#schedule .slot-entry { display: block; white-space: nowrap; }
#schedule .slot-entry a { color: #1f6feb; text-decoration: none; }
#schedule .slot-entry a:hover { text-decoration: underline; }
/* Color rules target both the entry text (manual entries — no <a>) and the
   link inside (auto pins — wrapped in <a href> to the Maps URL). Pin links
   default to blue via `.slot-entry a` above, but manual entries have no link
   so the explicit `.early` rule ensures both kinds get the same blue when a
   pin/manual time lands before the slot window. */
#schedule .slot-entry.late,
#schedule .slot-entry.late a       { color: #b3261e; font-weight: 600; }
#schedule .slot-entry.in-window,
#schedule .slot-entry.in-window a  { color: #1e4620; }
#schedule .slot-entry.early,
#schedule .slot-entry.early a      { color: #1f6feb; }
#schedule .slot-entry.manual { font-style: italic; }
#schedule .slot-entry.manual::before { content: "✎ "; opacity: 0.6; }

/* POD/POP entries (orders-source view). The kind prefix distinguishes the
   two visually so a Maxima cell's POP and a Catharina cell's POD don't
   look identical when scanning across the row. Recipient is a smaller
   second line under the time. */
#schedule .slot-entry.order { white-space: nowrap; }
#schedule .slot-entry.pop::before { content: "📦 "; opacity: 0.7; }
#schedule .slot-entry.pod::before { content: "✓ ";  opacity: 0.7; }
#schedule .slot-entry .recipient {
    display: block; font-size: 0.78rem; color: #555; margin-top: 0.1rem;
    white-space: normal;
}

/* Combined view marks which source led each cell: 📍 for the offsetted Google
   Maps pin, and the POD keeps its ✓/📦 prefix (via the `order` class) for the
   cells where it overrode an out-of-window pin. */
#schedule .slot-entry.source-pin::before { content: "📍 "; opacity: 0.65; }
/* Admin proof datapoint (combined view): a camera marks the alternative on-site
   evidence (e.g. a WhatsApp photo) that the admin entered. */
#schedule .slot-entry.source-proof::before { content: "📷 "; opacity: 0.7; }

/* Driver (roster-source) view: a driver code rather than a time. Rendered as a
   compact monospace chip so codes line up and read as identifiers, not times.
   The same code repeats down a Rit's three hospital rows — one driver, three
   stops. */
#schedule .slot-entry.driver {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.82rem;
    color: #1f2933;
}
#schedule .slot-entry.driver::before { content: "🧑 "; opacity: 0.6; }

/* Admin-only inline controls. The buttons are hidden by default; the script
   reveals them by toggling the .can-edit class on the schedule table once the
   /api/me response confirms an admin role. */
#schedule .cell-btn {
    display: none;
    background: none;
    border: 1px solid transparent;
    color: #888;
    font: inherit;
    cursor: pointer;
    padding: 0 0.25rem;
    margin-left: 0.25rem;
    border-radius: 3px;
    font-size: 0.78rem;
    line-height: 1.1;
}
#schedule .cell-btn:hover { border-color: #cdd2d8; color: #333; }
#schedule.can-edit .cell-btn { display: inline-block; }

/* Invalidated-orders panel: admin-reversible list shown above the order grid. */
#invalidated-panel {
    margin: 0.5rem 0;
    padding: 0.5rem 0.75rem;
    border: 1px solid #e3c7c7;
    background: #fbf2f2;
    border-radius: 5px;
    font-size: 0.85rem;
}
#invalidated-panel .inv-list { list-style: none; margin: 0.35rem 0 0; padding: 0; }
#invalidated-panel .inv-list li { display: flex; align-items: center; gap: 0.5rem; padding: 0.1rem 0; }
#invalidated-panel .inv-order { font-weight: 600; }
#invalidated-panel button[data-action="restore-order"] {
    font: inherit;
    font-size: 0.78rem;
    cursor: pointer;
    border: 1px solid #cdd2d8;
    background: #fff;
    color: #333;
    border-radius: 3px;
    padding: 0 0.4rem;
    margin-left: auto;
}
#invalidated-panel button[data-action="restore-order"]:hover { border-color: #888; }
#schedule .cell-add {
    display: none;
    color: #888;
    font: inherit;
    font-size: 0.78rem;
    cursor: pointer;
    background: none;
    border: 1px dashed #d0d4d9;
    border-radius: 3px;
    padding: 0.1rem 0.4rem;
}
#schedule.can-edit .cell-add { display: inline-block; }
#schedule .cell-add:hover { color: #1f6feb; border-color: #1f6feb; }
#schedule .cell-edit-form { display: inline-flex; gap: 0.25rem; align-items: center; }
#schedule .cell-edit-form input[type="time"] {
    width: 6em; padding: 0.05rem 0.2rem; font-size: 0.8rem; margin: 0;
}
/* Inline POD/POP override form (orders view): time-only, no car/kind picker. */
#schedule .order-edit-form { display: inline-flex; gap: 0.25rem; align-items: center; }
#schedule .order-edit-form input[type="time"] {
    width: 6em; padding: 0.05rem 0.2rem; font-size: 0.8rem; margin: 0;
}
/* Inline proof-entry form (combined view, opened from the right-click menu). */
#schedule .proof-edit-form { display: inline-flex; gap: 0.25rem; align-items: center; flex-wrap: wrap; margin-top: 0.2rem; }
#schedule .proof-edit-form input[type="time"] { width: 6em; padding: 0.05rem 0.2rem; font-size: 0.8rem; margin: 0; }
#schedule .proof-edit-form input[type="text"] { width: 7em; padding: 0.05rem 0.2rem; font-size: 0.8rem; margin: 0; }
#schedule .car-select {
    font-size: 0.78rem; color: #555; display: inline-flex; gap: 0.2rem; align-items: center;
}
#schedule .car-select input[type="checkbox"] { margin: 0; }

/* Car badge: visible inline next to a pin/manual entry when it's not the
   main car. car-2 stands out in orange to be scannable across the grid. */
#schedule .car-badge {
    display: inline-block;
    margin-left: 0.3rem;
    padding: 0 0.35rem;
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1.3;
    border-radius: 9px;
    background: #fff;
    border: 1px solid currentColor;
}
#schedule .car-badge.car-2 { color: #c2410c; background: #fff7ed; }

/* Right-click context menu for assigning car numbers. Positioned absolutely
   by JS at the click coordinates. Hidden by default; the contextmenu handler
   sets display:block. Width is intrinsic; clipping is handled in JS. */
#car-menu {
    position: fixed;
    z-index: 1000;
    background: #fff;
    border: 1px solid #cdd2d8;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    padding: 0.2rem;
    min-width: 7rem;
}
#car-menu button {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    padding: 0.35rem 0.7rem;
    font: inherit;
    font-size: 0.85rem;
    cursor: pointer;
    border-radius: 3px;
    color: #222;
}
#car-menu button:hover { background: #f1f3f5; }
#car-menu button.active { background: #e7f0ff; color: #1f6feb; font-weight: 600; }

.chart-legend {
    display: flex;
    gap: 1.25rem;
    margin-bottom: 0.5rem;
    font-size: 0.85rem;
    color: #444;
}
.legend-item { display: inline-flex; align-items: center; gap: 0.4rem; }
.legend-item::before {
    content: ""; width: 12px; height: 12px; border-radius: 2px;
}
.legend-early::before    { background: #1f6feb; }
.legend-ontime::before   { background: #1e4620; }
.legend-late::before     { background: #b3261e; }
.legend-combined::before { background: #0f766e; }

/* The combined "on time or earlier" legend item only shows when the chart is
   in combined mode; the two source items it replaces hide in turn. */
.legend-combined { display: none; }
#weekly-stats-chart.combined .legend-early,
#weekly-stats-chart.combined .legend-ontime { display: none; }
#weekly-stats-chart.combined .legend-combined { display: inline-flex; }

.chart-mode-toggle {
    display: inline-flex; align-items: center; gap: 0.4rem;
    margin-bottom: 0.75rem; font-size: 0.85rem; color: #444;
}
.chart-mode-toggle input { width: auto; display: inline-block; margin: 0; }

#weekly-stats-chart svg {
    width: 100%; height: auto;
    background: #fff;
    border: 1px solid #e3e6ea;
    border-radius: 4px;
    padding: 0.5rem;
}
#weekly-stats-svg .grid    { stroke: #eef0f3; stroke-width: 1; }
#weekly-stats-svg .axis    { stroke: #cdd2d8; stroke-width: 1; }
#weekly-stats-svg .y-label { font-size: 11px; fill: #666; text-anchor: end; }
#weekly-stats-svg .x-label { font-size: 11px; fill: #666; text-anchor: start; }
#weekly-stats-svg polyline { fill: none; stroke-width: 2; }
#weekly-stats-svg .series-early    { stroke: #1f6feb; }
#weekly-stats-svg .series-ontime   { stroke: #1e4620; }
#weekly-stats-svg .series-late     { stroke: #b3261e; }
#weekly-stats-svg .series-combined { stroke: #0f766e; }
#weekly-stats-svg .dot-early    { fill: #1f6feb; }
#weekly-stats-svg .dot-ontime   { fill: #1e4620; }
#weekly-stats-svg .dot-late     { fill: #b3261e; }
#weekly-stats-svg .dot-combined { fill: #0f766e; }

/* Backups section on App.html. One card per /backups subfolder, with an
   expandable log tail underneath. */
.small-btn {
    padding: 0.2rem 0.55rem; font-size: 0.78rem;
    background: #eef0f3; color: #1f2328;
    border: 1px solid #cdd2d8; border-radius: 4px;
    margin-left: 0.5rem; vertical-align: middle;
}
.small-btn:hover { background: #d9e6f7; }
.small-btn:disabled { opacity: 0.6; cursor: default; }

#backups-section h2 { display: flex; align-items: center; }
.backup-card {
    background: #fff;
    border: 1px solid #e3e6ea;
    border-radius: 4px;
    padding: 0.75rem 1rem;
    margin-bottom: 0.75rem;
}
.backup-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; }
.backup-meta {
    display: grid;
    grid-template-columns: max-content 1fr;
    column-gap: 1rem; row-gap: 0.25rem;
    margin: 0.5rem 0 0;
    font-size: 0.88rem;
}
.backup-meta dt { color: #666; }
.backup-meta dd { margin: 0; }
.backup-log {
    margin-top: 0.6rem; max-height: 24rem; overflow: auto;
    font-size: 0.78rem; line-height: 1.35;
    background: #1f2328; color: #e6edf3;
    border-radius: 4px; padding: 0.75rem 1rem;
}

/* Tracker management. Wide table — let it scroll horizontally on narrow
   viewports rather than wrap, which keeps the admin grid scannable. */
#trackers-section h2 { display: flex; align-items: center; }
.trackers-wrap { overflow-x: auto; }
#trackers-table { font-size: 0.85rem; min-width: 1000px; }
#trackers-table td { vertical-align: top; }
#trackers-table .dot {
    display: inline-block; width: 8px; height: 8px; border-radius: 50%;
    margin-right: 0.35rem; vertical-align: middle;
}
#trackers-table .dot.online  { background: #2ea043; }
#trackers-table .dot.offline { background: #b3261e; }
#trackers-table .trk-ip,
#trackers-table .trk-net {
    width: 9rem; padding: 0.2rem 0.35rem; font-size: 0.82rem; margin: 0;
}
#trackers-table .trk-comment {
    width: 100%; min-width: 12rem; padding: 0.25rem 0.35rem;
    font: inherit; font-size: 0.82rem;
    border: 1px solid #cdd2d8; border-radius: 4px; resize: vertical;
}
#trackers-table .trk-status,
#trackers-table .trk-acl-status { margin-left: 0.4rem; font-size: 0.78rem; }

.trk-access > summary {
    cursor: pointer; color: #1f6feb; font-size: 0.82rem; user-select: none;
}
.trk-access-list {
    margin-top: 0.4rem; padding: 0.4rem 0.5rem;
    background: #fafbfc; border: 1px solid #e3e6ea; border-radius: 4px;
    max-height: 14rem; overflow-y: auto;
    display: flex; flex-direction: column; gap: 0.2rem;
}
.trk-access-row {
    display: flex; align-items: center; gap: 0.35rem;
    font-size: 0.82rem; margin: 0;
}
.trk-access-row input { width: auto; margin: 0; }
.trk-access-list .small-btn { margin-left: 0; align-self: flex-start; margin-top: 0.3rem; }

/* --- WhatsApp reader control --- */
#whatsapp-section h2 { display: flex; align-items: center; gap: 0.5rem; }
.wa-badge {
    font-size: 0.72rem; font-weight: 600; text-transform: uppercase;
    letter-spacing: 0.02em; padding: 0.1rem 0.5rem; border-radius: 999px;
    border: 1px solid transparent;
}
.wa-badge-open       { background: #e6f4ea; color: #1e4620; border-color: #b7dfc1; }
.wa-badge-qr         { background: #fff4e5; color: #7a4b00; border-color: #f5d6a8; }
.wa-badge-connecting { background: #eef2f7; color: #3b526b; border-color: #cdd9e6; }
.wa-badge-close      { background: #fdecea; color: #5f2120; border-color: #f5c2c0; }
.wa-badge-unknown    { background: #f0f0f0; color: #555; border-color: #ddd; }

.wa-meta {
    display: grid; grid-template-columns: max-content 1fr; gap: 0.25rem 1rem;
    margin: 0 0 1rem; font-size: 0.88rem;
}
.wa-meta dt { color: #666; }
.wa-meta dd { margin: 0; }

#wa-qr-canvas {
    border: 1px solid #e3e6ea; border-radius: 6px; padding: 6px; background: #fff;
}

.wa-chats {
    margin: 0.25rem 0 0.75rem; padding: 0.4rem 0.5rem;
    background: #fafbfc; border: 1px solid #e3e6ea; border-radius: 4px;
    max-height: 18rem; overflow-y: auto;
    display: flex; flex-direction: column; gap: 0.2rem;
}
.wa-chat-row {
    display: flex; align-items: center; gap: 0.4rem; font-size: 0.85rem; margin: 0;
}
.wa-chat-row input { width: auto; margin: 0; }
.wa-save-row { display: flex; align-items: center; gap: 0.5rem; }
