:root {
  color-scheme: light dark;
  --bg: #f5f4f0;
  --surface: #ffffff;
  --surface-2: #eeede8;
  --text: #1c2220;
  --muted: #59625e;
  --border: #d7d9d4;
  --accent: #2f6f62;
  --accent-strong: #235449;
  --accent-ink: #ffffff;
  --accent-soft: #e0eeea;
  --danger: #b3261e;
  --danger-soft: #fbe8e6;
  --warn-soft: #fff3d1;
  --warn-border: #e2be5a;
  --info-soft: #e4eef8;
  --info-border: #9dbbe0;
  --focus: #1b66d2;
  --brand-word: #0077b3;
  --brand-n: #f4495d;
  --radius: 14px;
  --tabbar-h: 62px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141816;
    --surface: #1e2321;
    --surface-2: #272d2a;
    --text: #edf0ee;
    --muted: #a3aca8;
    --border: #3a423e;
    --accent: #5fb3a1;
    --accent-strong: #7cc7b6;
    --accent-ink: #0d1f1b;
    --accent-soft: #1f3a34;
    --danger: #ff8a80;
    --danger-soft: #3d2120;
    --warn-soft: #3a3118;
    --warn-border: #8a7330;
    --info-soft: #1d2b3b;
    --info-border: #3f5f86;
    --focus: #7fb0ff;
    --brand-word: #fff7ec;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) calc(var(--tabbar-h) + var(--safe-bottom)) env(safe-area-inset-left, 0px);
  -webkit-tap-highlight-color: transparent;
}

h1, h2, p { margin: 0; }
h2 { font-size: 1.05rem; font-weight: 650; }

button, input, select { font: inherit; color: inherit; }
button, label.btn, .tabbar button { touch-action: manipulation; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.brand, .view, .banners {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 16px;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 12px 0 24px;
}

.muted { color: var(--muted); }
.empty { color: var(--muted); padding: 8px 0; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.icon { display: inline-flex; width: 24px; height: 24px; }
.icon svg, .tabbar svg {
  width: 100%; height: 100%;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

/* ---------- LearnNJoy brand bar ---------- */

.brand {
  display: flex; align-items: center; gap: 10px;
  padding-top: 12px;
}
.brand img { width: 34px; height: auto; }
.brand-word {
  font-family: ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  font-size: 1.25rem; font-weight: 700; letter-spacing: -0.01em;
  color: var(--brand-word);
}
.brand-n { color: var(--brand-n); }
.brand-app {
  padding-left: 10px;
  border-left: 1px solid var(--border);
  color: var(--muted); font-weight: 600;
}

/* ---------- buttons and inputs ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px; padding: 0 20px;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer;
  white-space: nowrap;
}
.btn:active { background: var(--surface-2); }
.btn.small { min-height: 44px; padding: 0 14px; font-size: 0.95rem; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:active { background: var(--accent-strong); }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); }
.btn:disabled { opacity: 0.45; cursor: default; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 50%;
  background: transparent; color: var(--muted);
  cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.nav { width: 48px; height: 48px; background: var(--surface); border: 1px solid var(--border); color: var(--text); }

.text-input, .search, .money {
  min-height: 48px; width: 100%;
  padding: 0 14px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface);
  font-size: 1.05rem;
}
.text-input:focus, .search:focus, .money:focus { border-color: var(--accent); outline: 2px solid color-mix(in srgb, var(--accent) 35%, transparent); outline-offset: 0; }
.search { -webkit-appearance: none; appearance: none; }

.money { text-align: right; font-variant-numeric: tabular-nums; font-size: 1.15rem; padding: 0 12px; }
.money.invalid, .money.invalid:focus { border-color: var(--danger); background: var(--danger-soft); outline-color: color-mix(in srgb, var(--danger) 35%, transparent); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
}
.card.stack { display: flex; flex-direction: column; gap: 12px; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* ---------- banners and toast ---------- */

.banners { display: flex; flex-direction: column; gap: 8px; }
.banners:not(:empty) { padding-top: 12px; }

.banner {
  display: flex; align-items: center; gap: 12px;
  padding: 8px 8px 8px 16px;
  border-radius: 12px;
  border: 1px solid var(--info-border);
  background: var(--info-soft);
}
.banner p { flex: 1; padding: 6px 0; }
.banner-warn { background: var(--warn-soft); border-color: var(--warn-border); }
.banner-error { background: var(--danger-soft); border-color: var(--danger); }

.toast {
  position: fixed; left: 50%; z-index: 20;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 84px);
  transform: translate(-50%, 20px);
  display: flex; align-items: center; gap: 16px;
  max-width: calc(100vw - 32px);
  padding: 8px 8px 8px 18px; min-height: 56px;
  border-radius: 14px;
  background: #222927; color: #fff;
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
  opacity: 0; pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast:not(:has(button)) { padding-right: 18px; }
.toast-action {
  min-height: 44px; padding: 0 16px;
  border: 0; border-radius: 10px;
  background: #3b4542; color: #a6e3d4;
  font-weight: 700; cursor: pointer;
}

/* ---------- screen header ---------- */

.screen-head {
  display: flex; align-items: center; gap: 10px;
  min-height: 56px;
}
.title-wrap { position: relative; flex: 1; min-width: 0; text-align: center; }
.title { font-size: 1.35rem; font-weight: 700; line-height: 1.25; }
.screen-head > .title { flex: 1; }

/* The real date input sits invisibly on top of the title so a tap opens the picker. */
.date-input {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  opacity: 0; cursor: pointer;
  -webkit-appearance: none; appearance: none;
}
.today .title { text-decoration: underline dotted color-mix(in srgb, var(--muted) 60%, transparent); text-underline-offset: 5px; }

/* ---------- Today ---------- */

.present-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 7.5rem 7.5rem 44px;
  gap: 10px;
  align-items: center;
}
.present-head { padding-bottom: 6px; }
.col-label { color: var(--muted); font-size: 0.9rem; text-align: right; padding-right: 12px; }

.present-list { list-style: none; margin: 0; padding: 0; }
.present-row { padding: 8px 0; border-top: 1px solid var(--border); }
.present-row .name { font-weight: 600; overflow-wrap: anywhere; }
.tag {
  margin-left: 8px; padding: 1px 8px;
  border-radius: 999px; background: var(--surface-2);
  color: var(--muted); font-size: 0.8rem; font-weight: 500;
}
.remove:active { color: var(--danger); }

.suggestions, .add-student { display: flex; flex-direction: column; gap: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 10px; }
.chip {
  min-height: 48px; padding: 0 18px 0 14px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: 999px;
  background: var(--accent-soft); color: var(--accent-strong);
  font-weight: 600; cursor: pointer;
}
.chip::before { content: "+"; margin-right: 8px; font-weight: 700; }
.chip:active { background: color-mix(in srgb, var(--accent) 25%, var(--surface)); }

.search-results { display: flex; flex-direction: column; gap: 6px; }
.search-results:empty { display: none; }
.result {
  min-height: 48px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); text-align: left;
  font-weight: 600; cursor: pointer;
}
.result:active { background: var(--surface-2); }
.result:disabled { cursor: default; font-weight: 400; color: var(--muted); }
.result.add { color: var(--accent-strong); border-style: dashed; }

.day-total {
  position: sticky; z-index: 5;
  bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px);
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.08);
}
.stat { display: flex; flex-direction: column; }
.stat-label { color: var(--muted); font-size: 0.85rem; }
.stat-value { font-weight: 700; font-size: 1.1rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat.grand .stat-value { color: var(--accent-strong); }

/* ---------- Week ---------- */

.table-wrap { overflow-x: auto; margin: -4px 0; }
.week-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.week-table th, .week-table td { padding: 12px 10px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
.week-table th:first-child, .week-table td:first-child { padding-left: 0; white-space: normal; }
.week-table th:last-child, .week-table td:last-child { padding-right: 0; }
.week-table thead th { color: var(--muted); font-size: 0.9rem; font-weight: 600; }
.week-table tbody th { font-weight: 600; }
.week-table .num { text-align: right; }
.week-table .strong { font-weight: 650; }
.week-table tfoot th, .week-table tfoot td { font-weight: 700; border-bottom: 0; border-top: 2px solid var(--text); }

/* ---------- Students ---------- */

.add-form { display: flex; gap: 10px; }
.warning { padding: 10px 14px; border-radius: 10px; background: var(--warn-soft); border: 1px solid var(--warn-border); }

.student-list { list-style: none; margin: 0; padding: 0; }
.student-row {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 0;
  border-top: 1px solid var(--border);
}
.student-list > .student-row:first-child { border-top: 0; }
.name-btn {
  flex: 1; min-width: 0; min-height: 44px;
  padding: 0; border: 0; background: none;
  text-align: left; font-weight: 600; cursor: text;
  overflow-wrap: anywhere;
}
.rename { flex: 1; min-width: 0; }
.days { font-size: 0.9rem; white-space: nowrap; }

.archived summary {
  min-height: 44px; display: flex; align-items: center;
  font-weight: 650; cursor: pointer; list-style-position: inside;
}
.archived[open] summary { margin-bottom: 4px; }
.archived .student-row:first-child { border-top: 1px solid var(--border); }

/* ---------- Backup ---------- */

.radios { display: flex; flex-direction: column; gap: 4px; }
.radio { display: flex; align-items: center; gap: 12px; min-height: 44px; cursor: pointer; }
.radio input { width: 22px; height: 22px; margin: 0; accent-color: var(--accent); }

.restore-panel {
  display: flex; flex-direction: column; gap: 8px;
  padding: 14px 16px;
  border-radius: 12px;
  background: var(--warn-soft); border: 1px solid var(--warn-border);
}
.restore-panel.error { background: var(--danger-soft); border-color: var(--danger); }

/* ---------- tab bar ---------- */

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding: 0 env(safe-area-inset-right, 0px) var(--safe-bottom) env(safe-area-inset-left, 0px);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tabbar button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: var(--tabbar-h);
  border: 0; background: none;
  color: var(--muted); font-size: 0.8rem; font-weight: 600;
  cursor: pointer;
}
.tabbar svg { width: 26px; height: 26px; }
.tabbar button[aria-current="page"] { color: var(--accent); }

/* ---------- narrow screens (phones, split view) ---------- */

@media (max-width: 560px) {
  .present-grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px; }
  .present-head h2, .present-row .name { grid-column: 1 / -1; }
  .day-total { grid-template-columns: repeat(2, 1fr); }
  .title { font-size: 1.15rem; }
  .screen-head { gap: 6px; }
  .student-row { flex-wrap: wrap; }
}

/* ---------- print (Week screen) ---------- */

@media print {
  @page { margin: 16mm; }
  body { background: #fff; color: #000; padding: 0; font-size: 11pt; }
  .tabbar, .banners, .toast, .no-print, button { display: none !important; }
  .brand { padding: 0 0 10pt; }
  .brand-word { color: #0077b3; }
  .brand-app { color: #555; }
  .view { max-width: none; padding: 0; }
  .screen { padding: 0; gap: 8pt; }
  .screen-head { min-height: 0; }
  .title-wrap { text-align: left; }
  .title { font-size: 16pt; }
  .card { border: 0; padding: 0; background: none; }
  .week-table th, .week-table td, .week-table thead th { padding: 5pt 8pt; border-bottom: 1px solid #999; color: #000; }
  .week-table tfoot th, .week-table tfoot td { border-top: 2px solid #000; border-bottom: 0; }
}
