:root {
  --bg: #0f1420;
  --panel: #161d2e;
  --panel-2: #1c2438;
  --line: #28324a;
  --text: #e6ebf5;
  --muted: #93a0b8;
  --brand: #2563eb;
  --good: #10b981;
  --warn: #f59e0b;
  --bad: #ef4444;
  --radius: 14px;
}

* { box-sizing: border-box; }
/* The hidden attribute must always win over class-based display rules. */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(1200px 600px at 80% -10%, #1a2440, transparent), var(--bg);
  background-color: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.muted { color: var(--muted); }
.brand { font-weight: 800; letter-spacing: -0.02em; font-size: 20px; color: var(--text); text-decoration: none; }
.brand span { color: var(--brand); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; border-bottom: 1px solid var(--line);
  background: var(--panel); position: sticky; top: 0; z-index: 10;
}
.topbar-right { display: flex; align-items: center; gap: 14px; }
.inline { display: inline; margin: 0; }

/* ---------- burger menu (top-right nav) ---------- */
.burger {
  width: 42px; height: 42px; flex: none; padding: 0;
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
}
.burger:hover { border-color: var(--brand); }
.burger span {
  display: block; width: 20px; height: 2px; background: var(--text); border-radius: 2px;
  transition: transform .25s ease, opacity .2s ease;
}
.burger.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.burger.open span:nth-child(2) { opacity: 0; }
.burger.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.menu {
  position: absolute; top: calc(100% + 8px); right: 16px; z-index: 50;
  min-width: 224px; max-width: calc(100vw - 32px); padding: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 18px 44px rgba(0, 0, 0, .5);
  display: flex; flex-direction: column; gap: 2px;
}
.menu[hidden] { display: none; }
.menu-user {
  padding: 8px 12px 10px; font-size: 13px; color: var(--muted);
  border-bottom: 1px solid var(--line); margin-bottom: 4px; word-break: break-word;
}
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 11px 12px; border-radius: 9px; font-size: 14px; font-family: inherit;
  color: var(--text); text-decoration: none; background: transparent; border: 0;
  cursor: pointer; text-align: left;
}
.menu-item[hidden] { display: none; }   /* the hidden attr must beat .menu-item{display:flex} */
.menu-item:hover { background: var(--panel-2); }
.menu-item.active { color: var(--brand); font-weight: 600; }
.menu-item.danger { color: #fca5a5; }
.menu-sep { height: 1px; background: var(--line); margin: 5px 4px; }
.menu form { margin: 0; }
.menu .lang-switch { width: 100%; margin: 2px 0; }
.menu .lang-switch button { flex: 1; padding: 9px 10px; font-size: 12.5px; }
.status { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); display: inline-block; }
.dot.good { background: var(--good); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }

.btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 12px; cursor: pointer; font-size: 13px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--brand); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .6; cursor: default; }

/* ---------- layout ---------- */
main { max-width: 1180px; margin: 0 auto; padding: 24px; display: grid; gap: 20px; }

.banner {
  border-radius: 10px; padding: 10px 14px; font-size: 14px;
  border: 1px solid var(--line);
}
.banner.warn { background: rgba(245, 158, 11, .12); border-color: rgba(245,158,11,.4); }
.banner.error { background: rgba(239, 68, 68, .12); border-color: rgba(239,68,68,.4); }

/* filter bar: each control is a label-above-select group; side by side on
   desktop, stacked full-width on mobile (a long branch name can't overflow). */
.controls { display: flex; flex-wrap: wrap; gap: 12px 16px; margin-bottom: 14px; }
.control { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.control > label { font-size: 12px; color: var(--muted); }
.control select { min-width: 0; max-width: 100%; }
select, input {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; font-size: 14px;
}

/* ---------- KPI cards ---------- */
.card-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 16px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px;
}
.card-label { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.card-value { font-size: 28px; font-weight: 750; margin: 8px 0 4px; letter-spacing: -0.02em; }
.card-sub { font-size: 12px; color: var(--muted); }
.tag {
  font-size: 10px; text-transform: uppercase; letter-spacing: .04em;
  background: var(--panel-2); border: 1px solid var(--line); padding: 2px 6px; border-radius: 999px;
}
.tag.accent { color: #bfdbfe; background: rgba(37, 99, 235, .18); border-color: rgba(37, 99, 235, .5); }

/* ---------- revenue-share card (per-branch %, month-to-date) ---------- */
.share-card {
  background: linear-gradient(180deg, rgba(37, 99, 235, .08), transparent 60%), var(--panel-2);
  border-color: rgba(37, 99, 235, .35);
}
.share-card .card-label { flex-wrap: wrap; }

/* ---------- panels ---------- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.panel-head h2 { font-size: 16px; margin: 0; }
.chart-wrap { position: relative; height: 340px; }

.toggle { display: inline-flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.toggle button { background: transparent; color: var(--muted); border: 0; padding: 7px 14px; cursor: pointer; font-size: 13px; }
.toggle button.active { background: var(--brand); color: white; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
th.sortable:hover { color: var(--brand); }
th.sorted-asc::after { content: " ▲"; color: var(--brand); }
th.sorted-desc::after { content: " ▼"; color: var(--brand); }
tbody tr:hover { background: var(--panel-2); }
tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: 0; }

/* ---------- login ---------- */
.login-body { display: grid; place-items: center; min-height: 100vh; }
.login-card {
  width: 340px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 28px; display: grid; gap: 8px;
}
.login-card h1 { margin: 8px 0 0; font-size: 22px; }
.login-card .muted { margin: 0 0 8px; font-size: 13px; }
.login-card label { font-size: 12px; color: var(--muted); margin-top: 6px; }
.login-card button { margin-top: 14px; background: var(--brand); color: white; border: 0; padding: 11px; border-radius: 10px; font-size: 15px; cursor: pointer; }
.login-card button:hover { background: #1d4ed8; }
.error { color: var(--bad); font-size: 13px; margin-top: 6px; }
.info-msg { color: var(--good); font-size: 13px; margin-top: 6px; }
.login-forgot { color: var(--brand); font-size: 13px; text-decoration: none; margin-top: 8px; display: inline-block; cursor: pointer; }
.login-forgot:hover { text-decoration: underline; }

/* ---------- detail tree ---------- */
.tree-controls { display: inline-flex; gap: 8px; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.small.danger { border-color: rgba(239,68,68,.5); color: #fca5a5; }
.btn.small.danger:hover { border-color: var(--bad); }

.tree-table td, .tree-table th { padding: 9px 12px; }
.tree-table tbody tr { cursor: pointer; transition: background .12s; }
.tree-table tbody tr.lvl-checkout { cursor: default; }
.tcell { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.tcell .tsub { color: var(--muted); font-size: 12px; margin-left: 8px; }

/* caret / leaf marker — a small box makes "expandable" obvious */
.tw {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; flex: none; border-radius: 5px;
  font-size: 10px; color: var(--muted);
  background: var(--panel-2); border: 1px solid var(--line);
}
tr.open > .tcell .tw { color: var(--brand); border-color: var(--brand); }
.tw-leaf { background: transparent; border: 0; color: #3a455f; }

/* hierarchy: a left rail + tint that deepens by level makes nesting evident */
tr.lvl-year > .tcell     { padding-left: 12px; border-left: 3px solid var(--brand); }
tr.lvl-month > .tcell    { padding-left: 30px; border-left: 3px solid rgba(37,99,235,.45); }
tr.lvl-day > .tcell      { padding-left: 50px; border-left: 3px solid rgba(37,99,235,.22); }
tr.lvl-checkout > .tcell { padding-left: 72px; border-left: 3px solid var(--line); }

tr.lvl-year            { background: rgba(37,99,235,.10); }
tr.lvl-year .tlabel    { font-weight: 750; font-size: 15px; }
tr.lvl-month           { background: rgba(37,99,235,.05); }
tr.lvl-month .tlabel   { font-weight: 650; }
tr.lvl-day .tlabel     { font-weight: 500; }
tr.lvl-checkout        { background: rgba(0,0,0,.16); font-size: 13px; }
tr.lvl-checkout .tlabel       { color: var(--text); font-weight: 500; }
tr.lvl-checkout.lvl-checkout-msg .tlabel { color: var(--muted); }

tr.lvl-year:hover, tr.lvl-month:hover, tr.lvl-day:hover { background: var(--panel-2); }

/* ---------- language switch ---------- */
.lang-switch { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.lang-switch button { background: transparent; color: var(--muted); border: 0; padding: 6px 10px; font-size: 11.5px; font-weight: 600; cursor: pointer; font-family: inherit; }
.lang-switch button:hover { color: var(--text); }
.lang-switch button.active { background: var(--brand); color: #fff; }
.lang-switch.page-lang { position: fixed; top: 16px; right: 16px; z-index: 20; background: var(--panel); }

/* ---------- branches page (two columns: form | table) ---------- */
.branches-main {
  max-width: 1180px; margin: 0 auto; padding: 24px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start;
}
.branches-main .panel-head { justify-content: center; }
.branches-main .panel-head h2 { text-align: center; }
.branches-main .table-wrap { width: 100%; }
/* compact, paired form so it fits one screen */
#branch-form { grid-template-columns: 1fr 1fr; gap: 12px 14px; }
#branch-form .full { grid-column: 1 / -1; }
#branch-form .check { align-self: center; }
@media (max-width: 820px) {
  .branches-main { grid-template-columns: 1fr; }
  #branch-form { grid-template-columns: 1fr; }
}

/* ---------- admin page ---------- */
.admin-main {
  max-width: 580px;
  min-height: calc(100vh - 110px);
  display: flex; flex-direction: column; justify-content: safe center; gap: 20px;
}
.admin-main .panel-head { justify-content: center; }
.admin-main .panel-head h2 { text-align: center; }
.admin-main .table-wrap { width: 100%; }
.user-form { display: grid; gap: 12px; }
.user-form .field { display: grid; gap: 4px; }
.user-form .field label { font-size: 12px; color: var(--muted); }
.user-form .check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.btn.primary { background: var(--brand); color: #fff; border: 0; padding: 10px 14px; border-radius: 10px; cursor: pointer; }
.btn.primary:hover { background: #1d4ed8; }
.form-msg { font-size: 13px; }
.form-msg.ok { color: var(--good); }
.form-msg.bad { color: var(--bad); }
/* keep the actions cell a real table-cell (border-collapse needs this for an
   unbroken row divider); the buttons live in an inner flex group. */
#users td.actions, #branches td.actions { vertical-align: middle; }
.action-group { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.user-form .form-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 4px; }
.user-form .hint { font-size: 12px; color: var(--muted); }

/* ---------- branch-access toggles (add-user form + modal) ---------- */
.assign-list { display: flex; flex-direction: column; gap: 8px; }
.assign-list .muted { font-size: 13px; }
.assign-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 11px 14px; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 10px; cursor: pointer; transition: border-color .12s;
}
.assign-item:hover { border-color: var(--brand); }
.assign-item-name { font-size: 14px; color: var(--text); }
.switch { position: relative; flex: none; width: 42px; height: 24px; }
.switch input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.switch .track {
  position: absolute; inset: 0; background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; transition: background .15s, border-color .15s; pointer-events: none;
}
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: var(--muted); transition: transform .15s, background .15s;
}
.switch input:checked + .track { background: var(--brand); border-color: var(--brand); }
.switch input:checked + .track::after { transform: translateX(18px); background: #fff; }

/* ---------- modal dialog ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center;
  justify-content: center; padding: 20px; background: rgba(2, 6, 23, .62);
}
.modal-card {
  width: min(440px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 20px; box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-close {
  background: transparent; border: 0; color: var(--muted); font-size: 22px; line-height: 1;
  cursor: pointer; padding: 2px 8px; border-radius: 8px;
}
.modal-close:hover { color: var(--text); background: var(--panel-2); }
.modal-sub { margin: 4px 0 16px; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
body.modal-open { overflow: hidden; }

/* ---------- responsive: width containment (unconditional, harmless on desktop) ---------- */
/* Grid/flex children default to min-width:auto and refuse to shrink below their
   content, so a wide table stretches the whole page. min-width:0 lets the
   .table-wrap cap at the viewport and own the horizontal scroll itself. */
main { min-width: 0; }
main > section, .panel { min-width: 0; }
.table-wrap { min-width: 0; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.topbar-right { flex-wrap: wrap; }
.topbar-right .inline { display: inline-flex; }
.login-body { padding: 16px; }
.login-card { width: min(340px, 100% - 32px); }

@media (max-width: 1080px) {
  .card-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 860px) {
  .card-grid { grid-template-columns: repeat(2, 1fr); }
}

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

  /* mobile topbar: logo + freshness dot + burger on one row */
  .topbar { padding: 12px 14px; }
  #last-updated { display: none; }   /* keep the status dot; hide the long text */

  /* reclaim horizontal space on phones */
  main { padding: 16px 12px; gap: 14px; }
  .panel { padding: 14px 12px; }
  .card { padding: 14px; }

  /* 16px text controls stop iOS Safari auto-zoom on focus; comfortable tap height */
  select, input, textarea { font-size: 16px; padding: 11px 12px; }
  /* stack the filters and let each select fill the row — no sideways overflow */
  .controls { flex-direction: column; align-items: stretch; gap: 12px; margin-bottom: 16px; }
  .control select { width: 100%; }

  /* data tables: keep columns legible; the wrapper scrolls for any residual width */
  .tree-table, #users { min-width: 560px; }
  .tree-table td, .tree-table th { padding: 8px 7px; }
  #detail { font-size: 12.5px; }
  tr.lvl-year .tlabel { font-size: 13.5px; }
  tr.lvl-checkout { font-size: 12px; }

  /* shallower indentation — the left rail still conveys nesting */
  tr.lvl-year > .tcell     { padding-left: 8px; }
  tr.lvl-month > .tcell    { padding-left: 18px; }
  tr.lvl-day > .tcell      { padding-left: 28px; }
  tr.lvl-checkout > .tcell { padding-left: 38px; }

  /* checkout leaf rows wrap: time · SO on line 1, customer underneath */
  .tree-table .tcell { white-space: normal; }
  .tree-table .tcell .tlabel { overflow-wrap: anywhere; }
  .tree-table tr.lvl-checkout .tcell { flex-wrap: wrap; }
  .tree-table tr.lvl-checkout .tsub { flex-basis: 100%; margin-left: 26px; }
  .tree-table .tw { width: 26px; height: 26px; font-size: 11px; }

  /* admin users actions wrap so Reset/Delete stay visible + tappable */
  #users .action-group { gap: 6px; }
  #users .action-group .btn.small,
  .tree-controls .btn.small { min-height: 40px; padding: 9px 12px; font-size: 13px; }

  /* login card: fluid, with clearance from the fixed top-right lang switch */
  .login-body { align-items: safe center; padding: 56px 16px 24px; }
  .login-card { padding: 22px 18px; }

  /* admin checkbox: bigger tap target */
  .user-form .check { min-height: 40px; }
  .user-form .check input[type="checkbox"] { width: 20px; height: 20px; }

  /* admin: top-align content (vertical centering leaves a big gap on tall phones) */
  .admin-main { justify-content: flex-start; min-height: auto; }
}

@media (max-width: 520px) and (orientation: landscape) {
  /* a tall stacked header shouldn't eat a short landscape viewport */
  .topbar { position: static; }
}
