:root {
    color-scheme: light;
    --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f2f4f7; --text: #191919; --text-2: #5f6368; --border: #e2e5e9;
    --accent: #1f6fd1; --accent-text: #ffffff; --accent-soft: #e8f1fc;
    --urgent: #b42318; --urgent-bg: #fef3f2; --high: #b54708; --high-bg: #fffaeb; --medium: #344054; --medium-bg: #f2f4f7;
    --low: #667085; --low-bg: #f9fafb; --ok: #067647; --ok-bg: #ecfdf3; --warn-bg: #fffaeb; --warn-border: #fedf89;
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg: #111214; --surface: #191a1c; --surface-2: #202226; --text: #efefef; --text-2: #a0a4a8; --border: #2c2e31;
        --accent: #4b93ea; --accent-text: #0b1220; --accent-soft: #1b2a3d;
        --urgent: #f97066; --urgent-bg: #3a1714; --high: #fdb022; --high-bg: #33250b; --medium: #d0d5dd; --medium-bg: #26282c;
        --low: #9aa0a6; --low-bg: #202226; --ok: #47cd89; --ok-bg: #0f2b1d; --warn-bg: #2e2510; --warn-border: #6b5413;
    }
}
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #111214; --surface: #191a1c; --surface-2: #202226; --text: #efefef; --text-2: #a0a4a8; --border: #2c2e31;
    --accent: #4b93ea; --accent-text: #0b1220; --accent-soft: #1b2a3d;
    --urgent: #f97066; --urgent-bg: #3a1714; --high: #fdb022; --high-bg: #33250b; --medium: #d0d5dd; --medium-bg: #26282c;
    --low: #9aa0a6; --low-bg: #202226; --ok: #47cd89; --ok-bg: #0f2b1d; --warn-bg: #2e2510; --warn-border: #6b5413;
}

* { box-sizing: border-box; }
body {
    margin: 0; background: var(--bg); color: var(--text);
    font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", sans-serif;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--text-2); }
.wrap { max-width: 1240px; margin: 0 auto; padding: 0 16px; }

/* top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar .wrap { display: flex; align-items: center; gap: 16px; min-height: 56px; flex-wrap: wrap; }
.brand { font-weight: 600; font-size: 15px; text-decoration: none; color: var(--text); }
.topbar .status { display: flex; gap: 14px; margin-left: auto; font-size: 12px; color: var(--text-2); flex-wrap: wrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--low); vertical-align: 1px; }
.dot.on { background: var(--ok); }
.dot.bad { background: var(--urgent); }
.dot.busy { background: var(--high); }
.dot.big { width: 14px; height: 14px; margin: 6px 0 0; flex: none; }

/* navigation */
.nav { display: flex; gap: 4px; }
.nav a { text-decoration: none; color: var(--text-2); padding: 6px 10px; border-radius: 6px; font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.wa-link { color: inherit; text-decoration: none; }
.wa-link:hover { color: var(--text); }
.page-head h1 { font-size: 20px; margin: 18px 0 12px; }

/* WhatsApp page */
.wa-state { display: flex; gap: 12px; align-items: flex-start; }
.wa-state-title { font-size: 18px; font-weight: 600; }
.wa-step { color: var(--text); font-weight: 500; margin: 2px 0; }
.qr { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin: 8px 0 14px; }
.qr img { width: 300px; height: 300px; background: #fff; border: 1px solid var(--border); border-radius: 8px; image-rendering: pixelated; }
.progress { width: 100%; max-width: 360px; height: 10px; accent-color: var(--accent); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.btn.danger { color: var(--urgent); border-color: var(--urgent); }
.btn.danger:hover { background: var(--urgent-bg); }
.small { font-size: 12px; }
.strong, td.num.strong { font-weight: 600; color: var(--text); }
.warn-text { color: var(--high); }
.timeline li.is-error > div:last-child { color: var(--urgent); }
table.plain tbody tr, tbody tr.static { cursor: default; }
table.plain tbody tr:hover, tbody tr.static:hover { background: none; }
.metric.static { cursor: default; }
.metric.static:hover { background: none; }

/* buttons & fields */
.btn { border: 1px solid var(--border); background: var(--surface); border-radius: 6px; padding: 6px 12px; cursor: pointer; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .6; cursor: default; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-2); }
.field input, .field select, .field textarea {
    background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 7px 9px; min-width: 150px; font-size: 14px;
}
.field input:focus, .field select:focus, .field textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* login */
.login { max-width: 360px; margin: 12vh auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 28px; }
.login h1 { font-size: 18px; margin: 0 0 4px; }
.login form { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.login .field input { width: 100%; }
.error { color: var(--urgent); font-size: 13px; }

/* metric strip */
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; margin: 16px 0; }
.metric { padding: 12px 16px; border-right: 1px solid var(--border); text-align: left; background: none; border-top: 0; border-bottom: 0; border-left: 0; cursor: pointer; }
.metric:last-child { border-right: 0; }
.metric:hover { background: var(--surface-2); }
.metric .v { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; }
.metric .l { color: var(--text-2); font-size: 12px; }

/* filters */
.filters { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin: 0 0 12px; }
.filters .grow { flex: 1 1 220px; }
.filters .grow input { width: 100%; }
.count { margin-left: auto; font-size: 12px; color: var(--text-2); }

/* table */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-2); font-weight: 600; font-size: 12px; white-space: nowrap; background: var(--surface); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--text-2); }
/* sortable headers (tickets list) and pager */
th[aria-sort] { padding: 0; }
th .sort {
    display: flex; align-items: center; gap: 5px; width: 100%; padding: 9px 12px; border: 0; background: none;
    color: inherit; font-weight: inherit; font-size: inherit; text-align: left; white-space: nowrap; cursor: pointer;
}
th .sort:hover, th .sort.active { color: var(--text); }
th .sort:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
th .sort .arrow { font-size: 10px; opacity: .4; }
th .sort.active .arrow { opacity: 1; }
.pager { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px; margin: 12px 0 0; }
.pager .page-of { font-size: 12px; color: var(--text-2); padding: 0 6px; }
.pager select { background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 12px; margin-left: 8px; }
.pager select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
td.title { min-width: 280px; }
td.title .t { font-weight: 500; }
.nowrap { white-space: nowrap; }
.empty { padding: 40px; text-align: center; color: var(--text-2); }

/* badges */
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 10px; white-space: nowrap; }
.p-urgent { color: var(--urgent); background: var(--urgent-bg); }
.p-high { color: var(--high); background: var(--high-bg); }
.p-medium { color: var(--medium); background: var(--medium-bg); }
.p-low { color: var(--low); background: var(--low-bg); }
.s-new { color: var(--accent); background: var(--accent-soft); }
.s-in_progress { color: var(--high); background: var(--high-bg); }
.s-waiting_reporter { color: var(--medium); background: var(--medium-bg); }
.s-resolved, .s-closed { color: var(--ok); background: var(--ok-bg); }
.missing-count { color: var(--high); font-size: 12px; }

/* detail */
.back { display: inline-block; margin: 16px 0 8px; text-decoration: none; font-size: 13px; }
.detail-head { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 16px; }
.detail-head h1 { font-size: 20px; margin: 0; flex: 1 1 400px; }
.detail-head .num { color: var(--text-2); font-weight: 500; margin-right: 6px; }
.status-form { display: flex; gap: 8px; align-items: end; }
.grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 16px; margin-bottom: 16px; }
.card h2 { font-size: 14px; margin: 0 0 10px; }
dl.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; font-size: 13px; }
dl.facts dt { color: var(--text-2); }
dl.facts dd { margin: 0; overflow-wrap: anywhere; }
.callout { background: var(--warn-bg); border: 1px solid var(--warn-border); border-radius: 8px; padding: 12px 14px; margin-bottom: 16px; font-size: 13px; }
.summary { white-space: pre-wrap; }

.messages { display: flex; flex-direction: column; gap: 12px; }
.msg { border-left: 3px solid var(--border); padding: 2px 0 2px 12px; }
.msg.staff { border-left-color: var(--accent); }
.msg .meta { font-size: 12px; color: var(--text-2); display: flex; gap: 8px; flex-wrap: wrap; }
.msg .who { font-weight: 600; color: var(--text); }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; margin-top: 2px; }
.msg .quote { font-size: 12px; color: var(--text-2); border-left: 2px solid var(--border); padding-left: 8px; margin: 4px 0; white-space: pre-wrap; }
.msg img { max-width: min(360px, 100%); max-height: 360px; border-radius: 6px; border: 1px solid var(--border); display: block; margin-top: 6px; }
.msg video { max-width: min(420px, 100%); border-radius: 6px; margin-top: 6px; display: block; }
.msg audio { margin-top: 6px; max-width: 100%; }
.media-missing { font-size: 12px; color: var(--text-2); font-style: italic; margin-top: 4px; }

.timeline { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.timeline li:last-child { border-bottom: 0; }
.timeline .when { font-size: 12px; color: var(--text-2); }
.note-form { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.note-form textarea { min-height: 70px; resize: vertical; width: 100%; }

@media (max-width: 860px) {
    .grid { grid-template-columns: 1fr; }
    .metric { border-right: 0; border-bottom: 1px solid var(--border); }
}
.offscreen { position: fixed; left: -9999px; top: 0; opacity: 0; }

/* add a group */
.add-group { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 12px; }
.add-group h3 { font-size: 13px; margin: 0 0 8px; }
.group-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; }
.group-form .grow { flex: 1 1 280px; }
.group-form .grow input { width: 100%; }
.add-group [role="status"] { margin-top: 8px; min-height: 18px; }
.ok-text { color: var(--ok); }
.btn.small-btn { padding: 3px 9px; font-size: 12px; white-space: nowrap; }
td.right { text-align: right; }
