:root {
  color-scheme: light dark;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #16202a;
  --muted: #5b6875;
  --border: #d9e0e7;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #e2f3f1;
  --debt: #b42318;
  --debt-bg: #fdecea;
  --danger: #b42318;
  --danger-text: #ffffff;
  --ok: #067647;
  --track: #e5eaef;
  --radius: 14px;
  --tap: 48px;
  --tabbar: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #1a222b;
    --text: #e8edf2;
    --muted: #9aa8b5;
    --border: #2d3844;
    --accent: #2dd4bf;
    --accent-text: #052e2a;
    --accent-soft: #123a37;
    --debt: #ff9b92;
    --debt-bg: #3a1f1d;
    --danger: #ff9b92;
    --danger-text: #3a0d09;
    --ok: #6ee7a8;
    --track: #2d3844;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  /* room for the fixed tab bar on phones */
  padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom));
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 56px;
  padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

h1 { margin: 0; font-size: 1.1rem; white-space: nowrap; }
h2 { margin: 0; font-size: 1.05rem; }

.lang {
  display: flex;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.lang button {
  min-height: 40px;
  min-width: 46px;
  padding: 0 10px;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
}

.lang button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

/* Tabs: a fixed bar at the bottom of a phone, inline in the header on a laptop. */
.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 6;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabs button {
  flex: 1;
  min-height: var(--tabbar);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0 8px;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
}

.tabs button[aria-selected="true"] { color: var(--accent); }

.tabs svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- layout ---------- */

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left));
}

.view > section + section { margin-top: 28px; }

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 12px;
  margin-bottom: 8px;
}

.month-nav { display: flex; align-items: center; gap: 2px; margin-left: auto; }

#month-label {
  min-width: 7.5em;
  text-align: center;
  font-weight: 600;
  text-transform: capitalize;
}

/* ---------- controls ---------- */

button {
  font: inherit;
  min-height: var(--tap);
  padding: 0 18px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled { opacity: 0.6; cursor: progress; }

button.secondary {
  background: transparent;
  color: var(--accent);
  border: 1.5px solid var(--accent);
}

button.danger { background: var(--danger); color: var(--danger-text); }

button.small { min-height: 44px; padding: 0 16px; }

.icon-btn {
  width: 40px;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.7rem;
  line-height: 1;
}

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

label { font-weight: 600; font-size: 0.92rem; margin-top: 6px; }

select, input {
  font: inherit;
  font-size: 16px; /* 16px stops iOS Safari from zooming the page on focus */
  min-height: var(--tap);
  width: 100%;
  padding: 0 12px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
}

.search { margin-bottom: 12px; background: var(--surface); }

#amount, #f-fee { font-variant-numeric: tabular-nums; }

.hint { margin: 0; color: var(--muted); font-size: 0.88rem; }
.form-error { margin: 4px 0 0; color: var(--debt); font-weight: 600; }

/* ---------- lists and cards ---------- */

.summary { margin: 0 0 12px; color: var(--muted); }
.summary strong { color: var(--debt); }

.cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 8px;
  align-content: start;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 12px;
}

.card-name { font-weight: 700; overflow-wrap: anywhere; }

.card-debt {
  flex: none;
  font-weight: 700;
  color: var(--debt);
  background: var(--debt-bg);
  border-radius: 999px;
  padding: 2px 10px;
  font-variant-numeric: tabular-nums;
}

.card-fee { flex: none; font-weight: 600; color: var(--muted); font-variant-numeric: tabular-nums; }

.meta { margin: 0; color: var(--muted); font-size: 0.92rem; overflow-wrap: anywhere; }
.meta a { color: inherit; text-underline-offset: 2px; }

.progress { height: 8px; border-radius: 99px; background: var(--track); overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--ok); border-radius: 99px; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 4px; }
.actions button { padding: 0 8px; font-size: 0.92rem; }

.history { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--border); }

.history li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

.history li:last-child { border-bottom: 0; }
.history .date { color: var(--muted); }

.state {
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 22px 16px;
}

.state p { margin: 0 0 8px; }
.state p:last-child { margin-bottom: 0; }

/* ---------- forms ---------- */

form {
  display: grid;
  gap: 6px;
}

#pay-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

#pay-form button[type="submit"] { margin-top: 10px; }

dialog {
  border: 0;
  border-radius: 16px;
  padding: 18px;
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow: auto;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 12px 40px rgb(0 0 0 / 0.35);
}

dialog::backdrop { background: rgb(0 0 0 / 0.55); }

dialog h2 { margin-bottom: 8px; }
dialog p { margin: 0 0 8px; }

.dialog-actions { display: flex; gap: 10px; margin-top: 12px; }
.dialog-actions button { flex: 1; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar) + 12px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  z-index: 10;
}

/* ---------- tablet ---------- */

@media (min-width: 700px) {
  main { padding-top: 24px; }
}

/* ---------- laptop / desktop ---------- */

@media (min-width: 960px) {
  body { padding-bottom: 0; }

  .topbar { padding-inline: calc((100% - 1000px) / 2 + 16px); } /* align with <main> */
  h1 { margin-right: auto; }

  .tabs {
    position: static;
    border: 0;
    padding: 0;
    gap: 4px;
    background: transparent;
  }

  .tabs button {
    flex: none;
    min-height: 40px;
    flex-direction: row;
    gap: 8px;
    padding: 0 16px;
    border-radius: 999px;
    font-size: 0.95rem;
  }

  .tabs button[aria-selected="true"] { background: var(--accent-soft); }
  .tabs svg { width: 20px; height: 20px; }

  main { max-width: 1000px; }

  #view-debtors {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 360px;
    gap: 32px;
    align-items: start;
  }

  .view > section + section { margin-top: 0; }
  #pay-section { position: sticky; top: 88px; }

  #students { grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }

  .toast { bottom: 24px; }
}
