/* Pilot Help Desk styles. Every colour comes from a theme token below:
   light by default, dark when the system prefers it (unless the agent picked
   Light), or when the agent picked Dark (js/theme.js sets data-theme). */

/* --- Theme tokens ----------------------------------------------------------- */
:root {
  color-scheme: light;
  --navy: #002445;
  --aqua: #38bfc3;
  --coral: #ea4949;

  --bg: #f2f3f4;
  --surface: #ffffff;
  --surface-2: #f7f9fa;
  --surface-hover: #e7ecf0;
  --text: #151619;
  --muted: #5b6b7a;
  --heading: #002445;
  --line: #dbe4ec;
  --line-soft: #eef2f5;
  --input-bg: #ffffff;
  --topbar-bg: #002445;
  --primary-bg: #002445;
  --primary-fg: #ffffff;
  --rail-on-bg: #002445;
  --rail-on-fg: #ffffff;
  --shadow: 0 2px 12px rgba(0, 36, 69, 0.07);
  --ok: #1d6f73;
  --warn: #8a5a00;
  --danger: #c23434;

  --open-bg: #ffe3e3;   --open-fg: #a12a2a;
  --await-bg: #fff1d6;  --await-fg: #8a5a00;
  --closed-bg: #eceff2; --closed-fg: #5b6b7a;
  --alert-bg: #fff1f1;  --alert-line: #ffc9c9; --alert-fg: #8f2727;
  --okbox-bg: #e9f8f8;  --okbox-line: #b7e6e7; --okbox-fg: #1d6f73;
  --note-bg: #fff7d6;   --note-line: #e0b100;  --note-fg: #6b5200;
  --note-btn-bg: #e0b100; --note-btn-fg: #2b2100;
}

/* Dark: system preference (unless Light was chosen), or explicitly chosen. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1419; --surface: #182028; --surface-2: #1f2933; --surface-hover: #26323d;
    --text: #e6edf3; --muted: #9aa8b5; --heading: #cfe3f5; --line: #2f3b47; --line-soft: #24303b;
    --input-bg: #11181f; --topbar-bg: #001426; --primary-bg: #2f6fa8; --primary-fg: #ffffff;
    --rail-on-bg: #2f6fa8; --rail-on-fg: #ffffff; --shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
    --ok: #6fd3d6; --warn: #f0c46a; --danger: #ff8a8a;
    --open-bg: #4a1f22;   --open-fg: #ffb4b4;
    --await-bg: #4a3a12;  --await-fg: #ffd98a;
    --closed-bg: #2a333c; --closed-fg: #a9b6c2;
    --alert-bg: #3a1d20;  --alert-line: #6b2a2f; --alert-fg: #ffb4b4;
    --okbox-bg: #123333;  --okbox-line: #1f5c5e; --okbox-fg: #8be0e2;
    --note-bg: #332b10;   --note-line: #a88a1c;  --note-fg: #f1d58a;
    --note-btn-bg: #c9a227; --note-btn-fg: #1d1700;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1419; --surface: #182028; --surface-2: #1f2933; --surface-hover: #26323d;
  --text: #e6edf3; --muted: #9aa8b5; --heading: #cfe3f5; --line: #2f3b47; --line-soft: #24303b;
  --input-bg: #11181f; --topbar-bg: #001426; --primary-bg: #2f6fa8; --primary-fg: #ffffff;
  --rail-on-bg: #2f6fa8; --rail-on-fg: #ffffff; --shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
  --ok: #6fd3d6; --warn: #f0c46a; --danger: #ff8a8a;
  --open-bg: #4a1f22;   --open-fg: #ffb4b4;
  --await-bg: #4a3a12;  --await-fg: #ffd98a;
  --closed-bg: #2a333c; --closed-fg: #a9b6c2;
  --alert-bg: #3a1d20;  --alert-line: #6b2a2f; --alert-fg: #ffb4b4;
  --okbox-bg: #123333;  --okbox-line: #1f5c5e; --okbox-fg: #8be0e2;
  --note-bg: #332b10;   --note-line: #a88a1c;  --note-fg: #f1d58a;
  --note-btn-bg: #c9a227; --note-btn-fg: #1d1700;
}

/* --- Base --------------------------------------------------------------------- */
* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
}
body.app { display: block; place-items: normal; padding: 0; }
[hidden] { display: none !important; }
a { color: inherit; }
a:not(.btn):not(.timer-chip) { text-decoration-color: var(--line); text-underline-offset: 2px; }
a:not(.btn):not(.timer-chip):hover { text-decoration-color: currentColor; }

h1 { margin: 0 0 8px; color: var(--heading); font-size: 22px; }
h2 { margin: 0 0 8px; color: var(--heading); font-size: 17px; }
h3 { margin: 16px 0 4px; color: var(--heading); font-size: 15px; }
p { margin: 8px 0 0; line-height: 1.5; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.empty { padding: 24px 8px; }

.card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-top: 4px solid var(--navy);
  border-radius: 10px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.card.wide { max-width: 860px; margin: 0 auto 16px; }

/* --- Controls ----------------------------------------------------------------- */
.btn {
  display: inline-block;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--heading);
  border-radius: 8px;
  padding: 9px 14px;
  font: inherit;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
  cursor: pointer;
}
.btn:hover { background: var(--surface-hover); }
.btn.primary { background: var(--primary-bg); border-color: var(--primary-bg); color: var(--primary-fg); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.ghost { background: transparent; color: inherit; border-color: rgba(127, 127, 127, 0.4); }
.btn.ghost:hover { background: rgba(127, 127, 127, 0.12); }
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn.danger:hover { background: color-mix(in srgb, var(--danger) 12%, transparent); }
a.btn.primary { display: block; margin-top: 18px; }
.row a.btn.primary { display: inline-block; margin-top: 0; }

label { display: block; margin-top: 18px; font-weight: 600; font-size: 13px; color: var(--heading); }
.row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
input, select, textarea {
  flex: 1;
  min-width: 0;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  padding: 9px 11px;
  font: inherit;
  background: var(--input-bg);
  color: var(--text);
}
select, input[type="date"] { flex: 0 0 auto; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, .rail-item:focus-visible, .tab:focus-visible { outline: 2px solid var(--aqua); outline-offset: 1px; }
input[type="checkbox"] { flex: 0 0 auto; width: 16px; height: 16px; accent-color: var(--aqua); }

.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; margin-top: 18px; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 1px solid var(--line); }

.alert { background: var(--alert-bg); border: 1px solid var(--alert-line); color: var(--alert-fg); border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.alert.ok { background: var(--okbox-bg); border-color: var(--okbox-line); color: var(--okbox-fg); }
.alert.trash { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; justify-content: space-between; }
.side-actions { display: flex; justify-content: flex-end; margin-top: 12px; }
.hint, .status { font-size: 13px; color: var(--muted); }
.hint.ok, .status.ok { color: var(--ok); }
.hint.bad, .status.bad { color: var(--danger); }

/* --- Tables ------------------------------------------------------------------- */
.table { width: 100%; border-collapse: collapse; margin-top: 14px; font-size: 14px; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line); padding: 6px 8px; }
.table td { border-bottom: 1px solid var(--line-soft); padding: 8px; }
.table tr.inactive td { color: var(--muted); }
.table td.actions { white-space: nowrap; text-align: right; }
.table td.actions .btn + .btn { margin-left: 6px; }
.table .num { text-align: right; white-space: nowrap; }
.table .key { white-space: nowrap; font-size: 13px; }
.table .subject { min-width: 220px; }
.table .s1 { color: var(--text); }
.s2 { font-size: 12px; color: var(--muted); max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 600px) {
  .table th:nth-child(2), .table td:nth-child(2) { display: none; }
}

/* --- App shell ---------------------------------------------------------------- */
.topbar { display: flex; align-items: center; gap: 12px; background: var(--topbar-bg); color: #fff; padding: 12px 16px; margin-bottom: 16px; }
.topbar .nav { display: flex; gap: 4px; margin-left: 12px; }
.topbar .nav a { color: #fff; opacity: 0.75; text-decoration: none; padding: 6px 10px; border-radius: 6px; font-size: 14px; }
.topbar .nav a.on, .topbar .nav a:hover { opacity: 1; background: rgba(255, 255, 255, 0.12); }
.topbar .who { margin-left: auto; font-size: 13px; opacity: 0.85; }
.topbar .btn.ghost { color: #fff; border-color: rgba(255, 255, 255, 0.35); }
.topbar .btn.ghost:hover { background: rgba(255, 255, 255, 0.12); }
.theme-toggle { min-width: 82px; }
.page { padding: 0 16px 24px; max-width: 1400px; margin: 0 auto; }

/* --- Ticket list ------------------------------------------------------------------ */
.list-layout { display: grid; grid-template-columns: 200px 1fr; gap: 16px; align-items: start; }
.rail { display: flex; flex-direction: column; gap: 2px; position: sticky; top: 16px; }
.rail-item { display: flex; justify-content: space-between; gap: 8px; border: 0; background: transparent; text-align: left; padding: 8px 10px; border-radius: 8px; font: inherit; font-size: 14px; color: var(--heading); cursor: pointer; }
.rail-item:hover { background: var(--surface-hover); }
.rail-item.on { background: var(--rail-on-bg); color: var(--rail-on-fg); }
.rail-item .count { font-size: 12px; opacity: 0.8; }
.list-main { min-width: 0; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.filters input[type="search"] { flex: 1 1 260px; }
.table-wrap { background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); overflow-x: auto; }
.table.tickets { margin-top: 0; }
.table.tickets tbody tr { cursor: pointer; }
.table.tickets tbody tr:hover td { background: var(--surface-2); }
.table.tickets tr.unread .s1 { font-weight: 700; }
.dot-cell { width: 14px; padding-right: 0 !important; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--aqua); }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--closed-bg); color: var(--closed-fg); }
.pill.open { background: var(--open-bg); color: var(--open-fg); }
.pill.awaiting, .pill.warn { background: var(--await-bg); color: var(--await-fg); }
.pill.closed { background: var(--closed-bg); color: var(--closed-fg); }

/* --- Ticket: two columns, sticky side panel ------------------------------------ */
.ticket .back { margin: 0 0 8px; }
.ticket-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.ticket-main { min-width: 0; }
.ticket-title { background: var(--surface); border-radius: 10px; padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 12px; }
.ticket-title h1 { overflow-wrap: anywhere; }
.ticket-title .meta { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 14px; color: var(--muted); }
.ticket-side { position: sticky; top: 16px; display: flex; flex-direction: column; gap: 12px; max-height: calc(100vh - 32px); overflow-y: auto; }
.side-card { background: var(--surface); border-radius: 10px; padding: 16px; box-shadow: var(--shadow); }
.side-card h2 { display: flex; justify-content: space-between; align-items: baseline; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 8px 12px; margin: 0; font-size: 14px; align-items: center; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.facts .controls, .alert.trash .controls { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.facts select { flex: 0 1 auto; max-width: 100%; padding: 4px 8px; font-size: 14px; }
.time-list { list-style: none; padding: 0; margin: 10px 0 0; font-size: 13px; }
.time-list li { padding: 6px 0; border-top: 1px solid var(--line-soft); }
.time-row { display: flex; justify-content: space-between; gap: 8px; }
.time-list .s2 { max-width: 100%; }
.history-list { list-style: none; padding: 0; margin: 0 0 8px; font-size: 13px; }
.history-day { margin: 12px 0 2px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.history-day:first-child { margin-top: 0; }
.history-item { padding: 6px 0 6px 10px; border-left: 2px solid var(--line); line-height: 1.4; }
.history-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.history-head span:last-child { flex: 0 0 auto; }
.history-details { display: grid; grid-template-columns: auto 1fr; gap: 1px 8px; margin: 3px 0 0; font-size: 12px; }
.history-details dt { color: var(--muted); }
.history-details dd { margin: 0; overflow-wrap: anywhere; }

/* Conversation */
.conversation { display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; }
.msg { background: var(--surface); border-radius: 10px; padding: 14px 16px; box-shadow: var(--shadow); border-left: 4px solid var(--line); }
.msg.inbound { border-left-color: var(--heading); }
.msg.outbound { border-left-color: var(--aqua); margin-left: 32px; }
/* Automatic emails to the customer (received / closed notices). */
.msg.auto { border-left-style: dashed; }
.msg.auto .body { color: var(--muted); }
.controls .check { margin-top: 0; flex-basis: 100%; }
.msg.note { border-left-color: var(--note-line); background: var(--note-bg); }
.msg.system { border-left-color: var(--line); background: var(--surface-2); font-size: 14px; }
.msg header { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.msg .addr { font-size: 12px; margin-bottom: 6px; overflow-wrap: anywhere; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; }
.msg .files { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 6px; }
.msg .files li { background: var(--surface-2); border: 1px solid var(--line-soft); border-radius: 6px; padding: 4px 8px; font-size: 13px; }
.msg > .btn { margin-top: 10px; }
/* Customer emails stay on white: they're written for it. */
/* Received email in its own formatting: a white "paper" area, sized by the frame's height message. */
.msg .email-frame { display: block; width: 100%; height: 120px; margin: 4px 0 6px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color-scheme: light; }
.send { font-size: 13px; margin: 0 0 6px; }
.send.ok { color: var(--ok); }
.send.pending { color: var(--warn); }
.send.bad { color: var(--danger); }

/* Composer tabs: reply vs. internal note */
.composer-wrap { margin-bottom: 16px; }
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: var(--surface-hover); color: var(--heading); font: inherit; font-weight: 600; padding: 8px 14px; border-radius: 8px 8px 0 0; cursor: pointer; }
.tab[aria-selected="true"] { background: var(--surface); }
.tab-note[aria-selected="true"] { background: var(--note-bg); color: var(--note-fg); }
.composer { background: var(--surface); border-radius: 10px; padding: 16px 20px; box-shadow: var(--shadow); border-top: 4px solid var(--aqua); }
.composer-wrap .composer { border-top-left-radius: 0; }
.composer label { margin-top: 10px; }
.composer input { width: 100%; margin-top: 4px; }
.composer textarea { width: 100%; margin-top: 12px; line-height: 1.5; resize: vertical; min-height: 140px; }
.composer.note { background: var(--note-bg); border-top-color: var(--note-line); }
/* @mention picker in the note box, and mentions shown in notes. */
.mention-wrap { position: relative; }
.mention-menu { position: absolute; left: 0; top: 100%; z-index: 20; min-width: 260px; max-width: 100%; margin: 2px 0 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); }
.mention-menu li { padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.mention-menu li.active, .mention-menu li:hover { background: var(--line-soft); }
.mention { font-weight: 600; color: var(--note-fg); background: var(--note-bg); border-radius: 4px; padding: 0 2px; }
.note-warning { margin: 0 0 4px; font-size: 13px; color: var(--note-fg); font-weight: 600; }
.note-btn { background: var(--note-btn-bg); border-color: var(--note-btn-bg); color: var(--note-btn-fg); }

/* Timer */
.timer-chip { margin-left: 8px; background: var(--coral); color: #fff; text-decoration: none; font-size: 13px; font-weight: 700; padding: 4px 10px; border-radius: 999px; font-variant-numeric: tabular-nums; }
.timer-chip.paused { background: rgba(255, 255, 255, 0.18); }
.timer-panel { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 4px 0 4px; }
.timer-panel .clock { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--heading); min-width: 96px; }
.timer-panel .clock.live { color: var(--coral); }
.timer-panel .hint { flex-basis: 100%; }
.log-form { flex-basis: 100%; background: var(--surface-2); border: 1px solid var(--line-soft); border-radius: 8px; padding: 12px; }
.log-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.log-grid label { margin: 0; display: flex; flex-direction: column; gap: 4px; }
.log-grid input, .log-grid select { width: 100%; }
.check { display: flex; gap: 6px; align-items: center; font-weight: 400; color: var(--text); margin-top: 8px; }

/* Admin */
.problems { list-style: none; padding: 0; margin: 6px 0 12px; }
.problems li { padding: 8px 0; border-bottom: 1px solid var(--line-soft); }

/* --- Narrow screens ------------------------------------------------------------- */
@media (max-width: 1000px) {
  .ticket-layout { grid-template-columns: 1fr; }
  /* Side panel (timer, details) first, then the conversation. */
  .ticket-side { order: -1; position: static; max-height: none; overflow: visible; }
}
@media (max-width: 800px) {
  .list-layout { grid-template-columns: 1fr; }
  .rail { flex-direction: row; overflow-x: auto; position: static; }
  .rail-item { white-space: nowrap; }
  .msg.outbound { margin-left: 0; }
  .table.tickets th:nth-child(n+7), .table.tickets td:nth-child(n+7) { display: none; }
  .topbar .who { display: none; }
}
