:root {
  color-scheme: light;
  --bg: #f4f2f8;
  --card: #ffffff;
  --text: #1d1b22;
  --muted: #6b6676;
  --line: #ddd8e6;
  --brand: #4a148c;
  --brand-2: #7e57c2;
  --accent: #1e88e5;
  --ok: #2e7d32;
  --bad: #c62828;
  --warn-bg: #fff4e5;
  --input: #faf9fc;
  --shadow: 0 1px 2px rgba(29, 27, 34, 0.06), 0 4px 14px rgba(29, 27, 34, 0.06);
  /* chart tokens (validated: dataviz palette slots 1-3) */
  --s1: #257de0;
  --s2: #f47230;
  --s3: #00b894;
  --grid: #ece9f1;
  --axis: #c9c4d2;
  --ink2: #6b6676;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    color-scheme: dark;
    --bg: #15121b;
    --card: #201c29;
    --text: #f0edf5;
    --muted: #a39db0;
    --line: #383245;
    --brand: #5e35b1;
    --brand-2: #7e57c2;
    --accent: #64b5f6;
    --ok: #66bb6a;
    --bad: #ef5350;
    --warn-bg: #3a2c16;
    --input: #1a1722;
    --shadow: none;
    --s1: #338ced;
    --s2: #e76624;
    --s3: #00a888;
    --grid: #2f2a3a;
    --axis: #4a4357;
    --ink2: #a39db0;
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #15121b;
  --card: #201c29;
  --text: #f0edf5;
  --muted: #a39db0;
  --line: #383245;
  --brand: #5e35b1;
  --brand-2: #7e57c2;
  --accent: #64b5f6;
  --ok: #66bb6a;
  --bad: #ef5350;
  --warn-bg: #3a2c16;
  --input: #1a1722;
  --shadow: none;
  --s1: #338ced;
  --s2: #e76624;
  --s3: #00a888;
  --grid: #2f2a3a;
  --axis: #4a4357;
  --ink2: #a39db0;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
  line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
[hidden] { display: none !important; }
a { color: var(--accent); }
h2 { margin: 0 0 12px; font-size: 1.1rem; }
h3 { margin: 16px 0 8px; font-size: 1rem; }

/* ---------- header & nav ---------- */
.top {
  position: relative;
  overflow: hidden;
  color: #fff;
  padding: calc(12px + env(safe-area-inset-top, 0px)) 16px 12px;
  background: linear-gradient(120deg, #4a148c, #7e57c2, #1e88e5, #4a148c);
  background-size: 300% 300%;
  animation: flow 14s ease infinite;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.top::before, .top::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  animation: drift 9s ease-in-out infinite;
  pointer-events: none;
}
.top::before { width: 120px; height: 120px; left: -30px; top: -50px; }
.top::after { width: 80px; height: 80px; right: 12%; bottom: -36px; animation-delay: -4s; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; position: relative; }
.brand .logo { font-size: 1.8rem; }
.brand h1 { margin: 0; font-size: 1.25rem; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand .addr { font-size: 0.82rem; opacity: 0.9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who { position: relative; font-size: 0.8rem; text-align: right; opacity: 0.95; }
.who a { color: #fff; font-weight: 600; }
.who span { display: block; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.nav {
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 6px;
  padding: 8px max(12px, calc((100% - 1180px) / 2));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.nav button {
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 9px 4px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--c);
  background: var(--card);
  border: 2px solid var(--c);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: transform 0.15s, background 0.2s, color 0.2s;
}
.nav button:hover { transform: translateY(-1px); }
.nav button.on { background: var(--c); color: #fff; }
@media (max-width: 760px) {
  .nav { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .nav button { font-size: 0.78rem; padding: 8px 2px; }
}

main { max-width: 1180px; margin: 0 auto; padding: 12px; }
section[data-view] { animation: up 0.35s both; }

/* ---------- cards, forms, buttons ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.row-between h2 { margin: 0; }
.grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.span2 { grid-column: 1 / -1; }
label, .lbl { font-weight: 600; font-size: 0.85rem; display: block; }
label > input, label > select, label > textarea { margin-top: 3px; }
input[type='text'], input[type='date'], input[type='number'], input[type='search'], input[type='tel'], input:not([type]), select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--input);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  min-width: 0;
}
textarea { min-height: 140px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.85rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
input[type='checkbox'] { width: 20px; height: 20px; accent-color: var(--accent); vertical-align: middle; }
input[type='color'] { width: 48px; height: 38px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--input); }
.check { display: flex; align-items: center; gap: 8px; font-weight: 500; margin: 8px 0; }

button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  color: #fff;
  background: var(--accent);
  transition: transform 0.15s, filter 0.2s, box-shadow 0.2s;
}
button:hover { filter: brightness(1.08); }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.55; cursor: wait; }
button.ok { background: var(--ok); }
button.bad { background: var(--bad); }
button.brand { background: var(--brand); }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }
button.link { background: none; color: var(--accent); padding: 4px 6px; text-decoration: underline; }
button.sm { padding: 5px 9px; font-size: 0.82rem; }
button.big { padding: 12px 20px; font-size: 1.05rem; }
button.icon { padding: 4px 8px; background: transparent; color: var(--muted); border: 1px solid var(--line); font-size: 0.85rem; }
.btnrow { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; align-items: center; }
.hint { color: var(--muted); font-size: 0.85rem; margin-top: 4px; }
.msg { min-height: 1.2em; margin-top: 6px; font-weight: 600; }
.msg.ok { color: var(--ok); }
.msg.bad { color: var(--bad); }
.muted { color: var(--muted); }
.warn { background: var(--warn-bg); border-radius: 8px; padding: 10px 12px; margin: 8px 0; font-size: 0.9rem; }

/* tabs (book lists) */
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.tab {
  background: transparent;
  color: var(--c);
  border: 2px solid var(--c);
  font-weight: 700;
  position: relative;
}
.tab.on { background: var(--c); color: #fff; animation: pop 0.3s; }
.tab .badge {
  display: inline-block;
  min-width: 20px;
  padding: 0 6px;
  margin-left: 6px;
  border-radius: 10px;
  font-size: 0.75rem;
  line-height: 18px;
  background: var(--c);
  color: #fff;
}
.tab.on .badge { background: #fff; color: var(--c); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: transparent; color: var(--text); border: 1px solid var(--line); padding: 6px 12px; border-radius: 20px; font-weight: 600; font-size: 0.85rem; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* tables */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { border-bottom: 1px solid var(--line); padding: 7px 8px; text-align: right; vertical-align: top; }
th { font-size: 0.8rem; text-transform: none; color: var(--muted); font-weight: 700; background: color-mix(in srgb, var(--brand) 6%, transparent); white-space: nowrap; }
td.l, th.l { text-align: left; }
td.c, th.c { text-align: center; }
td.num, .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
tr.click { cursor: pointer; }
tr.click:hover td { background: color-mix(in srgb, var(--accent) 7%, transparent); }
td.actions { white-space: nowrap; text-align: right; }
td.actions button { margin-left: 4px; }
.pos { color: var(--bad); font-weight: 600; }
.neg { color: var(--ok); font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 18px 8px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 0.75rem; font-weight: 700; color: #fff; }
.tag.bill { background: #1e88e5; }
.tag.payment { background: #2e7d32; }
.tag.expense { background: #ef6c00; }

/* book picker */
table.pick td, table.pick th { text-align: center; }
table.pick td.l { text-align: left; }
table.pick tr.sel td { background: color-mix(in srgb, var(--tc, var(--accent)) 16%, transparent); }
table.pick td.l::before {
  content: '';
  display: inline-block;
  width: 9px;
  height: 14px;
  border-radius: 2px 4px 4px 2px;
  margin-right: 8px;
  vertical-align: -2px;
  background: var(--tc, var(--accent));
}
.qty { display: inline-flex; align-items: center; gap: 4px; }
.qty button { padding: 2px 9px; background: var(--input); color: var(--text); border: 1px solid var(--line); }
.qty input { width: 52px; text-align: center; padding: 5px; }
.anim tr { animation: rowIn 0.35s both; animation-delay: calc(var(--i) * 25ms); }

.acct {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  font-size: 0.92rem;
}
.acct b { font-variant-numeric: tabular-nums; }
.totals { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: baseline; }
.grand { font-size: 1.3rem; font-weight: 700; }
.done { border: 2px solid var(--ok); animation: pop 0.35s; }
.stickybar {
  position: sticky;
  bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  border-radius: 12px;
  background: var(--brand);
  color: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  animation: up 0.3s both;
}
.stickybar b { font-size: 1.1rem; }

/* combobox */
.cwrap { position: relative; margin-top: 3px; }
.combo {
  display: none;
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  z-index: 40;
  max-height: 40vh;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
}
.combo.open { display: block; }
.combo .opt { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; justify-content: space-between; gap: 8px; }
.combo .opt small { color: var(--muted); }
.combo .opt.hl, .combo .opt:hover { background: color-mix(in srgb, var(--accent) 14%, transparent); }

/* multi-select filter */
.filters .fl { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; margin-bottom: 8px; }
.filters .fl > label { flex: 1 1 140px; max-width: 220px; }
.ms { position: relative; }
.ms > summary {
  list-style: none;
  cursor: pointer;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--input);
  font-size: 0.88rem;
  white-space: nowrap;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ms > summary::-webkit-details-marker { display: none; }
.ms > summary::after { content: ' ▾'; color: var(--muted); }
.ms[open] > summary { outline: 2px solid var(--accent); }
.ms > summary b { font-weight: 700; }
.ms.active > summary { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--input)); }
.ms-pop {
  position: absolute;
  z-index: 45;
  top: calc(100% + 4px);
  left: 0;
  min-width: 220px;
  max-width: min(340px, 86vw);
  max-height: 50vh;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.25);
  padding: 6px;
}
.ms-pop label { display: flex; gap: 8px; align-items: center; font-weight: 500; padding: 6px 6px; border-radius: 6px; cursor: pointer; }
.ms-pop label:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.ms-pop .grp { font-size: 0.75rem; font-weight: 700; color: var(--muted); padding: 8px 6px 2px; }
.ms-act { display: flex; gap: 6px; padding: 4px 4px 8px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.ms-act button { flex: 1; }
.dash-student { flex: 1 1 200px; max-width: 260px; }

/* ---------- dashboard ---------- */
.kpis { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 3fr); gap: 12px; margin-bottom: 12px; }
.hero { display: flex; flex-direction: column; justify-content: center; }
.hero .v { font-size: clamp(2.2rem, 5vw, 3.2rem); font-weight: 700; line-height: 1.1; margin: 4px 0; }
.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.tile .v { font-size: 1.45rem; font-weight: 650; margin-top: 2px; }
.tile .k, .hero .k { font-size: 0.85rem; color: var(--muted); font-weight: 600; }
.tile .s, .hero .s { font-size: 0.8rem; color: var(--muted); }
.tile, .hero { margin: 0; }
.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.charts .card { margin: 0; }
.charts .wide { grid-column: 1 / -1; }
.chartbox { position: relative; height: 280px; }
.chartbox.tall { height: auto; min-height: 120px; }
.chart-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); }
details.tbl { margin-top: 8px; }
details.tbl > summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { background: transparent; color: var(--text); border-radius: 0; padding: 5px 10px; font-size: 0.82rem; }
.seg button.on { background: var(--accent); color: #fff; }
.note { font-size: 0.8rem; color: var(--muted); }
@media (max-width: 900px) {
  .kpis { grid-template-columns: 1fr; }
  .charts { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g2 { grid-template-columns: 1fr; }
  .tile .v { font-size: 1.2rem; }
  main { padding: 8px; }
  .card { padding: 12px; }
  .filters .fl > label { max-width: none; }
}

/* ---------- modal, toast ---------- */
.ov {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  animation: fade 0.2s;
}
.box {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  overflow: auto;
  padding: 16px;
  animation: pop 0.25s;
}
.box.wide { max-width: 900px; }
.box h2 { margin-top: 0; }
.box .btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 14px; position: sticky; bottom: -16px; background: var(--card); padding: 10px 0 2px; }
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 90;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  width: min(92vw, 480px);
}
#toast div {
  background: #2b2633;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  font-weight: 600;
  animation: up 0.25s both;
  pointer-events: auto;
}
#toast div.bad { background: #b71c1c; }
#toast div.ok { background: #1b5e20; }
.banner {
  max-width: 1180px;
  margin: 12px auto 0;
  padding: 12px 16px;
  border-radius: 10px;
  background: var(--warn-bg);
  border: 1px solid var(--line);
}
.loading { padding: 40px; text-align: center; color: var(--muted); }
.progress { height: 8px; border-radius: 4px; background: var(--line); overflow: hidden; margin: 10px 0; }
.progress i { display: block; height: 100%; width: 0; background: var(--ok); transition: width 0.2s; }

/* settings */
.list-rows td input { padding: 6px 8px; font-size: 0.92rem; }
.list-rows td input.price { width: 90px; text-align: right; }
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 80; }
.confetti i { position: absolute; top: -14px; width: 8px; height: 14px; border-radius: 2px; animation: fall 1.8s ease-in forwards; }

/* report on screen */
.report h2 { margin-bottom: 2px; }
.report .sum { display: flex; flex-wrap: wrap; gap: 6px 22px; margin: 10px 0; }
.report table { font-size: 0.86rem; }
.report td.wide { text-align: left; min-width: 220px; white-space: normal; }

/* ---------- printing ---------- */
#printArea { display: none; }
@media print {
  @page { margin: 12mm; }
  body { background: #fff; color: #000; padding: 0; }
  body.printing > *:not(#printArea) { display: none !important; }
  body.printing #printArea { display: block; }
  #printArea table { width: 100%; border-collapse: collapse; font-size: 11pt; }
  #printArea th, #printArea td { border: 1px solid #000; padding: 4px 6px; color: #000; background: none; }
  #printArea th { background: #eee !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  #printArea td.wide { text-align: left; }
  .r-head { text-align: center; border-bottom: 2px solid #000; padding-bottom: 6px; margin-bottom: 8px; }
  .r-head h1 { margin: 0; font-size: 18pt; }
  .r-title { text-align: center; font-weight: 700; letter-spacing: 2px; margin: 6px 0 10px; }
  #printArea table.r-meta td, #printArea table.r-sum td { border: none; padding: 2px 0; text-align: left; }
  #printArea table.r-sum { width: auto; margin-left: auto; margin-top: 8px; }
  #printArea table.r-sum td { text-align: right; padding-left: 18px; }
  .r-list td { font-weight: 700; text-align: left !important; background: #f5f5f5 !important; }
  .r-foot { margin-top: 14px; text-align: center; font-size: 10pt; }
  .r-sign { margin-top: 36px; text-align: right; font-size: 10pt; }
  .r-sign span { border-top: 1px solid #000; padding-top: 2px; }
  .report .sum { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 6px 0 10px; }
  .report h3 { margin: 12px 0 4px; }
}

/* ---------- motion ---------- */
@keyframes flow { 0% { background-position: 0 50%; } 50% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
@keyframes drift { 0%, 100% { transform: translate(0, 0); } 50% { transform: translate(30px, 14px); } }
@keyframes up { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
@keyframes rowIn { from { opacity: 0; transform: translateX(-12px); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(0.9); opacity: 0; } 70% { transform: scale(1.03); } 100% { transform: scale(1); opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes fall { to { transform: translate(var(--dx), 105vh) rotate(720deg); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
