:root {
  --bg: #f3f5f7;
  --surface: #ffffff;
  --ink: #15202b;
  --muted: #5f6f7d;
  --line: #d8e0e8;
  --line-strong: #c3ced9;
  --sidebar: #132536;
  --sidebar-ink: #d7e2ec;
  --sidebar-active: #1e3a52;
  --accent: #0f6b5c;
  --accent-hover: #0c574b;
  --accent-ink: #ffffff;
  --warn-bg: #fff6e5;
  --warn-ink: #7a4d00;
  --error: #9b1c1c;
  --error-bg: #fdecea;
  --ok-bg: #e8f5e9;
  --ok-ink: #1b5e20;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(21, 32, 43, 0.06);
  --sidebar-w: 220px;
  --header-h: 56px;
  --font: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, Consolas, "Courier New", monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  line-height: 1.45;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0 0 0.5rem; line-height: 1.25; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.05rem; }
p { margin: 0 0 0.75rem; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 0.8rem; }
.error { color: var(--error); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); border: 0;
}

/* ----- Auth (login) ----- */
.auth-body {
  background:
    linear-gradient(160deg, #10233a 0%, #16324d 42%, #f3f5f7 42%, #f3f5f7 100%);
  min-height: 100vh;
}
.auth-main {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.login-shell {
  width: 100%;
  max-width: 420px;
}
.login-brand {
  color: #fff;
  margin-bottom: 1.25rem;
}
.login-brand .brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  margin-bottom: 0.75rem;
}
.login-brand h1 {
  margin: 0;
  font-size: 1.55rem;
  color: #fff;
}
.login-brand p {
  margin: 0.35rem 0 0;
  color: rgba(255,255,255,0.82);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1.35rem 1.4rem 1.5rem;
}
.login-card h2 {
  margin-bottom: 0.25rem;
}
.form-error-box {
  background: var(--error-bg);
  color: var(--error);
  border: 1px solid #f1c0c0;
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
  margin: 0.75rem 0;
  font-size: 0.92rem;
}

/* ----- App shell ----- */
.app-body { min-height: 100vh; }
.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  z-index: 40;
  border-right: 1px solid #0d1b28;
}
.sidebar a { color: inherit; text-decoration: none; }
.sidebar-brand {
  padding: 1rem 1rem 0.85rem;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.sidebar-brand a {
  display: flex;
  gap: 0.7rem;
  align-items: center;
}
.sidebar-brand a:hover { text-decoration: none; }
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}
.brand-text {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}
.brand-text strong {
  font-size: 0.92rem;
  color: #fff;
}
.brand-text small {
  color: rgba(215,226,236,0.7);
  font-size: 0.72rem;
}
.sidebar-nav {
  display: flex;
  flex-direction: column;
  padding: 0.75rem 0.55rem;
  gap: 0.15rem;
  overflow-y: auto;
}
.sidebar-nav a {
  display: block;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  color: var(--sidebar-ink);
  font-size: 0.92rem;
}
.sidebar-nav a:hover {
  background: rgba(255,255,255,0.06);
  text-decoration: none;
}
.sidebar-nav a.is-active {
  background: var(--sidebar-active);
  color: #fff;
  font-weight: 600;
}
.nav-section {
  margin: 0.85rem 0.7rem 0.35rem;
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: rgba(215,226,236,0.55);
}

.shell {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.top-header {
  position: sticky;
  top: 0;
  z-index: 20;
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 1.25rem;
  background: rgba(255,255,255,0.96);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(4px);
}
.header-title { flex: 1; min-width: 0; }
.header-title h1 { margin: 0; font-size: 1.05rem; }
.header-user {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.user-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.2;
}
.user-meta strong { font-size: 0.88rem; }
.user-meta span { font-size: 0.75rem; color: var(--muted); }
.menu-btn {
  display: none;
  cursor: pointer;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 0.35rem 0.65rem;
  font-size: 0.85rem;
}
.nav-toggle {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}
.nav-backdrop { display: none; }

.content {
  padding: 1.15rem 1.25rem 2.5rem;
  max-width: 1180px;
  width: 100%;
}

/* ----- Forms / buttons ----- */
label {
  display: block;
  margin: 0.7rem 0;
  font-size: 0.9rem;
  color: var(--ink);
}
input[type="text"],
input[type="password"],
input[type="search"],
input[type="email"],
select,
textarea {
  font: inherit;
  width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
}
textarea { min-height: 110px; resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid rgba(15, 107, 92, 0.25);
  border-color: var(--accent);
}
.btn,
button,
a.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  padding: 0.5rem 0.9rem;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
  text-decoration: none !important;
}
.btn:hover,
button:hover,
a.button:hover { background: var(--accent-hover); }
.btn-ghost,
button.btn-ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line-strong);
}
.btn-ghost:hover,
button.btn-ghost:hover {
  background: #eef3f7;
  color: var(--ink);
}
.btn-secondary {
  background: #203447;
  color: #fff;
}
.inline { display: inline; margin: 0; }
.inline button { width: auto; }
.checkbox {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.checkbox input { width: auto; margin: 0; }

/* ----- Cards / KPI / tables ----- */
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.page-head h1 { margin-bottom: 0.25rem; }
.data-banner {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: var(--warn-bg);
  color: var(--warn-ink);
  border: 1px solid #efd7a8;
  border-radius: 8px;
  padding: 0.45rem 0.7rem;
  font-size: 0.88rem;
  font-weight: 600;
  margin: 0.35rem 0 0.85rem;
}
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.85rem;
  margin: 0 0 1.25rem;
}
.kpi, .card, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.kpi {
  padding: 0.95rem 1rem;
}
.kpi h2 {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.kpi-value {
  margin: 0.4rem 0 0.2rem;
  font-size: 1.45rem;
  font-weight: 700;
}
.kpi .muted { margin: 0; font-size: 0.82rem; }
.card, .panel {
  padding: 1rem 1.05rem;
  margin: 0 0 1rem;
}
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}
.grid-2 {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}
.table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
table {
  width: 100%;
  border-collapse: collapse;
  background: transparent;
  border: none;
  border-radius: 0;
}
th, td {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--line);
  text-align: left;
  font-size: 0.9rem;
  vertical-align: top;
}
th {
  background: #f7f9fb;
  color: #3d4d5c;
  font-weight: 600;
  white-space: nowrap;
}
tr:last-child td { border-bottom: none; }
.empty-state {
  padding: 1.25rem;
  color: var(--muted);
  text-align: center;
}
.pill {
  display: inline-block;
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 650;
  background: #e8eef2;
  color: #334455;
  white-space: nowrap;
}
.status-open { background: #e3f2fd; color: #0d47a1; }
.status-in_progress { background: #fff8e1; color: #8a5a00; }
.status-resolved { background: #e8f5e9; color: #1b5e20; }
.status-closed { background: #eceff1; color: #455a64; }
.priority-low { background: #f5f5f5; color: #616161; }
.priority-normal { background: #e3f2fd; color: #1565c0; }
.priority-high { background: #ffe0b2; color: #e65100; }
.priority-urgent { background: #ffcdd2; color: #b71c1c; }
.badge {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--warn-bg);
  color: var(--warn-ink);
  font-size: 0.75rem;
  font-weight: 600;
}
.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0 0 1rem;
  align-items: flex-end;
  padding: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.filter-bar label {
  margin: 0;
  min-width: 120px;
  flex: 1 1 140px;
}
.filter-bar select,
.filter-bar input {
  width: 100%;
  margin-top: 0.25rem;
}
.filter-bar .filter-actions {
  display: flex;
  gap: 0.45rem;
  align-items: center;
  margin-top: 1.15rem;
}
.filter-bar button { width: auto; margin: 0; }
.trend-list {
  list-style: none;
  margin: 0;
  padding: 0;
}
.trend-list li {
  display: grid;
  grid-template-columns: 110px 1fr auto;
  gap: 0.65rem;
  align-items: center;
  padding: 0.45rem 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.9rem;
}
.trend-list li:last-child { border-bottom: none; }
.trend-bar {
  height: 8px;
  background: #e8eef2;
  border-radius: 999px;
  overflow: hidden;
}
.trend-bar > span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
}
.meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.75rem;
}
.meta-item {
  background: #f7f9fb;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.65rem 0.75rem;
}
.meta-item .label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.2rem;
}
.ticket-layout {
  display: grid;
  grid-template-columns: 1.5fr 1fr;
  gap: 1rem;
}
.comment {
  background: #fafbfc;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.75rem 0.9rem;
  margin-bottom: 0.65rem;
}
.comment header {
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.comment.internal {
  border-left: 3px solid #8a5a00;
  background: #fffaf0;
}
.ticket-form label { margin-bottom: 0.85rem; }
.ticket-form button { width: auto; margin-top: 0.5rem; }
.search-results { list-style: none; padding: 0; margin: 0.5rem 0 0; }
.search-results li {
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: baseline;
}
button.linkish, .btn-tiny {
  width: auto;
  margin: 0;
  padding: 0.25rem 0.55rem;
  background: #203447;
  font-size: 0.8rem;
}
.messages { list-style: none; padding: 0; margin: 0 0 1rem; }
.message {
  background: var(--ok-bg);
  color: var(--ok-ink);
  border: 1px solid #a5d6a7;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  margin-bottom: 0.5rem;
}
.stack-gap > * + * { margin-top: 1rem; }
.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}
.rep-switch { min-width: 220px; }
.rep-switch label { margin: 0; }

/* ----- Responsive ----- */
@media (max-width: 980px) {
  .grid-2, .ticket-layout {
    grid-template-columns: 1fr;
  }
}
@media (max-width: 860px) {
  .menu-btn { display: inline-flex; }
  .sidebar {
    transform: translateX(-105%);
    transition: transform 0.18s ease;
  }
  .nav-toggle:checked ~ .sidebar {
    transform: translateX(0);
  }
  .nav-toggle:checked ~ .nav-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 24, 34, 0.45);
    z-index: 35;
  }
  .shell { margin-left: 0; }
  .content { padding: 1rem 0.85rem 2rem; }
  .user-meta { display: none; }
  .kpi-value { font-size: 1.25rem; }
  .trend-list li {
    grid-template-columns: 1fr;
    gap: 0.25rem;
  }
}
@media (max-width: 520px) {
  .top-header { padding: 0.5rem 0.75rem; }
  .filter-bar label { min-width: 100%; }
}

/* Ticket list: sortable headers, pagination, error messages (M1) */
.message-error {
  background: var(--error-bg);
  color: var(--error);
  border-color: #f3b7b2;
}
th a {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
th a:hover { color: var(--accent); }
th.is-sorted { background: #eef3f7; }
th.is-sorted a { color: var(--accent); }
.sort-indicator { font-size: 0.7rem; }
.pagination {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.75rem;
  font-size: 0.9rem;
}

/* Ticket timeline (M1) */
.timeline {
  list-style: none;
  margin: 0;
  padding: 0 0 0 1.1rem;
  border-left: 2px solid var(--line);
}
.timeline-item {
  position: relative;
  padding: 0 0 0.9rem 0.9rem;
}
.timeline-item:last-child { padding-bottom: 0; }
.timeline-mark {
  position: absolute;
  left: -1.55rem;
  top: 0.3rem;
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: var(--line-strong);
  border: 2px solid var(--surface);
}
.timeline-item.event-created .timeline-mark,
.timeline-item.event-resolved .timeline-mark { background: var(--accent); }
.timeline-item.event-closed .timeline-mark { background: #203447; }
.timeline-item.event-reopened .timeline-mark { background: var(--error); }
.timeline-summary { margin: 0; font-weight: 600; font-size: 0.92rem; }
.timeline-meta { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.82rem; }

/* Inline notice inside a panel (e.g. ticket pre-filled from an inquiry) */
.note-box {
  background: var(--warn-bg);
  color: var(--warn-ink);
  border: 1px solid #f0d8a8;
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  margin: 0 0 1rem;
  font-size: 0.9rem;
}
.note-box a { color: inherit; }

/* Dashboard "Needs attention" row (M6.6) */
.attention { border-left: 4px solid var(--accent); }
.attention .actions-row { gap: 0.75rem; }
.attention-item {
  display: inline-flex;
  align-items: baseline;
  gap: 0.35rem;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f7f9fb;
  color: var(--ink);
  text-decoration: none;
  font-size: 0.9rem;
}
.attention-item:hover { border-color: var(--accent); color: var(--accent); }
.attention-item strong { font-size: 1.15rem; }

/* Global search box in the portal header (M6.2) */
.header-search { margin: 0 0.75rem; flex: 1 1 260px; max-width: 360px; }
.header-search input {
  width: 100%;
  padding: 0.4rem 0.65rem;
  font-size: 0.9rem;
}
@media (max-width: 720px) {
  .header-search { display: none; }
}

/* Clickable table rows (Phase 1)
   The link itself stays in the first cell — the row only signals that it opens.
   No nested anchors, so keyboard and screen-reader behaviour is unchanged. */
.table-wrap tbody tr.is-linked:hover {
  background: #eef3f7;
}
.table-wrap tbody tr.is-linked:hover td:first-child a {
  color: var(--accent);
  text-decoration: underline;
}
.table-wrap tbody tr.is-linked:focus-within {
  background: #eef3f7;
}

/* Manager queue state and ticket conversation (Phase 2) */
.waiting-manager { background: #ffe0b2; color: #e65100; }
.waiting-rep { background: #e8eef2; color: #445566; }

/* Conversation thread. The team's side is tinted so a manager can see whose
   turn it is at a glance; the markup is the same for both. */
.comment.from-team {
  border-left: 3px solid var(--accent);
  background: #f4f9fc;
}
.comment.opening {
  border-left: 3px solid var(--line);
}
.comment-author { font-weight: 650; color: var(--ink); }
.ticket-context {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 1rem;
  margin: 0.4rem 0 0;
  font-size: 0.88rem;
  color: var(--muted);
}
.ticket-context strong { color: var(--ink); font-weight: 650; }
/* Numeric table cells read better right-aligned. */
td.num, th.num { text-align: right; }
td.nowrap, th.nowrap { white-space: nowrap; }

/* Overview section headings (Phase 2) — they group the page without adding
   another panel border around every block. */
.section-label {
  margin: 1.5rem 0 0.6rem;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.section-label:first-of-type { margin-top: 0.5rem; }

/* Admin account actions and the one-time generated password (Phase 3) */
.account-action {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--line);
}
.account-action:last-child { border-bottom: none; padding-bottom: 0; }
.account-action p { margin: 0.2rem 0 0; font-size: 0.86rem; }
.account-action strong { font-size: 0.95rem; }
.generated-password {
  border-left: 3px solid var(--accent);
}
.generated-password-value {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: #f4f9fc;
  border: 1px dashed var(--line);
  border-radius: 6px;
  padding: 0.6rem 0.8rem;
  margin: 0.4rem 0;
  user-select: all;
  word-break: break-all;
}

/* Narrow screens: let a wide table scroll instead of collapsing (Phase 4)
   .table-wrap already scrolls, but `table { width: 100% }` meant the table
   could never be wider than the viewport, so a nine-column ticket list
   squashed every cell to a few characters rather than scrolling. A minimum
   width restores the intended behaviour: the table scrolls inside its own
   container and the page itself never overflows sideways.

   Scoped to the full-width data tables, which are direct children of
   .content. The small tables inside .panel (age buckets, team snapshot,
   recent orders, search results) stay fluid and need no scrolling. */
@media (max-width: 860px) {
  .content > .table-wrap table { min-width: 700px; }
}

/* Admin account actions stack rather than squeezing the button (Phase 4) */
@media (max-width: 520px) {
  .account-action {
    flex-direction: column;
    align-items: stretch;
  }
  .account-action form,
  .account-action button { width: 100%; }
  /* A stacked filter bar does not need the label-height offset. */
  .filter-bar .filter-actions { margin-top: 0.5rem; }
}

/* Long values (emails, clinic names) wrap inside a cell instead of forcing
   the table wider than its own minimum. */
td, th { overflow-wrap: anywhere; }
td.nowrap, th.nowrap, .pill { overflow-wrap: normal; }
