:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --ink: #1a2236;
  --ink-2: #55607a;
  --ink-3: #8b94ab;
  --line: #e6eaf2;
  --accent: #3b5bdb;
  --accent-strong: #2b46b8;
  --accent-soft: #e8edfc;
  --sidebar: #141b2e;
  --sidebar-ink: #9aa5c3;
  --good-bg: #ddf3e6; --good-ink: #196b3f;
  --info-bg: #e2edfd; --info-ink: #1c56a8;
  --bad-bg: #fbe4e4; --bad-ink: #a13030;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 28, 55, .05), 0 4px 16px rgba(20, 28, 55, .06);
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg); color: var(--ink); font-size: 14.5px;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--ink-3); }
.error { color: #b23434; min-height: 1.2em; margin-top: 10px; font-size: 13px; }
svg { width: 18px; height: 18px; flex: none; }

/* ---------- Login ---------- */
#login-view {
  display: flex; align-items: center; justify-content: center; min-height: 100vh;
  background: linear-gradient(150deg, #141b2e 0%, #1e2c54 55%, #2b46b8 130%);
  padding: 20px;
}
.login-card {
  background: var(--surface); padding: 40px 36px 32px; border-radius: 18px;
  box-shadow: 0 24px 60px rgba(8, 12, 28, .45);
  width: 360px; text-align: center;
}
.login-logo {
  width: 52px; height: 52px; margin: 0 auto 14px; border-radius: 14px;
  background: var(--accent-soft); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
}
.login-logo svg { width: 28px; height: 28px; }
.login-card h1 { font-size: 22px; letter-spacing: -.2px; }
.login-card > p { margin: 4px 0 24px; font-size: 13.5px; }
.login-card input {
  width: 100%; padding: 12px 14px; margin-bottom: 12px;
  border: 1px solid var(--line); border-radius: 10px; font-size: 15px;
  background: #fafbfd;
}
.login-card input:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Shell ---------- */
#app-view { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; flex: none; background: var(--sidebar); color: var(--sidebar-ink);
  display: flex; flex-direction: column; padding: 18px 12px;
  position: sticky; top: 0; height: 100vh;
}
.side-brand {
  display: flex; align-items: center; gap: 10px; color: #fff;
  font-weight: 700; font-size: 16.5px; letter-spacing: -.2px;
  padding: 6px 10px 22px;
}
.side-brand svg { width: 24px; height: 24px; color: #7d96f4; }
.sidebar nav { display: flex; flex-direction: column; gap: 3px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  background: none; border: none; color: var(--sidebar-ink);
  padding: 10px 12px; border-radius: 9px; cursor: pointer;
  font-size: 14px; font-weight: 500; text-align: left; width: 100%;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: rgba(255,255,255,.06); color: #dfe5f5; }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item.logout { margin-top: 12px; color: #8790a8; }
.nav-item.logout:hover { color: #f0b9b9; background: rgba(255,120,120,.08); }

.content { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  padding: 22px 32px 0;
}
.topbar h1 { font-size: 21px; letter-spacing: -.3px; }
main { padding: 20px 32px 48px; max-width: 1060px; width: 100%; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border-radius: var(--radius); padding: 22px;
  box-shadow: var(--shadow); margin-bottom: 18px;
}
.card.narrow { max-width: 560px; }
.card-title { font-size: 15.5px; margin-bottom: 16px; letter-spacing: -.2px; }
.card-sub { margin: -8px 0 18px; font-size: 13px; line-height: 1.55; }
.table-card { padding: 6px 0 2px; overflow-x: auto; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; align-items: end; }
.form-stack { display: flex; flex-direction: column; gap: 16px; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
input, select {
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px;
  font-size: 14px; font-weight: 400; background: #fafbfd; color: var(--ink); width: 100%;
}
input:focus, select:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
.inline { display: flex; gap: 8px; align-items: center; }
.inline input, .inline select { flex: 1; width: auto; min-width: 0; }
.form-actions { display: flex; align-items: end; }

/* Toggle switch */
.switch-row { flex-direction: row; align-items: center; justify-content: space-between; font-size: 14px; color: var(--ink); }
.switch { position: relative; display: inline-block; width: 44px; height: 25px; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; z-index: 1; }
.switch i {
  position: absolute; inset: 0; background: #cdd4e3; border-radius: 20px; transition: background .18s;
}
.switch i::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 19px; height: 19px;
  background: #fff; border-radius: 50%; transition: transform .18s;
  box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(19px); }

/* ---------- Buttons ---------- */
.btn {
  padding: 10px 18px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 9px; cursor: pointer; font-size: 14px; font-weight: 600; color: var(--ink);
  transition: background .15s, border-color .15s;
}
.btn:hover { background: #f2f4fa; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.small { padding: 6px 12px; font-size: 12.5px; }
.btn.danger { color: #a13030; border-color: #ecc8c8; }
.btn.danger:hover { background: #fbf0f0; }
.btn.full { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }

/* ---------- Tables ---------- */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 11px 18px; text-align: left; white-space: nowrap; }
.data-table th {
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--ink-3); font-weight: 600; border-bottom: 1px solid var(--line);
}
.data-table td { border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: #f8f9fd; }
.data-table .code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-weight: 600; letter-spacing: .5px; }
.data-table .link-edit { color: var(--accent); font-weight: 600; text-decoration: none; }
.data-table .link-edit:hover { text-decoration: underline; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.unused { background: var(--info-bg); color: var(--info-ink); }
.badge.active { background: var(--good-bg); color: var(--good-ink); }
.badge.expired { background: var(--bad-bg); color: var(--bad-ink); }

.row-between { display: flex; align-items: center; justify-content: space-between; margin: 26px 0 14px; flex-wrap: wrap; gap: 10px; }

.code-block {
  background: #141b2e; color: #c8d3f0; border-radius: 9px; padding: 14px 16px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px;
  line-height: 1.5; overflow-x: auto; white-space: pre-wrap; word-break: break-all;
  margin-bottom: 12px;
}
.switch-row small { font-weight: 400; font-size: 12px; }

/* ---------- Stat tiles ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; }
.stat {
  background: var(--surface); border-radius: var(--radius); padding: 18px 20px;
  box-shadow: var(--shadow);
}
.stat .label { font-size: 12.5px; color: var(--ink-3); font-weight: 600; }
.stat .value { font-size: 27px; font-weight: 650; letter-spacing: -.4px; margin-top: 6px; }
.stat.accent .value { color: var(--accent); }
.stat .value.good { color: var(--good-ink); }
.stat .value.bad { color: var(--bad-ink); }
.stat .value.dim { color: var(--ink-3); }

/* Revenue per-day proportional bar behind the amount */
.rev-bar { position: relative; font-variant-numeric: tabular-nums; }
.rev-bar::before {
  content: ""; position: absolute; left: 0; top: 4px; bottom: 4px;
  width: var(--pct, 0%); background: var(--accent-soft); border-radius: 4px; z-index: 0;
}
.rev-bar span { position: relative; z-index: 1; }

/* ---------- Clickable stat tiles ---------- */
.stat.clickable { cursor: pointer; transition: box-shadow .15s, transform .05s; position: relative; }
.stat.clickable:hover { box-shadow: 0 4px 18px rgba(20, 28, 55, .12); }
.stat.clickable:active { transform: translateY(1px); }
.stat.clickable::after {
  content: "›"; position: absolute; top: 14px; right: 16px;
  color: var(--ink-3); font-size: 18px; line-height: 1;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0; z-index: 200; padding: 20px;
  background: rgba(20, 28, 55, .5); display: flex; align-items: center; justify-content: center;
}
.modal-box {
  background: var(--surface); border-radius: 14px; width: 100%; max-width: 860px;
  max-height: 86vh; display: flex; flex-direction: column;
  box-shadow: 0 24px 70px rgba(8, 12, 28, .45);
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 20px; border-bottom: 1px solid var(--line);
}
.modal-head h3 { font-size: 16px; }
.modal-close {
  background: none; border: none; font-size: 24px; line-height: 1; cursor: pointer;
  color: var(--ink-3); padding: 0 4px;
}
.modal-close:hover { color: var(--ink); }
.modal-body { overflow: auto; padding: 4px 0 8px; }
.vm-group td {
  font-weight: 700; font-size: 12px; letter-spacing: .3px;
  padding: 10px 18px 6px; background: #f8f9fd;
}
.vm-group.on td { color: var(--good-ink); }
.vm-group.off td { color: var(--ink-3); }
.modal-sub { padding: 10px 20px 0; }

@media (max-width: 760px) {
  .modal { padding: 0; align-items: stretch; }
  .modal-box { max-width: 100%; max-height: 100%; border-radius: 0; }
}

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: #1a2236; color: #fff; padding: 12px 22px; border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.3); z-index: 100; font-size: 14px;
}
#toast.err { background: #9c2f2f; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  html, body { overflow-x: hidden; }
  #app-view { flex-direction: column; }

  /* App-style fixed bottom tab bar */
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    width: 100%; height: auto; z-index: 50;
    flex-direction: row; padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    box-shadow: 0 -4px 18px rgba(8, 12, 28, .3);
  }
  .side-brand { display: none; }
  .sidebar nav { flex-direction: row; flex: 5; gap: 2px; }  /* 5 nav items + 1 logout = 6 equal slots */
  .nav-item {
    flex: 1; flex-direction: column; gap: 3px;
    padding: 7px 2px; border-radius: 9px;
    font-size: 10.5px; font-weight: 600; text-align: center; justify-content: center;
    min-width: 0;
  }
  .nav-item svg { width: 21px; height: 21px; }
  .nav-item.logout { flex: 1; margin: 0; }

  /* Top bar shows a compact brand + the page title */
  .topbar { padding: 16px 16px 0; }
  .topbar h1 { font-size: 19px; }
  main { padding: 16px 16px 92px; }  /* bottom space clears the tab bar */

  .card { padding: 18px 16px; }
  .card.narrow { max-width: 100%; }
  .stat-grid { gap: 12px; }
  .stat .value { font-size: 24px; }
  .row-between { margin-top: 18px; }
  .code-block { font-size: 11.5px; }

  /* Tables stay one row, scrolling horizontally inside their card */
  .table-card { -webkit-overflow-scrolling: touch; }
}

@media (max-width: 380px) {
  .nav-item { font-size: 0; gap: 0; }   /* very narrow: icons only */
  .nav-item svg { width: 22px; height: 22px; }
}

/* ---------- Print tickets ---------- */
#print-area { display: none; }
@media print {
  @page { size: A4 portrait; margin: 8mm; }
  html, body { background: #fff !important; }
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block; }
  .print-title { font-size: 10pt; font-weight: 700; margin: 0 0 3mm; }
  .ticket-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 2.5mm; }
  .ticket {
    border: 1px dashed #999; border-radius: 2mm; padding: 2mm 1.5mm;
    text-align: center; page-break-inside: avoid; break-inside: avoid;
    font-size: 7.5pt; line-height: 1.2;
  }
  .ticket .t-sn { font-size: 5.5pt; color: #999; text-align: right; margin-bottom: .3mm; }
  .ticket .t-brand { font-weight: 700; font-size: 8pt; color: #111; }
  .ticket .t-plan { color: #333; font-size: 7pt; margin: .3mm 0; }
  .ticket .t-code {
    font-family: ui-monospace, Consolas, monospace; font-size: 11pt;
    font-weight: 700; letter-spacing: .5px; margin: 1mm 0;
    border: 1px solid #333; border-radius: 1.5mm; padding: .8mm .3mm;
    word-break: break-all;
  }
  .ticket .t-meta { color: #444; font-size: 6pt; }
}
