/* Support console — a dense, keyboard-friendly work surface for agents who
   live in this screen all day. Light and dark both, following the OS. */

:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #14181f;
  --text-dim: #5c6672;
  --accent: #1463b8;
  --accent-text: #ffffff;
  --accent-soft: #e8f0fa;

  --new: #7c3aed;
  --open: #1463b8;
  --progress: #b45309;
  --waiting: #a16207;
  --resolved: #15803d;
  --closed: #6b7280;
  --rejected: #b91c1c;
  --duplicate: #6b7280;

  --critical: #b91c1c;
  --high: #c2410c;
  --normal: #5c6672;
  --low: #94a3b8;

  --warn: #b45309;
  --warn-text: #7c4308;
  --warn-soft: #fdf4e6;
  --warn-border: #f0dcc0;

  --radius: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1b1f26;
    --surface-2: #232830;
    --border: #333a45;
    --text: #e8ecf1;
    --text-dim: #98a2b1;
    --accent: #4f9ceb;
    --accent-text: #0b1017;
    --accent-soft: #1c2c40;

    --new: #a78bfa;
    --open: #60a5fa;
    --progress: #fbbf24;
    --waiting: #fcd34d;
    --resolved: #4ade80;
    --closed: #9ca3af;
    --rejected: #f87171;
    --duplicate: #9ca3af;

    --critical: #f87171;
    --high: #fb923c;
    --normal: #9ca3af;
    --low: #6b7280;

    --warn: #fbbf24;
    --warn-text: #f0cf92;
    --warn-soft: #2a2317;
    --warn-border: #4a3c1e;

    --shadow: 0 1px 3px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.loading { padding: 3rem; text-align: center; color: var(--text-dim); }

button, input, select, textarea { font: inherit; color: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: .45rem .8rem;
  transition: background .12s, border-color .12s;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}
button.primary:hover:not(:disabled) { filter: brightness(1.08); }
button.danger { color: var(--rejected); border-color: var(--rejected); background: transparent; }
button.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
button.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }

input[type=text], input[type=email], input[type=password], input[type=search], select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .45rem .6rem;
  width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
textarea { resize: vertical; min-height: 5rem; font-family: inherit; }

a { color: var(--accent); }

/* ── login ─────────────────────────────────────────────────────────────── */

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1rem;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 2rem;
  width: min(400px, 100%);
}
.login-card h1 { margin: 0 0 .25rem; font-size: 1.35rem; }
.login-card p.sub { margin: 0 0 1.5rem; color: var(--text-dim); }
.login-card label { display: block; margin-bottom: 1rem; }
.login-card label span { display: block; margin-bottom: .3rem; font-weight: 500; }
.login-card button { width: 100%; }

.error-msg {
  background: color-mix(in srgb, var(--rejected) 12%, transparent);
  border: 1px solid var(--rejected);
  color: var(--rejected);
  border-radius: 6px;
  padding: .6rem .8rem;
  margin-bottom: 1rem;
}

/* ── shell ─────────────────────────────────────────────────────────────── */

.shell { display: flex; flex-direction: column; min-height: 100vh; }

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar .brand { font-weight: 700; font-size: 1.05rem; white-space: nowrap; }
.topbar .spacer { flex: 1; }
.topbar .who { color: var(--text-dim); font-size: .85rem; }

.tabs { display: flex; gap: .25rem; }
.tabs button {
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: .4rem .75rem;
  color: var(--text-dim);
  font-weight: 500;
}
.tabs button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.main { flex: 1; padding: 1rem; max-width: 1600px; width: 100%; margin: 0 auto; }

/* ── dashboard ─────────────────────────────────────────────────────────── */

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: .75rem;
  margin-bottom: 1.25rem;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem 1rem;
  box-shadow: var(--shadow);
}
.stat-card .n { font-size: 1.8rem; font-weight: 700; line-height: 1.2; }
.stat-card .l { color: var(--text-dim); font-size: .82rem; }
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:hover { border-color: var(--accent); }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1rem;
  overflow: hidden;
}
.panel > h2 {
  margin: 0;
  padding: .75rem 1rem;
  font-size: .95rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.panel-body { padding: 1rem; }

.bar-row { display: grid; grid-template-columns: minmax(140px, 240px) 1fr auto; gap: .75rem; align-items: center; margin-bottom: .5rem; }
/* min-width:0 lets the track shrink to its grid column instead of being sized
   by its content, and gives the fill below a definite width to take its
   percentage of — without it every bar renders full-width whatever its value. */
.bar-row .bar-track { background: var(--surface-2); border-radius: 4px; height: 10px; overflow: hidden; min-width: 0; width: 100%; }
.bar-row .bar-fill { background: var(--accent); height: 100%; border-radius: 4px; max-width: 100%; }
.bar-row .bar-n { color: var(--text-dim); font-variant-numeric: tabular-nums; min-width: 2.5rem; text-align: end; }

/* ── filters ───────────────────────────────────────────────────────────── */

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
  align-items: center;
  margin-bottom: .75rem;
}
.filters input[type=search] { width: min(280px, 100%); }
.filters select { width: auto; min-width: 130px; }
.filters .grow { flex: 1; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  padding: .15rem .5rem;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
  border: 1px solid currentColor;
}
.chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.chip.new { color: var(--new); }
.chip.open { color: var(--open); }
.chip.in_progress { color: var(--progress); }
.chip.waiting_on_user { color: var(--waiting); }
.chip.resolved { color: var(--resolved); }
.chip.closed { color: var(--closed); }
.chip.rejected { color: var(--rejected); }
.chip.duplicate { color: var(--duplicate); }

.pri { font-weight: 600; font-size: .78rem; }
.pri.critical { color: var(--critical); }
.pri.high { color: var(--high); }
.pri.normal { color: var(--normal); }
.pri.low { color: var(--low); }

.kind-tag {
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
  padding: .1rem .4rem;
  border-radius: 4px;
}
.kind-tag.bug { background: color-mix(in srgb, var(--rejected) 15%, transparent); color: var(--rejected); }
.kind-tag.feature { background: color-mix(in srgb, var(--accent) 15%, transparent); color: var(--accent); }

/* ── ticket table ──────────────────────────────────────────────────────── */

.table-wrap { overflow-x: auto; }
table.tickets { width: 100%; border-collapse: collapse; }
table.tickets th {
  text-align: start;
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--text-dim);
  padding: .55rem .75rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  white-space: nowrap;
}
table.tickets td {
  padding: .6rem .75rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
table.tickets tbody tr { cursor: pointer; }
table.tickets tbody tr:hover { background: var(--surface-2); }
table.tickets .num { font-variant-numeric: tabular-nums; color: var(--text-dim); white-space: nowrap; }
table.tickets .title-cell { min-width: 260px; }
table.tickets .title-cell .t { font-weight: 600; }
table.tickets .title-cell .meta { color: var(--text-dim); font-size: .8rem; margin-top: .15rem; }
table.tickets .nowrap { white-space: nowrap; }
.unread-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent); margin-inline-end: .35rem;
}

.empty { padding: 3rem 1rem; text-align: center; color: var(--text-dim); }

.pager { display: flex; gap: .5rem; align-items: center; justify-content: center; padding: .75rem; color: var(--text-dim); }

/* ── ticket detail ─────────────────────────────────────────────────────── */

.detail-grid { display: grid; grid-template-columns: 1fr 320px; gap: 1rem; align-items: start; }
@media (max-width: 980px) { .detail-grid { grid-template-columns: 1fr; } }

.detail-head { display: flex; align-items: flex-start; gap: .75rem; margin-bottom: .5rem; flex-wrap: wrap; }
.detail-head h1 { font-size: 1.2rem; margin: 0; flex: 1; min-width: 240px; }

.thread { display: flex; flex-direction: column; gap: .75rem; }
.msg {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem .9rem;
  background: var(--surface);
}
.msg.user { border-inline-start: 3px solid var(--accent); }
.msg.staff { border-inline-start: 3px solid var(--resolved); background: var(--surface-2); }
.msg.internal { border-inline-start: 3px solid var(--waiting); background: color-mix(in srgb, var(--waiting) 8%, var(--surface)); }
.msg.system { background: transparent; border: none; padding: .2rem 0; text-align: center; color: var(--text-dim); font-size: .82rem; }
.msg .who { font-weight: 600; font-size: .85rem; margin-bottom: .25rem; display: flex; gap: .5rem; align-items: baseline; }
.msg .when { color: var(--text-dim); font-weight: 400; font-size: .78rem; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }

.reply-box { margin-top: 1rem; }
.reply-box .row { display: flex; gap: .5rem; align-items: center; margin-top: .5rem; flex-wrap: wrap; }
.reply-box label.inline { display: flex; align-items: center; gap: .35rem; color: var(--text-dim); }
.reply-box input[type=checkbox] { width: auto; }
.reply-box input[type=checkbox]:disabled { opacity: .45; }
.reply-box label.inline:has(input:disabled) { opacity: .45; }

/* Shown only when sending would change the status and post no message. Kept
   out of the flow until then, so the row below does not jump as it appears. */
.reply-notice {
  display: none;
  margin-top: .5rem;
  padding: .45rem .6rem;
  border: 1px solid var(--warn-border);
  border-inline-start: 3px solid var(--warn);
  border-radius: 6px;
  background: var(--warn-soft);
  color: var(--warn-text);
  font-size: .82rem;
}
.reply-notice.shown { display: block; }

.side dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: .35rem .75rem; }
.side dt { color: var(--text-dim); font-size: .82rem; }
.side dd { margin: 0; font-size: .87rem; overflow-wrap: anywhere; }
.side .field { margin-bottom: .75rem; }
.side .field > label { display: block; margin-bottom: .25rem; color: var(--text-dim); font-size: .82rem; }

.attachments { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .5rem; }
.attachments a img {
  max-width: 120px; max-height: 120px; border-radius: 6px;
  border: 1px solid var(--border); display: block;
}

/* A non-image attachment has no useful preview, so it is a named row instead
   of a thumbnail: the filename and size are what tell an agent whether to
   open it. */
.file-chip {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .35rem .6rem; border: 1px solid var(--border);
  border-radius: 6px; text-decoration: none; font-size: .85rem;
  max-width: 100%;
}
.file-chip:hover { border-color: var(--accent, #1463b8); }
.file-chip .file-name { overflow-wrap: anywhere; }
.file-size { color: var(--text-dim); font-size: .78rem; white-space: nowrap; }

/* Files staged on the reply box before sending, each removable. */
.staged-files { display: flex; flex-wrap: wrap; gap: .4rem; margin: .5rem 0 0; }
.staged-file {
  display: inline-flex; align-items: center; gap: .4rem;
  padding: .25rem .5rem; border: 1px solid var(--border);
  border-radius: 6px; font-size: .82rem;
}
.staged-file button { padding: 0 .25rem; border: none; line-height: 1; }

.events { font-size: .82rem; color: var(--text-dim); }
.events li { margin-bottom: .3rem; }

.tag-list { display: flex; flex-wrap: wrap; gap: .3rem; }
.tag {
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 4px; padding: .1rem .4rem; font-size: .78rem;
}

/* ── right-to-left ─────────────────────────────────────────────────────── */
/* Nearly everything mirrors from `dir=rtl` plus the logical properties above.
   These are the exceptions: glyphs and numbers that must not flip. */

/* Ticket numbers, versions and timestamps are read left-to-right even in
   Arabic — without this, "#1" renders as "1#" and "iOS 17.2" reverses. */
[dir="rtl"] .bar-n,
[dir="rtl"] .msg .when { direction: ltr; text-align: start; }
/* The number cell also holds the unread dot, so only the number itself is
   forced back to LTR — the dot keeps its mirrored position. */
[dir="rtl"] table.tickets .num { unicode-bidi: isolate; }

/* The pager arrows are literal ‹ › glyphs, which point the wrong way once the
   row itself is mirrored; flipping the row back keeps them pointing along the
   direction of travel. */
[dir="rtl"] .pager { flex-direction: row-reverse; }

/* ── settings / staff ──────────────────────────────────────────────────── */

table.plain { width: 100%; border-collapse: collapse; }
table.plain th, table.plain td { text-align: start; padding: .5rem .75rem; border-bottom: 1px solid var(--border); }
table.plain th { color: var(--text-dim); font-size: .8rem; text-transform: uppercase; }

.form-row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: end; margin-bottom: .75rem; }
.form-row .f { flex: 1; min-width: 150px; }
.form-row .f label { display: block; margin-bottom: .25rem; color: var(--text-dim); font-size: .82rem; }

.toast {
  position: fixed; inset-block-end: 1rem;
  inset-inline-start: 0; inset-inline-end: 0;
  margin-inline: auto; width: fit-content;
  background: var(--text); color: var(--bg);
  padding: .6rem 1rem; border-radius: 6px; box-shadow: var(--shadow);
  z-index: 100; max-width: 90vw;
}
.toast.err { background: var(--rejected); color: #fff; }

.muted { color: var(--text-dim); }
.right { text-align: end; }
.mt { margin-top: 1rem; }
