:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --text: #1d2330;
    --muted: #697386;
    --border: #dfe3e8;
    --primary: #1f5fbf;
    --primary-dark: #174a96;
    --danger: #b42318;
    --success-bg: #e8f5ec;
    --success-text: #1d6b39;
    --error-bg: #fdecea;
    --error-text: #9b1c13;
    --radius: 8px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 var(--font); color: var(--text); background: var(--bg); }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
small { font-weight: normal; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 0.9em; }
.prewrap { white-space: pre-wrap; margin: 0; }
.pad { padding: 0 20px 16px; }

/* Top bar */
.topbar { display: flex; align-items: center; gap: 24px; padding: 0 24px; height: 56px; background: #16202f; color: #fff; }
.topbar .brand { color: #fff; font-weight: 700; }
.mainnav { display: flex; gap: 4px; flex: 1; }
.mainnav a, .mainnav .soon { color: #c7cfdb; padding: 6px 12px; border-radius: 6px; }
.mainnav a:hover { background: #24324a; text-decoration: none; color: #fff; }
.mainnav a.active { background: #2b3d5a; color: #fff; }
.mainnav .soon { opacity: 0.45; cursor: default; }
.userbox { display: flex; align-items: center; gap: 12px; font-size: 0.9rem; }
.userbox small { color: #9aa6b8; margin-left: 4px; }
.userbox form { margin: 0; }
.btn-link { background: none; border: 0; color: #c7cfdb; cursor: pointer; font: inherit; padding: 0; }
.btn-link:hover { color: #fff; text-decoration: underline; }

.container { max-width: 1200px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.crumbs { margin: 0 0 4px; font-size: 0.85rem; color: var(--muted); }
.actions { display: flex; gap: 8px; }
.actions form { margin: 0; }

/* Cards & layout */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.card-flush { padding: 0; overflow-x: auto; }
.card-head { padding: 16px 20px 0; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; }
.grid-2 .card { margin-bottom: 0; }
.grid-2 + * { margin-top: 20px; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 20px; display: flex; flex-direction: column; }
.stat-num { font-size: 1.8rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.85rem; }

.details { display: grid; grid-template-columns: 140px 1fr; gap: 8px 16px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }

/* Tables */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 20px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 600; background: #fafbfc; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: #f8fafc; }
.num { text-align: right !important; }

/* Badges */
.badge { display: inline-block; font-size: 0.75rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #eef1f5; color: #46516a; }
.badge-active { background: #e3f4e8; color: #1d6b39; }
.badge-prospect { background: #e7effc; color: #1f5fbf; }
.badge-inactive { background: #f1f1f1; color: #777; }

/* Buttons */
.btn { display: inline-block; padding: 8px 16px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; cursor: pointer; }
.btn:hover { background: #f1f3f6; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { color: var(--danger); border-color: #f0c4c0; }
.btn-danger:hover { background: var(--error-bg); }
.btn-block { width: 100%; }
.btn:disabled { opacity: 0.6; cursor: wait; }

/* Alerts */
.alert { padding: 12px 16px; border-radius: 6px; margin-bottom: 16px; }
.alert-success { background: var(--success-bg); color: var(--success-text); }
.alert-error { background: var(--error-bg); color: var(--error-text); }

/* Forms */
label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 0.9rem; }
input[type=text], input[type=email], input[type=password], input[type=search], input[type=tel], input[type=url], input[type=number], input[type=date], select, textarea {
    font: inherit; font-weight: normal; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: #fff; color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #b8cff2; border-color: var(--primary); }
.has-error input, .has-error select, .has-error textarea { border-color: var(--danger); }
.field-error { color: var(--danger); font-weight: normal; font-size: 0.85rem; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: normal; }
label.check input { width: auto; }
fieldset { border: 1px solid var(--border); border-radius: 6px; padding: 12px 16px 16px; margin: 0; }
legend { font-weight: 600; padding: 0 6px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 20px; }
.form-grid.inner { gap: 12px 20px; }
.span-2 { grid-column: span 2; }
.address-grid { grid-template-columns: 2fr 1.4fr 1fr; }
.span-full { grid-column: 1 / -1; }
.form-actions { display: flex; align-items: center; gap: 16px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

.filters { display: flex; gap: 8px; align-items: center; margin-bottom: 16px; flex-wrap: wrap; }
.filters input[type=search] { max-width: 340px; }
.filters select { width: auto; }

.pager { display: flex; justify-content: center; gap: 20px; margin: 16px 0; color: var(--muted); }

/* Auth pages */
.auth-body { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 16px; }
.auth-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 32px; width: 100%; max-width: 400px; }
.auth-card h1 { font-size: 1.25rem; margin-bottom: 16px; }
.brand-lg { font-weight: 700; font-size: 1.1rem; margin-bottom: 20px; color: var(--primary); }

@media (max-width: 720px) {
    .topbar { flex-wrap: wrap; height: auto; padding: 12px 16px; gap: 8px 16px; }
    .mainnav { order: 3; width: 100%; overflow-x: auto; }
    .container { padding: 16px; }
    .form-grid, .address-grid { grid-template-columns: 1fr; }
    .span-2 { grid-column: auto; }
    .details { grid-template-columns: 1fr; gap: 2px; }
    .details dd { margin-bottom: 8px; }
}

/* ---- Shared bits ---------------------------------------------------------- */
.card-head-row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding-bottom: 12px; }
.card-head-row h2 { margin: 0; }
.btn-sm { padding: 4px 12px; font-size: 0.85rem; }
.nowrap { white-space: nowrap; }
.small { font-size: 0.85rem; }
.row-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row-actions form { margin: 0; }
.btn-text { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; }
.btn-text:hover { text-decoration: underline; }
.btn-text.danger { color: var(--danger); }
.form-narrow { max-width: 720px; }
.page-head form { margin: 0; }
a.stat-link { color: inherit; }
a.stat-link:hover { text-decoration: none; border-color: #b8cff2; }
.auth-links { margin: 16px 0 0; }
.auth-sub { margin: -14px 0 20px; }

/* ---- Roles & geocode ----------------------------------------------------- */
.badge-admin    { background: #efe8fb; color: #5b33a6; }
.badge-tech     { background: #e6f4f1; color: #116b5c; }
.badge-customer { background: #e7effc; color: #1f5fbf; }
.geo-ok      { background: #e3f4e8; color: #1d6b39; }
.geo-pending { background: #fff0d9; color: #9a5300; }
.geo-failed  { background: #fde2e1; color: #a1160d; }
.alert-warn  { background: #fff6e0; color: #7a4d00; }
input[readonly], .locked { background: #f6f7f9; color: var(--muted); cursor: not-allowed; }
.coords { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.coords code { font-family: var(--mono); font-size: 0.95rem; background: #f6f7f9; border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; }
.lock-note { color: var(--muted); font-size: 0.85rem; font-weight: normal; }
.card-head-row.flat { padding: 0 0 12px; }
.mt { margin-top: 12px; }
.text-danger { color: var(--danger); }
.portal-hero { margin-bottom: 20px; }

/* ---- Tickets ------------------------------------------------------------- */
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 0; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > label { font-weight: 600; font-size: 0.9rem; }
.align-end { align-self: end; padding-bottom: 8px; }
label.inline { flex-direction: row; align-items: center; gap: 8px; font-weight: normal; }
label.inline select { width: auto; }
.prio-critical { background: #fde2e1; color: #a1160d; }
.prio-high     { background: #fff0d9; color: #9a5300; }
.prio-normal   { background: #e7effc; color: #1f5fbf; }
.prio-low      { background: #eef1f5; color: #5b6578; }
.st-open        { background: #e7effc; color: #1f5fbf; }
.st-in_progress { background: #efe8fb; color: #5b33a6; }
.st-waiting     { background: #fff0d9; color: #9a5300; }
.st-resolved    { background: #e3f4e8; color: #1d6b39; }
.st-closed      { background: #f1f1f1; color: #777; }
.badge-internal { background: #fff3c4; color: #7a5b00; }

.picker { position: relative; }
.picker-results { position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 4px); background: #fff;
                  border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 8px 24px rgba(20, 30, 50, 0.12);
                  max-height: 320px; overflow-y: auto; }
.picker-option { padding: 8px 12px; cursor: pointer; display: flex; flex-direction: column; border-bottom: 1px solid #f0f2f5; }
.picker-option:last-child { border-bottom: 0; }
.picker-option.is-active { background: #eef4fd; }
.picker-name { font-weight: 600; }
.picker-meta { color: var(--muted); font-size: 0.85rem; }
.picker-empty { padding: 10px 12px; color: var(--muted); }

.ticket-layout { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.ticket-side .card { margin-bottom: 16px; }
.details-stack { grid-template-columns: 1fr; gap: 2px; }
.details-stack dd { margin-bottom: 10px; }
.message { padding: 16px 20px; margin-bottom: 12px; border-left: 4px solid var(--border); }
.message-original { border-left-color: #8aa4c8; }
.message-staff { border-left-color: var(--primary); }
.message-customer { border-left-color: #2e9e5b; }
.message-internal { border-left-color: #e0b100; background: #fffbea; }
.message-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.message-head time { margin-left: auto; font-size: 0.85rem; }
.message-body { white-space: pre-wrap; word-break: break-word; }
.reply-form { display: flex; flex-direction: column; gap: 12px; }
.reply-form.is-internal { background: #fffbea; border-color: #ecd98a; }
.reply-form .form-actions { justify-content: space-between; flex-wrap: wrap; }
.segmented { display: flex; gap: 16px; flex-wrap: wrap; }
.segmented label { flex-direction: row; align-items: center; gap: 6px; font-weight: 600; }
.ticket-side input[readonly] { background: #f6f7f9; }
.alert-info { background: #e7effc; color: #1f4f99; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; flex-wrap: wrap; }
.tabs a { padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.public-head { padding: 16px 24px; background: #16202f; }
.public-head .brand { color: #fff; font-weight: 700; }
.container-narrow { max-width: 820px; }

@media (max-width: 900px) {
    .ticket-layout { grid-template-columns: 1fr; }
}
.stat-alert { border-color: #f0b4ae; background: #fff6f5; }
.stat-alert .stat-num { color: #a1160d; }

/* ---- Staff schedule settings ---------------------------------------------- */
.field-label { display: block; font-weight: 600; font-size: 0.9rem; margin-bottom: 6px; }
.hours-table { border-collapse: collapse; }
.hours-table td { padding: 3px 10px 3px 0; }
.hours-table input[type=time] { font: inherit; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.input-color { width: 64px; height: 38px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }
.has-error .hours-table input { border-color: var(--danger); }
.input-short { max-width: 100px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: flex-end; }
.row-muted td { color: var(--muted); }

/* ---- Scheduling --------------------------------------------------------------- */
.container-wide { max-width: none; }
.section-label { font-size: 0.78rem; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 0 0 12px; }
.section-label .muted { text-transform: none; letter-spacing: 0; font-weight: 600; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #999; margin-right: 6px; vertical-align: 1px; flex: none; }
.job-open      { background: #e7effc; color: #1f5fbf; }
.job-completed { background: #e3f4e8; color: #1d6b39; }
.job-cancelled { background: #f1f1f1; color: #777; }
.tech-tag { display: inline-flex; align-items: center; margin-right: 12px; white-space: nowrap; }

/* Job form */
.job-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px 18px; }
.job-grid .span-2 { grid-column: span 2; }
.job-grid .span-all { grid-column: 1 / -1; }
.state-zip { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.who-when .tech-row { display: flex; gap: 16px; align-items: center; margin-bottom: 10px; }
.tech-team { width: 110px; flex: none; font-weight: 600; font-size: 0.9rem; }
.tech-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { flex-direction: row; align-items: center; gap: 6px; font-weight: 500; padding: 6px 12px; border: 1px solid var(--border);
        border-radius: 999px; background: #fff; cursor: pointer; user-select: none; }
.chip input { width: auto; margin: 0; }
.chip.is-on { border-color: #8fb0e6; background: #eef4fd; font-weight: 600; }
.chip-lead { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.04em; background: var(--primary); color: #fff; border-radius: 999px; padding: 1px 6px; }
.has-error .tech-chips .chip { border-color: var(--danger); }
.when-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px 18px; margin: 16px 0; }
.when-grid .span-wide { grid-column: span 2; }
.when-grid input[type=datetime-local] { font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; width: 100%; }
.who-when .form-actions { flex-wrap: wrap; }
.open-times { border-top: 1px solid var(--border); margin-top: 18px; padding-top: 16px; }
.open-times input[type=date], .board-filters input[type=date] { font: inherit; padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px; }
.slot-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; color: var(--muted); }
.slot.is-picked { border-color: var(--primary); background: #eef4fd; font-weight: 600; }

/* Board */
.board-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; margin-bottom: 14px; }
.board-title { font-size: 1.25rem; margin: 0 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg a { padding: 5px 14px; color: var(--text); background: #fff; font-weight: 600; font-size: 0.9rem; }
.seg a + a { border-left: 1px solid var(--border); }
.seg a.is-on { background: #16202f; color: #fff; }
.seg a:hover { text-decoration: none; }
.board-filters { display: flex; gap: 8px; margin-left: auto; align-items: center; }
.board-filters select { width: auto; padding: 6px 8px; }
.board-links { display: flex; gap: 12px; align-items: center; }
.board-notice { margin-bottom: 12px; }
.board { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 16px; align-items: start; }
.board-side .card { padding: 14px; }
.unsched-item { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px; border: 1px solid var(--border); border-left: 3px solid #b0b8c4;
                border-radius: 6px; margin-bottom: 8px; color: var(--text); background: #fff; cursor: grab; touch-action: none; }
.unsched-item:hover { text-decoration: none; border-color: #b8cff2; }
.board-main { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.board-grid { min-width: 900px; }
.board-grid.is-week { min-width: 1750px; }
.is-week .b-block { padding: 1px 4px 1px 4px; border-left-width: 3px; font-size: 0.72rem; }
.is-week .b-block-meta { font-size: 0.68rem; }
.board-grid.is-saving { opacity: 0.7; pointer-events: none; }
.b-row { display: grid; grid-template-columns: 170px minmax(0, 1fr); border-bottom: 1px solid var(--border); }
.b-row:last-child { border-bottom: 0; }
.b-name { padding: 8px 12px; border-right: 1px solid var(--border); display: flex; flex-direction: column; justify-content: center; min-width: 0; }
.b-name strong { display: flex; align-items: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.92rem; }
.b-name small { font-size: 0.78rem; margin-left: 15px; }
.b-track { position: relative; height: max(54px, calc(var(--lanes, 1) * 42px + 8px)); touch-action: none; cursor: crosshair; }
.b-head .b-track { height: 30px; cursor: default; }
.b-head { background: #f6f7fb; position: sticky; top: 0; z-index: 3; }
.b-hour-label, .b-day-label { position: absolute; top: 7px; font-size: 0.75rem; font-weight: 600; color: var(--muted); padding-left: 4px; white-space: nowrap; }
.b-day-label { text-align: center; padding: 0; border-left: 1px solid var(--border); top: 0; line-height: 30px; }
.b-day-label.is-today { color: var(--primary); background: #eef4fd; }
.b-line { position: absolute; top: 0; bottom: 0; border-left: 1px solid #eceef3; pointer-events: none; }
.b-line-day { border-left-color: #d6dae3; }
.b-off { position: absolute; top: 0; bottom: 0; pointer-events: none;
         background: repeating-linear-gradient(135deg, rgba(150, 160, 180, 0.13) 0 2px, transparent 2px 9px); }
.b-now { position: absolute; top: 0; bottom: 0; border-left: 2px solid #e5484d; pointer-events: none; z-index: 2; }
.b-block { position: absolute; z-index: 1; display: flex; flex-direction: column; justify-content: center; overflow: hidden;
           padding: 2px 10px 2px 7px; border-left: 4px solid #888; border-radius: 5px; color: var(--text); font-size: 0.8rem; line-height: 1.25;
           cursor: grab; touch-action: none; box-shadow: 0 1px 2px rgba(20, 30, 50, 0.08); }
.b-block:hover { text-decoration: none; filter: brightness(0.97); }
.b-block-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.b-block-meta { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.75rem; }
.b-block.is-overlap { outline: 2px solid #e5484d; outline-offset: -2px; }
.b-block.is-done { opacity: 0.55; }
.b-block.is-moving { opacity: 0.35; }
.b-resize { position: absolute; top: 0; right: 0; bottom: 0; width: 8px; cursor: ew-resize; }
.b-resize:hover { background: rgba(0, 0, 0, 0.08); }
.b-ghost { position: absolute; top: 3px; bottom: 3px; z-index: 4; border: 2px dashed var(--primary); background: rgba(31, 95, 191, 0.10);
           border-radius: 5px; font-size: 0.75rem; font-weight: 600; color: var(--primary); padding: 2px 6px; pointer-events: none; white-space: nowrap; overflow: hidden; }
.b-ghost.is-other { border-color: #7e44b5; color: #7e44b5; background: rgba(126, 68, 181, 0.10); }
.b-floater { position: fixed; z-index: 50; pointer-events: none; background: #16202f; color: #fff; font-size: 0.8rem; padding: 4px 10px; border-radius: 6px; }
body.is-dragging, body.is-dragging * { cursor: grabbing !important; user-select: none; }
.board-help { margin-top: 10px; }

@media (max-width: 900px) {
    .board { grid-template-columns: 1fr; }
    .board-filters { margin-left: 0; }
    .job-grid, .when-grid { grid-template-columns: 1fr 1fr; }
    .job-grid .span-2, .job-grid .span-all, .when-grid .span-wide { grid-column: 1 / -1; }
    .who-when .tech-row { flex-direction: column; align-items: flex-start; gap: 6px; }
}
.plain-list { list-style: none; margin: 0; padding: 0; }
.plain-list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.plain-list li:last-child { border-bottom: 0; }
