/* ============================================================================
   Field — a technician's heads-down console for Autotask.
   Design intent: instrumentation, not a brochure. Dense but legible at arm's
   length, thumb-reachable controls, status carried by colour + a left spine on
   every ticket. System fonts (zero network on a phone in the field); a monospace
   "data" face for the things a tech reads precisely: ticket #, time, due.
   ========================================================================== */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --line: #d9dde3;
  --ink: #161c24;
  --ink-2: #5a6573;
  --ink-3: #8a94a3;
  --accent: #2563d9;
  --accent-ink: #ffffff;

  /* priority spine colours (P1..P4-ish; mapped by class, not value) */
  --p-crit: #d1342f;
  --p-high: #e8702a;
  --p-med: #c9a227;
  --p-low: #4a8c5f;
  --p-none: #9aa3b0;

  --ok: #1f9d57;
  --warn: #c9821f;
  --danger: #d1342f;

  --radius: 12px;
  --tap: 48px;            /* minimum touch target */
  --pad: 16px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1620;
    --surface: #161f2b;
    --surface-2: #1d2835;
    --line: #2a3645;
    --ink: #e8edf3;
    --ink-2: #9fadbd;
    --ink-3: #6b7888;
    --accent: #4f8cff;
    --accent-ink: #0a0f16;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  line-height: 1.4;
  overscroll-behavior-y: contain;
}
button { font-family: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }

/* ---- App frame ---------------------------------------------------------- */
#app { max-width: 560px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) var(--pad) 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topbar .back {
  appearance: none; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  height: 40px; padding: 0 14px 0 9px; margin-left: -2px;
  display: flex; align-items: center; gap: 3px;
  font: 600 14px var(--sans); border-radius: 999px; cursor: pointer;
}
.topbar .back:active { background: var(--surface-2); }
.topbar .back .bk-ic { font-size: 21px; line-height: 1; margin-top: -2px; }
.topbar h1 { font-size: 17px; font-weight: 700; margin: 0; letter-spacing: -0.01em; flex: 1; }
.topbar .who { font: 600 12px var(--mono); color: var(--ink-3); }

.content { flex: 1; padding: var(--pad) var(--pad) 48px; }

/* ---- Top tab dock ------------------------------------------------------- */
.topdock {
  position: sticky; top: 0; z-index: 15;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px;
  background: var(--line); border-bottom: 1px solid var(--line);
}
.dock-tab {
  appearance: none; border: 0; background: var(--surface); color: var(--ink-2);
  min-height: 58px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: 600 10.5px var(--sans); cursor: pointer; padding: 4px 2px; text-align: center; line-height: 1.1;
}
.dock-tab .ic { font-size: 18px; line-height: 1; filter: grayscale(0.2); }
.dock-tab:active { background: var(--surface-2); }
.dock-tab.active { color: var(--accent); box-shadow: inset 0 -2px 0 var(--accent); }
.dock-tab.disabled { opacity: 0.38; cursor: default; }

/* ---- Filter / sort button ----------------------------------------------- */
.filterbtn {
  appearance: none; width: 100%; display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 11px 14px; margin: 4px 0 4px; cursor: pointer; color: inherit; text-align: left;
}
.filterbtn:active { background: var(--surface-2); }
.filterbtn .fic { font-size: 17px; color: var(--ink-2); }
.filterbtn .fsum { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.filterbtn .fsum b { font-size: 14px; font-weight: 700; }
.filterbtn .fsum span { font: 600 11px var(--mono); color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filterbtn .chev { font-size: 20px; color: var(--ink-3); }

/* Selects inside a sheet (filter tray) */
.sheet select {
  width: 100%; min-height: 48px; font: 600 15px var(--sans); color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 0 34px 0 12px; margin-bottom: 14px; cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238a94a3' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 11px;
  text-overflow: ellipsis;
}
.sheet .seg { margin-bottom: 6px; }

/* ---- Section headers ---------------------------------------------------- */
.eyebrow {
  font: 700 11px var(--mono); letter-spacing: 0.14em; text-transform: uppercase;
  color: var(--ink-3); margin: 22px 2px 10px; display: flex; justify-content: space-between; align-items: baseline;
}
.eyebrow:first-child { margin-top: 4px; }
.eyebrow .count { color: var(--accent); }

/* ---- Ticket card -------------------------------------------------------- */
.card {
  position: relative; display: block; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 14px 14px 20px;
  margin-bottom: 10px; cursor: pointer; color: inherit;
  appearance: none; font: inherit;
  transition: transform .06s ease, border-color .12s ease;
}
.card:active { transform: scale(0.992); border-color: var(--accent); }
.card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 5px;
  border-radius: var(--radius) 0 0 var(--radius); background: var(--spine, var(--p-none));
}
.card.p-crit { --spine: var(--p-crit); }
.card.p-high { --spine: var(--p-high); }
.card.p-med  { --spine: var(--p-med); }
.card.p-low  { --spine: var(--p-low); }

.card .row1 { display: flex; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.card .num { font: 700 12px var(--mono); color: var(--ink-3); }
.card .due { margin-left: auto; font: 600 11px var(--mono); color: var(--ink-2); }
.card .due.overdue { color: var(--danger); }
.cardstatus {
  margin-left: auto; font: 600 11px var(--mono); padding: 3px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); white-space: nowrap;
}
.cardmeta { font-size: 13px; color: var(--ink-2); margin-top: 2px; }

/* Status-specific colours: pills, detail chip, AND the card's left spine. */
.cardstatus.st-new,         .chip.status.st-new          { background: #d1342f; color: #fff; }
.cardstatus.st-clientreplied,.chip.status.st-clientreplied{ background: #edc200; color: #161c24; }
.cardstatus.st-inprogress,  .chip.status.st-inprogress   { background: #e85aa6; color: #fff; }
.cardstatus.st-waiting,     .chip.status.st-waiting      { background: #1e3a8a; color: #fff; }
.cardstatus.st-waitclient,  .chip.status.st-waitclient   { background: #5b9be0; color: #fff; }
.card.st-new         { --spine: #d1342f; }
.card.st-clientreplied { --spine: #edc200; }
.card.st-inprogress  { --spine: #e85aa6; }
.card.st-waiting     { --spine: #1e3a8a; }
.card.st-waitclient  { --spine: #5b9be0; }

/* Ticket-detail header (mirrors the card) */
.dhead { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.dhead .num { font: 700 12px var(--mono); color: var(--ink-3); }
.dline { font-size: 14px; font-weight: 600; color: var(--ink); margin: 2px 0 4px; }
.dline.dline2 { font: 600 12px var(--mono); color: var(--ink-3); margin-bottom: 16px; }
.card .title { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; margin: 2px 0 6px; }
.card .meta { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  font: 600 11px var(--mono); padding: 3px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); white-space: nowrap;
}
.chip.company { color: var(--ink); }
.chip.status { background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); }

/* ---- Service call row --------------------------------------------------- */
.callrow {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  margin-bottom: 8px; width: 100%; text-align: left; color: inherit;
  appearance: none; font: inherit; cursor: pointer;
  transition: transform .06s ease, border-color .12s ease;
}
.callrow:active { transform: scale(0.992); border-color: var(--accent); }
.callrow .time { font: 700 13px var(--mono); color: var(--accent); min-width: 64px; }
.callrow .ctxt { flex: 1; min-width: 0; }
.callrow .ctxt b { display: block; font-size: 14px; font-weight: 600; }
.callrow .ctxt span { font-size: 12px; color: var(--ink-2); display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.callrow .chev { font-size: 20px; color: var(--ink-3); margin-left: 2px; }
.callrow.done { opacity: 0.5; }

.kv { font-size: 15px; margin: 0 2px 6px; }
.maplink { display: flex; align-items: center; justify-content: center; min-height: 52px; text-decoration: none; margin: 4px 0 8px; padding: 0; line-height: 1.2; }
.tellink { color: var(--accent); text-decoration: none; font-weight: 600; }

.empty { color: var(--ink-3); font-size: 14px; padding: 14px 2px; }

/* ---- Filter dropdowns (organization + sort) ----------------------------- */
.filters { display: flex; gap: 10px; margin: 4px 0 4px; }
.filters .fl { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.filters .fl span { font: 700 10px var(--mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3); padding-left: 2px; }
.filters select {
  width: 100%; min-height: 44px; font: 600 14px var(--sans); color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  padding: 0 32px 0 12px; cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238a94a3' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center; background-size: 11px;
  text-overflow: ellipsis;
}
.filters select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- Version footer ----------------------------------------------------- */
.ver { text-align: center; font: 600 11px var(--mono); color: var(--ink-3); padding: 24px 0 8px; }
.login .ver { margin-top: 20px; padding: 0; text-align: left; }

/* ---- Ticket detail ------------------------------------------------------ */
.detail h2 { font-size: 20px; line-height: 1.25; letter-spacing: -0.02em; margin: 4px 0 6px; }
.detail .sub { font: 600 12px var(--mono); color: var(--ink-3); margin-bottom: 16px; }
/* ---- Ticket detail: action bar at the top (mirrors the home dock) ------- */
.actionbar-top {
  position: sticky; top: 0; z-index: 14;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px;
  background: var(--line); border-bottom: 1px solid var(--line);
}
.actionbar-top button {
  appearance: none; border: 0; background: var(--surface); color: var(--ink);
  min-height: 56px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: 600 11px var(--sans); cursor: pointer;
}
.actionbar-top button:active { background: var(--surface-2); }
.actionbar-top button .ic { font-size: 19px; line-height: 1; }

/* Connected-ticket link row on the service-call screen */
.linkrow {
  appearance: none; width: 100%; display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin: 0 0 14px; cursor: pointer; color: inherit; text-align: left; font: inherit;
}
.linkrow:active { background: var(--surface-2); }
.linkrow span:first-child { flex: 1; min-width: 0; font-size: 14px; }
.linkrow .chev { font-size: 20px; color: var(--ink-3); }
.ph-label { font: 600 11px var(--mono); color: var(--ink-3); margin-left: 6px; }

/* Weekday line in the calendar time column */
.callrow .time .d { display: block; font: 700 10px var(--mono); color: var(--ink-3); letter-spacing: .04em; }
.callrow .time { display: flex; flex-direction: column; justify-content: center; }

/* Links inside any detail body (description AND notes) */
.detail .dlink { color: var(--accent); text-decoration: none; font-weight: 600; word-break: break-all; }
.detail .dlink:active { text-decoration: underline; }

.detail .desc { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; font-size: 14px; color: var(--ink-2); white-space: pre-wrap; margin-bottom: 18px; word-break: break-word; }
.seg3 button { font-size: 12px; padding: 0 4px; line-height: 1.15; }

.notes .note { background: var(--surface); border: 1px solid var(--line); border-left: 3px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; }
.notes .note .nhead { display: flex; gap: 8px; align-items: baseline; margin-bottom: 4px; }
.notes .note .nhead time { font: 600 11px var(--mono); color: var(--ink-3); }
.notes .note .nhead .vis { font: 600 10px var(--mono); text-transform: uppercase; letter-spacing: .08em; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--ink-3); }
.notes .note .nhead .vis.client { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.notes .note p { margin: 0; font-size: 14px; white-space: pre-wrap; }

/* ---- Thumb-zone action bar (the signature) ------------------------------ */
.actionbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  max-width: 560px; margin: 0 auto;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px;
  background: var(--line);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.actionbar button {
  appearance: none; border: 0; background: var(--surface); color: var(--ink);
  min-height: 60px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font: 600 11px var(--sans); cursor: pointer;
}
.actionbar button:active { background: var(--surface-2); }
.actionbar button .ic { font-size: 19px; line-height: 1; }

/* ---- Sheet (bottom modal for quick edits) ------------------------------- */
.scrim { position: fixed; inset: 0; background: rgba(5,8,12,.5); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .15s ease; }
.scrim.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  max-width: 560px; margin: 0 auto;
  background: var(--surface); border-radius: 18px 18px 0 0;
  border: 1px solid var(--line); border-bottom: 0;
  padding: 8px var(--pad) calc(20px + env(safe-area-inset-bottom));
  transform: translateY(100%); transition: transform .18s cubic-bezier(.2,.8,.2,1);
  max-height: 86dvh; overflow-y: auto;
}
.sheet.open { transform: translateY(0); }
.sheet .grip { width: 38px; height: 4px; border-radius: 99px; background: var(--line); margin: 6px auto 14px; }
.sheet h3 { margin: 0 0 14px; font-size: 16px; }

.optlist { display: flex; flex-direction: column; gap: 8px; }
.opt {
  display: flex; align-items: center; gap: 12px; width: 100%;
  min-height: var(--tap); padding: 0 14px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); border-radius: 10px;
  font: 600 15px var(--sans); cursor: pointer; text-align: left;
}
.opt:active { background: var(--surface-2); }
.opt.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.opt .dot { width: 9px; height: 9px; border-radius: 99px; background: var(--p-none); }
.opt.selected .dot { background: var(--accent); }

label.field { display: block; font: 700 11px var(--mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3); margin: 4px 2px 6px; }
textarea, input[type="text"], input[type="number"] {
  width: 100%; font: 400 15px var(--sans); color: var(--ink);
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px;
  padding: 12px; margin-bottom: 14px; resize: vertical;
}
textarea { min-height: 110px; }
.seg { display: flex; gap: 8px; margin-bottom: 16px; }
.seg button { flex: 1; min-height: var(--tap); border: 1px solid var(--line); background: var(--surface); color: var(--ink-2); border-radius: 10px; font: 600 13px var(--sans); cursor: pointer; }
.seg button.on { border-color: var(--accent); color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }

.btn-primary {
  width: 100%; min-height: 52px; border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); font: 700 15px var(--sans);
  cursor: pointer;
}
.btn-primary:active { filter: brightness(.94); }
.btn-primary[disabled] { opacity: .5; }

/* ---- Login -------------------------------------------------------------- */
.login { flex: 1; display: flex; flex-direction: column; justify-content: center; padding: var(--pad); gap: 18px; }
.login .brand { font: 800 13px var(--mono); letter-spacing: .3em; text-transform: uppercase; color: var(--accent); }
.login h1 { font-size: 27px; letter-spacing: -0.02em; margin: 0; }
.login p { color: var(--ink-2); margin: 0 0 6px; font-size: 14px; }

/* ---- Toast -------------------------------------------------------------- */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: 11px 16px; border-radius: 999px;
  font: 600 13px var(--sans); z-index: 60; opacity: 0; pointer-events: none; transition: all .18s ease; max-width: 88%;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--danger); color: #fff; }

/* ---- Skeleton / spinner ------------------------------------------------- */
.spin { width: 22px; height: 22px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 99px; animation: sp .7s linear infinite; margin: 40px auto; }
@keyframes sp { to { transform: rotate(360deg); } }
