/* App shell and the shared component vocabulary. */

/* ---- Demo bar: controls for the demo itself, kept visually apart from the product ---- */
.demo-bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3);
  min-height: 44px; padding: 6px var(--space-4);
  background: var(--demo-bg); color: var(--demo-ink); border-bottom: 1px solid var(--line);
  font-size: var(--text-sm);
}
.demo-bar .tag {
  font-weight: 600; font-size: var(--text-xs);
  padding: 2px 8px; border-radius: 999px;
  background: rgb(255 255 255 / 0.12);
}
.demo-bar .who { display: inline-flex; align-items: center; gap: var(--space-2); }
.demo-bar .spacer { flex: 1; }
.demo-bar .link {
  display: inline-flex; align-items: center; gap: 6px;
  background: transparent; color: inherit;
  border: 1px solid rgb(127 127 127 / 0.45); border-radius: var(--radius);
  padding: 4px 10px; cursor: pointer;
  transition: background-color var(--dur-fast) ease, transform var(--dur-press) var(--ease-out);
}
@media (hover: hover) { .demo-bar .link:hover { background: rgb(127 127 127 / 0.18); } }
.demo-bar .chip { background: rgb(127 127 127 / 0.25); color: inherit; }

/* ---- Shell ---- */
.app { display: grid; grid-template-columns: 240px minmax(0, 1fr); min-height: calc(100dvh - 44px); }
.app.no-nav { grid-template-columns: minmax(0, 1fr); }
.app.no-nav .nav { display: none; }

.nav {
  position: sticky; top: 44px; align-self: start; height: calc(100dvh - 44px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
  padding: var(--space-4) var(--space-3);
  background: var(--sidebar); border-inline-end: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 var(--space-2) var(--space-4); }
.brand-mark {
  width: 32px; height: 32px; border-radius: var(--radius); flex: none;
  display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); font-size: 18px;
}
.brand strong { display: block; font-size: var(--text-base); }
.brand small { display: block; color: var(--muted); font-size: var(--text-xs); }

.nav-item {
  display: flex; align-items: center; gap: 10px; width: 100%;
  min-height: 36px; padding: 0 10px;
  border: 0; border-radius: var(--radius); background: transparent;
  color: var(--ink-2); text-align: start; cursor: pointer;
  transition: background-color var(--dur-fast) ease, color var(--dur-fast) ease;
}
.nav-item .ph { font-size: 18px; color: var(--muted); transition: color var(--dur-fast) ease; }
.nav-item .text { flex: 1; min-width: 0; }
@media (hover: hover) {
  .nav-item:hover { background: var(--surface-2); color: var(--ink); }
  .nav-item:hover .ph { color: var(--ink-2); }
}
.nav-item[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-item[aria-current="page"] .ph { color: var(--accent); }
.nav-sep { height: 1px; background: var(--line); margin: var(--space-2) 10px; }
.count {
  min-width: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--bad); color: #fff; font-size: 11px; font-weight: 600; line-height: 18px; text-align: center;
}
:root[data-theme="dark"] .count { color: var(--bg); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .count { color: var(--bg); } }

.nav-foot {
  margin-top: auto; padding: var(--space-3) 10px 0; border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 2px; font-size: var(--text-sm); color: var(--muted);
}
.nav-foot strong { color: var(--ink); font-weight: 600; }
.nav-foot .nav-item { padding: 0; min-height: 32px; color: var(--accent); }
.nav-foot .nav-item .ph { color: currentColor; }

main { min-width: 0; padding: var(--space-6) var(--space-8) var(--space-10); display: flex; flex-direction: column; gap: var(--space-5); max-width: 1440px; }

/* ---- Page layout ---- */
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--space-3) var(--space-4); }
.page-head .lede { color: var(--muted); margin-top: var(--space-1); max-width: 72ch; }
.back { align-self: flex-start; margin-bottom: calc(var(--space-2) * -1); }
.row { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.row.bottom { align-items: flex-end; }
.row.nowrap { flex-wrap: nowrap; }
.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.stack-lg { display: flex; flex-direction: column; gap: var(--space-5); }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-5); }
.grid-main { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: var(--space-5); align-items: start; }

.panel {
  min-width: 0; display: flex; flex-direction: column; gap: var(--space-4);
  padding: var(--space-5);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-2) var(--space-3); }
.panel-head .sub { color: var(--muted); font-size: var(--text-sm); margin-top: 2px; }
.panel > .tablewrap { margin-inline: calc(var(--space-5) * -1); }
.panel > .tablewrap table { min-width: 100%; }
.panel > .tablewrap th:first-child, .panel > .tablewrap td:first-child { padding-inline-start: var(--space-5); }
.panel > .tablewrap th:last-child, .panel > .tablewrap td:last-child { padding-inline-end: var(--space-5); }
.panel > .tablewrap:last-child { margin-bottom: calc(var(--space-5) * -1); }
.panel > .tablewrap:last-child tr:last-child td { border-bottom: 0; }
.section-note { color: var(--muted); font-size: var(--text-sm); }

/* ---- Stats: one strip, not a row of cards ---- */
.stats {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm);
}
.stat { padding: var(--space-4) var(--space-5); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.stat + .stat { border-inline-start: 1px solid var(--line-soft); }
.stat .k { font-size: var(--text-sm); color: var(--muted); }
.stat .v { font-size: var(--text-2xl); font-weight: 600; letter-spacing: -0.02em; line-height: 1.2; overflow-wrap: anywhere; }
.stat .s { font-size: var(--text-xs); color: var(--muted); }
.stat .alt { font-size: var(--text-xs); }
.trend { display: inline-flex; align-items: center; gap: 3px; font-weight: 500; }
.trend.up { color: var(--ok); }
.trend.down { color: var(--bad); }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); color: var(--ink);
  font-weight: 500; white-space: nowrap; cursor: pointer; text-decoration: none;
  box-shadow: var(--shadow-sm);
  transition: background-color var(--dur-fast) ease, border-color var(--dur-fast) ease, color var(--dur-fast) ease, transform var(--dur-press) var(--ease-out);
}
.btn .ph { font-size: 16px; }
@media (hover: hover) { .btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--control); } }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
@media (hover: hover) { .btn-primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); } }
.btn-wa { background: var(--wa); border-color: var(--wa); color: var(--wa-ink); }
@media (hover: hover) { .btn-wa:hover:not(:disabled) { background: var(--wa); border-color: var(--wa); filter: brightness(1.06); } }
.btn-danger { color: var(--bad); }
@media (hover: hover) { .btn-danger:hover:not(:disabled) { background: var(--bad-soft); border-color: var(--bad); } }
.btn-quiet { border-color: transparent; background: transparent; box-shadow: none; }
@media (hover: hover) { .btn-quiet:hover:not(:disabled) { background: var(--surface-2); border-color: transparent; } }
.btn-sm { min-height: 28px; padding: 0 10px; font-size: var(--text-sm); }
.btn-sm .ph { font-size: 14px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; box-shadow: none; }
.btn[aria-busy="true"] { cursor: progress; }
.btn-link {
  border: 0; background: none; padding: 0; color: var(--ink); cursor: pointer; text-align: start; font-weight: 500;
  text-decoration: underline; text-decoration-color: transparent; text-underline-offset: 3px;
  transition: color var(--dur-fast) ease, text-decoration-color var(--dur-fast) ease;
}
@media (hover: hover) { .btn-link:hover { color: var(--accent); text-decoration-color: currentColor; } }
.icon-btn {
  display: inline-grid; place-items: center; width: 32px; height: 32px;
  border: 0; border-radius: var(--radius); background: transparent; color: var(--muted); cursor: pointer;
  transition: background-color var(--dur-fast) ease, color var(--dur-fast) ease, transform var(--dur-press) var(--ease-out);
}
.icon-btn .ph { font-size: 18px; }
@media (hover: hover) { .icon-btn:hover { background: var(--surface-2); color: var(--ink); } }

/* Press feedback: the interface heard you */
@media (prefers-reduced-motion: no-preference) {
  .btn:active:not(:disabled), .icon-btn:active, .demo-bar .link:active, .opt:active, .fav:active, .slot-free:active { transform: scale(0.97); }
}

/* ---- Chips: status text on a tint, no decorative dots ---- */
.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 1px 8px; border-radius: 999px;
  font-size: var(--text-xs); font-weight: 500; line-height: 18px; white-space: nowrap;
}
.chip .ph { font-size: 12px; }
.c-info { background: var(--info-soft); color: var(--info); }
.c-ok { background: var(--ok-soft); color: var(--ok); }
.c-warn { background: var(--warn-soft); color: var(--warn); }
.c-bad { background: var(--bad-soft); color: var(--bad); }
.c-acc { background: var(--accent-soft); color: var(--accent); }
.c-mute { background: var(--surface-2); color: var(--muted); box-shadow: inset 0 0 0 1px var(--line); }

/* ---- Tables ---- */
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  padding: 10px 12px; text-align: start; white-space: nowrap;
  font-size: var(--text-xs); font-weight: 500; color: var(--muted);
  border-bottom: 1px solid var(--line);
}
td { padding: 11px 12px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr { transition: background-color var(--dur-fast) ease; }
tr.click { cursor: pointer; }
@media (hover: hover) { tr.click:hover td { background: var(--surface-2); } }
td.r, th.r { text-align: end; }
td.c, th.c { text-align: center; }
.cell-sub { display: block; font-size: var(--text-sm); color: var(--muted); }
td.empty { padding: var(--space-8) var(--space-5); text-align: center; color: var(--muted); }

/* ---- Forms ---- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label, .field .flabel { font-size: var(--text-sm); font-weight: 500; color: var(--ink-2); }
.field .hint { font-size: var(--text-xs); color: var(--muted); }
input[type="text"], input[type="tel"], input[type="email"], input[type="password"], input[type="number"], input[type="date"], input[type="search"], select, textarea {
  width: 100%; min-width: 0; min-height: 36px; padding: 6px 10px;
  background: var(--surface); border: 1px solid var(--control); border-radius: var(--radius);
  transition: border-color var(--dur-fast) ease, box-shadow var(--dur-fast) ease;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
[aria-invalid="true"] { border-color: var(--bad) !important; }
[aria-invalid="true"]:focus-visible { box-shadow: 0 0 0 3px var(--bad-soft) !important; }
select { appearance: none; padding-inline-end: 32px; cursor: pointer;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 55%, calc(100% - 11px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }
[dir="rtl"] select { background-position: 16px 55%, 11px 55%; }
textarea { resize: vertical; min-height: 72px; line-height: 1.45; }
.search { position: relative; max-width: 420px; width: 100%; }
.search .ph { position: absolute; inset-inline-start: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { padding-inline-start: 34px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-4); }
.check { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 2px 0 0; flex: none; }
.pw-row { display: flex; gap: 6px; }
.pw-row input { flex: 1; }

/* ---- Banners: tinted, with an icon; never a thick colored edge ---- */
.banner {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; border-radius: var(--radius);
  font-size: var(--text-sm);
}
.banner > .ph { font-size: 18px; margin-top: 1px; }
.banner .body { color: var(--ink); flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.banner strong { font-weight: 600; }
.banner-bad { background: var(--bad-soft); color: var(--bad); }
.banner-warn { background: var(--warn-soft); color: var(--warn); }
.banner-info { background: var(--info-soft); color: var(--info); }
.banner-ok { background: var(--ok-soft); color: var(--ok); }

/* ---- Tabs ---- */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
.tabs button {
  position: relative; border: 0; background: transparent; cursor: pointer; white-space: nowrap;
  padding: 10px 12px; color: var(--muted); font-weight: 500;
  transition: color var(--dur-fast) ease;
}
.tabs button::after {
  content: ""; position: absolute; inset-inline: 8px; bottom: -1px; height: 2px; border-radius: 2px;
  background: var(--accent); transform: scaleX(0); transition: transform var(--dur) var(--ease-out);
}
@media (hover: hover) { .tabs button:hover { color: var(--ink); } }
.tabs button[aria-selected="true"] { color: var(--accent); }
.tabs button[aria-selected="true"]::after { transform: scaleX(1); }

/* ---- Lists with actions ---- */
.list { display: flex; flex-direction: column; }
.list > .item { display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); padding: var(--space-3) 0; }
.list > .item + .item { border-top: 1px solid var(--line-soft); }
.list > .item:first-child { padding-top: 0; }
.list > .item:last-child { padding-bottom: 0; }
.item .main { min-width: 0; display: flex; flex-direction: column; gap: 2px; }

.notif {
  border: 0; background: none; text-align: start; cursor: pointer; border-radius: var(--radius);
  padding-inline: var(--space-2); margin-inline: calc(var(--space-2) * -1); width: calc(100% + var(--space-4));
  transition: background-color var(--dur-fast) ease;
}
@media (hover: hover) { .notif:hover { background: var(--surface-2); } }
.notif.unread .main > span:first-child { font-weight: 600; }
.list > .notif.item:first-child { padding-top: var(--space-3); }
.list > .notif.item:last-child { padding-bottom: var(--space-3); }

.kv { display: grid; grid-template-columns: minmax(120px, 170px) 1fr; gap: var(--space-2) var(--space-4); margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }

/* ---- Empty states teach the next step ---- */
.empty-state { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); padding: var(--space-8) var(--space-4); text-align: center; color: var(--muted); }
.empty-state .ph { font-size: 28px; color: var(--control); }
.empty-state p { max-width: 44ch; }

/* ---- Switch ---- */
.toggle { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-4); }
.switch { position: relative; width: 40px; height: 24px; flex: none; }
.switch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--control); pointer-events: none; transition: background-color var(--dur) ease; }
.switch span::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.2); transition: transform var(--dur) var(--ease-out);
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Radio cards ---- */
.radio-cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
.radio-card {
  display: flex; gap: 10px; align-items: flex-start; padding: var(--space-3) var(--space-4);
  border: 1px solid var(--line); border-radius: var(--radius-lg); background: var(--surface); cursor: pointer;
  transition: border-color var(--dur-fast) ease, background-color var(--dur-fast) ease;
}
.radio-card input { margin-top: 3px; }
.radio-card b { display: block; }
.radio-card:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

/* ---- Meter ---- */
.meter { height: 6px; border-radius: 999px; background: var(--line-soft); overflow: hidden; }
.meter span { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.meter.hi span { background: var(--warn); }

/* ---- Dialog ---- */
.modal {
  width: min(560px, calc(100% - 32px)); max-height: calc(100dvh - 48px);
  padding: 0; border: 0; border-radius: var(--radius-lg);
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow-lg);
  overflow: hidden;
  opacity: 1; transform: scale(1);
  transition: opacity var(--dur-fast) var(--ease-out), transform var(--dur-fast) var(--ease-out), overlay var(--dur-fast) allow-discrete, display var(--dur-fast) allow-discrete;
}
.modal[open] { display: flex; flex-direction: column; transition-duration: var(--dur); }
@starting-style { .modal[open] { opacity: 0; transform: scale(0.96); } }
.modal:not([open]) { opacity: 0; transform: scale(0.98); }
.modal::backdrop { background: var(--scrim); opacity: 1; transition: opacity var(--dur) ease, overlay var(--dur) allow-discrete, display var(--dur) allow-discrete; }
@starting-style { .modal[open]::backdrop { opacity: 0; } }
.modal:not([open])::backdrop { opacity: 0; }
.modal header { display: flex; justify-content: space-between; align-items: flex-start; gap: var(--space-3); padding: var(--space-5) var(--space-5) 0; }
.modal header h2 { font-size: var(--text-lg); padding-top: 2px; }
.modal .mbody { padding: var(--space-4) var(--space-5) var(--space-5); display: flex; flex-direction: column; gap: var(--space-4); overflow-y: auto; }
.modal footer { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: var(--space-2); padding: var(--space-3) var(--space-5); background: var(--surface-2); border-top: 1px solid var(--line); }
.modal footer .btn:first-child:not(:only-child) { margin-inline-end: auto; }
.modal .merror:empty { display: none; }

/* ---- Toast (top layer, so it sits above an open dialog) ---- */
.toast {
  position: fixed; inset: auto auto calc(20px + env(safe-area-inset-bottom, 0px)) 50%; margin: 0;
  width: max-content; max-width: min(520px, calc(100vw - 32px)); padding: 10px 16px;
  display: flex; align-items: center; gap: 10px;
  border: 0; border-radius: var(--radius); background: var(--ink); color: var(--bg);
  font-size: var(--text-sm); box-shadow: var(--shadow-lg);
  opacity: 1; transform: translate(-50%, 0);
  transition: opacity var(--dur-fast) ease, transform var(--dur-fast) var(--ease-out), overlay var(--dur-fast) allow-discrete, display var(--dur-fast) allow-discrete;
}
.toast:popover-open { transition-duration: var(--dur); }
@starting-style { .toast:popover-open { opacity: 0; transform: translate(-50%, 8px); } }
.toast:not(:popover-open) { display: none; opacity: 0; transform: translate(-50%, 4px); }
.toast .ph { color: var(--accent-soft); font-size: 18px; }

@media (max-width: 1100px) {
  .grid-main { grid-template-columns: minmax(0, 1fr); }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat:nth-child(3) { border-inline-start: 0; }
  .stat:nth-child(n + 3) { border-top: 1px solid var(--line-soft); }
  main { padding: var(--space-6) var(--space-6) var(--space-10); }
}

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .nav {
    position: sticky; top: 44px; z-index: 10; height: auto; flex-direction: row; overflow-x: auto;
    padding: var(--space-2) var(--space-4); gap: 2px; border-inline-end: 0; border-bottom: 1px solid var(--line);
    scrollbar-width: none;
  }
  .nav-item { width: auto; white-space: nowrap; flex: none; }
  .nav .brand, .nav-foot, .nav-sep { display: none; }
  main { padding: var(--space-4) var(--space-4) var(--space-10); gap: var(--space-4); }
  .panel { padding: var(--space-4); }
  .panel > .tablewrap { margin-inline: calc(var(--space-4) * -1); }
  .panel > .tablewrap th:first-child, .panel > .tablewrap td:first-child { padding-inline-start: var(--space-4); }
  .panel > .tablewrap th:last-child, .panel > .tablewrap td:last-child { padding-inline-end: var(--space-4); }
  .panel > .tablewrap:last-child { margin-bottom: calc(var(--space-4) * -1); }
  .grid-2, .form-grid, .radio-cards { grid-template-columns: minmax(0, 1fr); }
  .kv { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .kv dd { margin-bottom: var(--space-2); }
  .stat { padding: var(--space-3) var(--space-4); }
  .stat .v { font-size: var(--text-xl); }
  .demo-bar { position: static; }
  .nav { top: 0; }
  .demo-bar .hide-sm { display: none; }
}

@media (max-width: 560px) {
  .stats { grid-template-columns: minmax(0, 1fr); }
  .stat + .stat { border-inline-start: 0; border-top: 1px solid var(--line-soft); }
}
