:root {
  color-scheme: light;
  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-2: #eef3f1;
  --ink: #1d2725;
  --muted: #64716d;
  --line: #d7e0dd;
  --primary: #0d766f;
  --primary-2: #0f5f85;
  --warning: #b7791f;
  --danger: #b42318;
  --critical: #7f1d1d;
  --ok: #16803c;
  --shadow: 0 16px 35px rgba(28, 45, 42, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.08s ease, filter 0.08s ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button:active {
  transform: scale(0.94);
  filter: brightness(0.92);
}

.app-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  background: #182321;
  color: #f8fffd;
  padding: 24px 18px;
}

.sidebar.single-purpose .nav {
  display: none;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 28px;
}

.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border-radius: 8px;
  background: #22a699;
  color: #06211e;
  font-size: 1.4rem;
  font-weight: 800;
}

.brand span,
.eyebrow,
.clock span {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
}

.sidebar .brand span {
  color: #afc5c0;
}

.nav {
  display: grid;
  gap: 8px;
}

.nav-item {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: 8px;
  background: transparent;
  color: #dbe8e5;
  text-align: left;
}

.nav-item.active,
.nav-item:hover {
  background: #2a3a37;
  color: #ffffff;
}

.nav-item[hidden] {
  display: none;
}

.main {
  min-width: 0;
  padding: 26px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 18px;
}

.session-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: flex-end;
  margin-left: auto;
  color: var(--muted);
  font-size: 0.84rem;
}

.session-bar strong {
  color: var(--ink);
}

.version-text {
  display: inline-block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
}

.compact-btn {
  min-height: 34px;
  padding: 7px 10px;
  font-size: 0.78rem;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 1.85rem;
}

h2 {
  font-size: 1rem;
}

.clock {
  min-width: 160px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  text-align: right;
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.toolbar,
.metrics,
.split-layout {
  display: grid;
  gap: 14px;
  margin-bottom: 14px;
}

.toolbar {
  grid-template-columns: repeat(2, minmax(180px, 260px)) auto;
  align-items: end;
}

.report-toolbar {
  grid-template-columns: repeat(2, minmax(180px, 240px)) auto;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

input,
select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  padding: 8px 10px;
}

.primary,
.secondary,
.danger {
  min-height: 42px;
  padding: 9px 12px;
  border-radius: 8px;
  font-weight: 800;
}

.primary {
  background: var(--primary);
  color: white;
}

.secondary {
  background: var(--surface-2);
  color: var(--ink);
}

.danger {
  background: #fee4e2;
  color: var(--danger);
}

.metrics {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.metric,
.panel,
.patient-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

body[data-route="tv"] .app-shell,
body[data-route="patient"] .app-shell,
body.auth-only .app-shell {
  grid-template-columns: 1fr;
}

body[data-route="tv"] .sidebar,
body[data-route="patient"] .sidebar,
body.auth-only .sidebar {
  display: none;
}

body.auth-only .main {
  display: grid;
  align-content: start;
  max-width: 780px;
  width: 100%;
  margin: 0 auto;
}

body.auth-only .view.active {
  display: none;
}

body[data-route="tv"] .topbar {
  display: none;
}

body[data-route="patient"] .topbar {
  display: none;
}

/* ── TV AUDIO PROMPT ────────────────────────────────────────── */
.tv-audio-prompt {
  display: none;
}

body[data-route="tv"] .tv-audio-prompt {
  display: flex !important;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: rgba(5, 19, 26, 0.92);
  align-items: center;
  justify-content: center;
}

body[data-route="tv"] .tv-audio-prompt[hidden] {
  display: none !important;
}

.tv-audio-prompt button {
  font-size: clamp(1.2rem, 3vw, 2rem);
  font-weight: 700;
  padding: 1.5vh 4vw;
  background: #5de0c8;
  color: #05131a;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  letter-spacing: 0.05em;
  box-shadow: 0 0 40px rgba(93, 224, 200, 0.4);
}

/* ── TV ROUTE OVERRIDE ────────────────────────────────────── */
body[data-route="tv"] .main {
  padding: 0;
  background: #05131a;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

body[data-route="tv"] #tvView {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  height: 100%;
}

body[data-route="tv"] .tv-header {
  display: flex !important;
}

body[data-route="tv"] .tv-main {
  display: flex !important;
  flex: 1;
  background: #05131a !important;
  align-items: center;
  justify-content: center;
}

body[data-route="tv"] .tv-footer {
  display: flex !important;
}

body[data-route="tv"] .tv-display {
  background: transparent !important;
}

body[data-route="tv"] .tv-display strong {
  color: #ffffff !important;
}

body[data-route="tv"] .tv-display span {
  color: #5de0c8 !important;
}

body[data-route="tv"] .tv-label {
  color: #5de0c8 !important;
}

body[data-route="tv"] .split-layout,
body[data-route="tv"] .public-login-link {
  display: none !important;
}

body[data-route="patient"] .main {
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
}

.metric {
  padding: 14px;
}

.metric span {
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 1.6rem;
}

.split-layout {
  grid-template-columns: minmax(0, 1.35fr) minmax(340px, 0.65fr);
  align-items: start;
}

.panel {
  overflow: hidden;
}

.login-panel,
.lookup-panel {
  margin-bottom: 14px;
}

.login-panel[hidden] {
  display: none;
}

.lookup-panel[hidden] {
  display: none;
}

.public-login-link {
  margin-top: 18px;
  padding-bottom: 8px;
  text-align: center;
}

.public-login-link a {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-decoration: none;
}

.public-login-link a:hover {
  color: var(--teal);
  text-decoration: underline;
}

body[data-route="tv"] .public-login-link {
  margin-top: 10px;
  opacity: 0.7;
}

.login-grid {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(160px, 0.7fr) auto auto;
  gap: 12px;
  align-items: end;
  padding: 14px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  min-height: 54px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.pill,
.tag {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  padding: 4px 10px;
  font-size: 0.78rem;
  font-weight: 800;
}

.queue-list,
.recent-list,
.setup-list,
.report-table,
.choice-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.report-table {
  overflow-x: auto;
}

.queue-row,
.recent-row,
.setup-row,
.report-row {
  display: grid;
  gap: 10px;
  align-items: center;
  min-height: 56px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfc;
}

.queue-row {
  grid-template-columns: 92px minmax(0, 1fr);
}

.queue-row .actions {
  grid-column: 1 / -1;
  justify-content: flex-start;
}

.queue-number {
  font-size: 1.25rem;
  font-weight: 900;
}

.queue-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 0.84rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.transfer-control {
  min-width: 150px;
  flex: 1 1 170px;
  color: var(--muted);
  font-size: 0.74rem;
}

.transfer-control select {
  min-height: 42px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 14px;
}

.full {
  grid-column: 1 / -1;
}

.checkbox-line {
  align-items: center;
  display: flex;
  flex-direction: row;
  gap: 10px;
}

.checkbox-line input {
  width: auto;
}

.status-waiting {
  border-left: 5px solid var(--primary-2);
}

.status-called {
  border-left: 5px solid var(--warning);
}

.status-serving {
  border-left: 5px solid var(--ok);
}

.status-skipped {
  border-left: 5px solid var(--danger);
}

.age-green {
  color: var(--ok);
}

.age-yellow {
  color: var(--warning);
}

.age-red {
  color: var(--danger);
}

.age-critical {
  color: var(--critical);
}

.patient-card {
  display: grid;
  gap: 14px;
  max-width: 620px;
  padding: 18px;
}

.patient-pass {
  border-top: 1px solid var(--line);
}

.pass-head {
  border-bottom: 0;
}

.pass-body {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  gap: 14px;
  align-items: center;
  padding: 0 14px 14px;
}

.pass-body img {
  width: 160px;
  height: 160px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.pass-body strong {
  display: block;
  font-size: 2rem;
}

.pass-body span {
  display: block;
  margin: 8px 0;
  color: var(--muted);
  overflow-wrap: anywhere;
  font-size: 0.82rem;
}

.patient-number {
  font-size: clamp(4rem, 18vw, 7rem);
  line-height: 1;
  font-weight: 950;
}

.patient-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.patient-field {
  min-height: 68px;
  padding: 10px;
  border-radius: 8px;
  background: var(--surface-2);
}

.patient-field.featured {
  grid-column: span 3;
  min-height: 72px;
}

.patient-field span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  margin-bottom: 6px;
}

.patient-field strong {
  font-size: 1.08rem;
}

/* ── TV LAYOUT ELEMENTS (hidden by default, shown via route CSS) ─ */

.tv-header {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0 3vw;
  height: 7vh;
  background: #071e28;
  border-bottom: 2px solid #0e4a5c;
  flex-shrink: 0;
}

.tv-facility {
  font-size: clamp(0.9rem, 2.2vw, 1.4rem);
  font-weight: 900;
  letter-spacing: 0.18em;
  color: #5de0c8;
  text-transform: uppercase;
}

.tv-clock {
  font-size: clamp(1rem, 2.5vw, 1.6rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #a0ccd6;
  letter-spacing: 0.08em;
}

.tv-main {
  display: none;
  align-items: center;
  justify-content: center;
  background: #05131a;
  overflow: hidden;
}

.tv-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.4vh;
  width: 100%;
  height: 100%;
  text-align: center;
}

.tv-label {
  font-size: clamp(0.75rem, 1.8vw, 1.2rem);
  font-weight: 900;
  letter-spacing: 0.3em;
  color: #5de0c8;
  margin: 0;
  opacity: 0.85;
}

.tv-display strong {
  font-size: clamp(7rem, 20vw, 16rem);
  font-weight: 900;
  line-height: 0.9;
  color: #ffffff;
  letter-spacing: -0.02em;
  text-shadow: 0 0 60px rgba(93, 224, 200, 0.2);
}

.tv-display span {
  font-size: clamp(1.4rem, 3.5vw, 3rem);
  font-weight: 700;
  color: #5de0c8;
  letter-spacing: 0.04em;
  margin-top: 0.5vh;
}

.tv-footer {
  display: none;
  flex-direction: column;
  background: #071e28;
  border-top: 2px solid #0e4a5c;
  padding: 1.2vh 2.5vw 1.5vh;
  gap: 1vh;
  height: 28vh;
  flex-shrink: 0;
  overflow: hidden;
}

.tv-footer-label {
  font-size: clamp(0.6rem, 1.4vw, 0.9rem);
  font-weight: 900;
  letter-spacing: 0.25em;
  color: #4a8fa0;
  margin: 0;
  flex-shrink: 0;
}

.tv-recent-list {
  display: flex;
  gap: 1.2vw;
  align-items: stretch;
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.tv-recent-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  background: #0a2530;
  border: 1px solid #0e4a5c;
  border-radius: 8px;
  padding: 0.8vh 1vw;
  min-width: 0;
  gap: 0.3vh;
}

.tv-recent-card:first-child {
  background: #0d3040;
  border-color: #1a7090;
}

.tv-recent-card .rc-number {
  font-size: clamp(1.4rem, 3.5vw, 2.8rem);
  font-weight: 900;
  color: #c8eef5;
  line-height: 1;
}

.tv-recent-card:first-child .rc-number {
  color: #5de0c8;
}

.tv-recent-card .rc-room {
  font-size: clamp(0.55rem, 1.2vw, 0.85rem);
  font-weight: 600;
  color: #4a8fa0;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tv-recent-card .rc-time {
  font-size: clamp(0.5rem, 1vw, 0.7rem);
  color: #2d6070;
  font-weight: 600;
}

/* ── TV FLASH ANIMATION ─────────────────────────────────────── */

@keyframes tv-flash {
  0%   { background: #05131a; }
  15%  { background: #0d6a52; }
  35%  { background: #0a4a3a; }
  100% { background: #05131a; }
}

@keyframes tv-number-pop {
  0%   { transform: scale(1); }
  25%  { transform: scale(1.07); }
  50%  { transform: scale(0.97); }
  75%  { transform: scale(1.02); }
  100% { transform: scale(1); }
}

.tv-main.tv-new-call {
  animation: tv-flash 2s ease-in-out;
}

.tv-main.tv-new-call .tv-display strong {
  animation: tv-number-pop 0.7s ease-out;
}

/* old tv-display rules removed — superseded by .tv-screen layout above */

.recent-row {
  grid-template-columns: 90px minmax(0, 1fr) 110px;
}

.clinic-message {
  padding: 18px;
  color: var(--muted);
  line-height: 1.6;
}

.setup-row {
  grid-template-columns: minmax(0, 1fr) auto;
}

.station-admin-row {
  grid-template-columns: minmax(0, 1fr) auto minmax(260px, auto);
}

.staff-admin-row {
  grid-template-columns: minmax(0, 1fr) auto auto;
}

.report-row {
  grid-template-columns: 92px 1fr 1fr 1fr;
}

.detailed-report-row {
  grid-template-columns: 82px 130px minmax(110px, 1fr) minmax(120px, 1fr) 120px 105px 135px minmax(170px, 1.2fr) 110px;
  align-items: start;
}

.report-header {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
}

.audit-row {
  align-items: start;
}

.choice-list {
  padding: 0 0 14px;
}

.choice-item {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  min-height: 52px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  text-align: left;
}

.choice-item span {
  color: var(--muted);
}

.needs-login .toolbar,
.needs-login .metrics,
.needs-login .split-layout,
.needs-login .report-table {
  display: none !important;
}

.empty {
  padding: 24px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 960px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 10;
  }

  .nav {
    grid-template-columns: repeat(5, minmax(120px, 1fr));
    overflow-x: auto;
  }

  .split-layout,
  .login-grid,
  .patient-grid {
    grid-template-columns: 1fr;
  }

  .toolbar {
    grid-template-columns: 1fr 1fr;
  }

  .toolbar .primary {
    grid-column: 1 / -1;
  }

  .metrics {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .queue-row {
    grid-template-columns: 76px minmax(0, 1fr);
  }

  .queue-row .actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }

  body[data-route="patient"] .patient-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body[data-route="patient"] .patient-field.featured {
    grid-column: span 2;
  }
}

@media (max-width: 620px) {
  .main,
  .sidebar {
    padding: 16px;
  }

  .topbar {
    display: grid;
  }

  .session-bar {
    justify-content: flex-start;
    margin-left: 0;
  }

  .clock {
    text-align: left;
  }

  .toolbar,
  .metrics,
  .form-grid {
    grid-template-columns: 1fr;
  }

  .queue-row,
  .recent-row,
  .report-row,
  .station-admin-row,
  .staff-admin-row,
  .detailed-report-row,
  .pass-body {
    grid-template-columns: 1fr;
  }

  .actions {
    justify-content: stretch;
  }

  .actions button {
    flex: 1 1 120px;
  }

  body[data-route="patient"] .main {
    padding: 14px;
  }

  body[data-route="patient"] .patient-card {
    padding: 16px;
  }

  body[data-route="patient"] .patient-number {
    font-size: clamp(4.4rem, 26vw, 6.5rem);
  }
}

/* ── TOAST NOTIFICATION ─────────────────────────────────────── */
#toastContainer {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9998;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}

.toast {
  background: #1d2725;
  color: #f8fffd;
  padding: 12px 22px;
  border-radius: 8px;
  font-size: 0.92rem;
  font-weight: 600;
  box-shadow: 0 4px 24px rgba(0,0,0,0.22);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.22s ease, transform 0.22s ease;
  max-width: 480px;
  text-align: center;
}

.toast.toast-show {
  opacity: 1;
  transform: translateY(0);
}

/* ── DEMOGRAPHICS REPORT ────────────────────────────────────── */
.demo-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.demo-card {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 16px;
}

.demo-card-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 12px;
}

@media (max-width: 760px) {
  .demo-grid {
    grid-template-columns: 1fr;
  }
}
