:root {
  --bg: #f5f4ef;
  --card: #ffffff;
  --ink: #1d2320;
  --muted: #66706b;
  --line: #e2e0d8;
  --brand: #1f5f4a;
  --brand-soft: #e3efe9;
  --in: #1d7a46;
  --out: #b3412f;
  --half: #b07d12;
  --leave: #3b62a8;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; }
button, input, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.loading { padding: 2rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.in { color: var(--in); }
.out { color: var(--out); }
.hidden { display: none !important; }

/* layout */
header.top {
  position: sticky; top: 0; z-index: 10;
  background: var(--brand); color: #fff;
  padding: 0.6rem 1rem calc(0.6rem);
  padding-top: calc(0.6rem + env(safe-area-inset-top));
  display: flex; align-items: center; gap: 1rem;
}
header.top h1 { font-size: 1.05rem; font-weight: 650; }
nav.tabs {
  display: flex; gap: 0.25rem; background: var(--card); border-bottom: 1px solid var(--line);
  padding: 0 0.5rem; overflow-x: auto; position: sticky; top: 0; z-index: 9;
}
nav.tabs a {
  padding: 0.75rem 0.7rem; text-decoration: none; color: var(--muted); font-weight: 550;
  border-bottom: 3px solid transparent; white-space: nowrap;
}
nav.tabs a.active { color: var(--brand); border-bottom-color: var(--brand); }
main { max-width: 960px; margin: 0 auto; padding: 1rem 1rem 6rem; }
.stack > * + * { margin-top: 1rem; }
.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; }
.card.flush { padding: 0; overflow: hidden; }

/* controls */
.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  padding: 0.55rem 0.9rem; cursor: pointer; font-weight: 550; min-height: 44px;
}
.btn:hover { border-color: #c9c6bb; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.in { background: var(--in); border-color: var(--in); color: #fff; }
.btn.out { background: var(--out); border-color: var(--out); color: #fff; }
.btn.danger { color: var(--out); }
.btn.small { min-height: 34px; padding: 0.3rem 0.65rem; font-size: 0.85rem; }
.btn:disabled { opacity: 0.5; cursor: default; }
.icon-btn { min-width: 44px; padding: 0.55rem; }
input[type=text], input[type=password], input[type=number], input[type=date], input[type=month], input[type=search], select {
  border: 1px solid var(--line); border-radius: 10px; padding: 0.55rem 0.7rem; background: #fff; min-height: 44px; width: 100%;
}
label.field { display: block; }
label.field > span { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 0.3rem; }
.form > * + * { margin-top: 0.8rem; }
.grow { flex: 1; min-width: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: var(--card); padding: 0.5rem 0.9rem; cursor: pointer; min-height: 40px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--brand-soft); color: var(--brand); font-weight: 650; }
.error { color: var(--out); font-size: 0.9rem; }

/* summary tiles */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.5rem; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.75rem; }
.tile .label { font-size: 0.8rem; color: var(--muted); }
.tile .value { font-size: 1.15rem; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 0.15rem; }
@media (max-width: 600px) { .tiles { grid-template-columns: repeat(2, 1fr); } }

/* lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 0.75rem 1rem; border-top: 1px solid var(--line); display: flex; gap: 0.75rem; align-items: center; }
.list li:first-child { border-top: 0; }
.list .main { flex: 1; min-width: 0; }
.list .title { font-weight: 550; }
.list .sub { font-size: 0.82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.empty { padding: 1.5rem; text-align: center; color: var(--muted); }

/* attendance */
.marks { display: inline-flex; gap: 0.25rem; }
.marks button {
  width: 42px; height: 42px; border-radius: 10px; border: 1px solid var(--line); background: var(--card);
  font-weight: 650; cursor: pointer; color: var(--muted);
}
.marks button.on.P { background: var(--in); border-color: var(--in); color: #fff; }
.marks button.on.A { background: var(--out); border-color: var(--out); color: #fff; }
.marks button.on.H { background: var(--half); border-color: var(--half); color: #fff; }
.marks button.on.L { background: var(--leave); border-color: var(--leave); color: #fff; }
.list li.changed { background: #fbf8ea; }
.savebar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--card); border-top: 1px solid var(--line);
  padding: 0.6rem 1rem calc(0.6rem + env(safe-area-inset-bottom)); display: flex; gap: 0.5rem; align-items: center; justify-content: flex-end;
}
.legend { display: flex; gap: 0.75rem; flex-wrap: wrap; font-size: 0.82rem; color: var(--muted); }
.legend b.P { color: var(--in); } .legend b.A { color: var(--out); } .legend b.H { color: var(--half); } .legend b.L { color: var(--leave); }

/* tables */
.table-wrap { overflow-x: auto; }
.card.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 0.9rem; }
th, td { padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--line); text-align: left; }
th { font-size: 0.78rem; color: var(--muted); font-weight: 600; background: #faf9f5; }
table.grid { font-size: 0.8rem; }
table.grid th, table.grid td { padding: 0.35rem 0.3rem; text-align: center; min-width: 26px; }
table.grid .name { text-align: left; position: sticky; left: 0; background: var(--card); min-width: 130px; z-index: 1; }
table.grid th.name { background: #faf9f5; }
table.grid td.P { color: var(--in); font-weight: 650; }
table.grid td.A { color: var(--out); font-weight: 650; }
table.grid td.H { color: var(--half); font-weight: 650; }
table.grid td.L { color: var(--leave); font-weight: 650; }
table.grid td.day { cursor: pointer; }
table.grid td.day:hover { background: var(--brand-soft); }

/* dialog */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(440px, calc(100vw - 2rem)); }
dialog::backdrop { background: rgba(20, 25, 22, 0.45); }
dialog form { padding: 1.1rem; }
dialog .actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1rem; }

/* auth */
.auth { max-width: 380px; margin: 12vh auto 0; padding: 0 1rem; }
.auth h1 { font-size: 1.4rem; margin-bottom: 0.25rem; }

#toast {
  position: fixed; left: 50%; bottom: 5rem; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 0.6rem 1rem; border-radius: 10px; font-size: 0.9rem; opacity: 0; pointer-events: none; transition: opacity 0.2s; z-index: 50;
}
#toast.show { opacity: 1; }
#toast.bad { background: var(--out); }
.row.top-align { align-items: flex-start; }
.pad { padding: 0.75rem 1rem; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; color: inherit; }
.list li.tappable { cursor: pointer; }
.list li.tappable:hover, .list li.tappable:focus-visible { background: #faf9f5; }
.push-left { margin-right: auto; }
.filters { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 0.5rem; align-items: end; }
@media (max-width: 600px) { .filters { grid-template-columns: 1fr 1fr; } }
.row.top-align > * { flex: 1 1 260px; }
td a { color: var(--brand); }
.filters > * { min-width: 0; }
table td:first-child { white-space: nowrap; }
.marks button { width: auto; min-width: 58px; height: 52px; padding: 0.2rem 0.35rem; display: inline-flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1; }
.marks button b { font-size: 1rem; }
.marks button small { font-size: 0.66rem; font-weight: 550; white-space: nowrap; }
@media (max-width: 560px) {
  .list li.att { flex-wrap: wrap; }
  .list li.att .main { flex-basis: 100%; }
  .list li.att .marks { width: 100%; }
  .list li.att .marks button { flex: 1; }
}
.tiles.three { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 600px) { .tiles.three { grid-template-columns: repeat(3, 1fr); } .tiles.three .value { font-size: 0.95rem; } }
.pill { font-size: 0.75rem; font-weight: 650; padding: 0.2rem 0.55rem; border-radius: 999px; }
.pill.paid { background: var(--brand-soft); color: var(--in); }
main h3 { margin-top: 1.25rem; }
table.grid td.sun { background: #f1efe7; }
table.grid td.S { color: var(--muted); font-weight: 650; }
.note { background: var(--brand-soft); color: var(--brand); padding: 0.6rem 0.8rem; border-radius: 10px; font-size: 0.9rem; }

/* header and navigation */
header.top h1 { display: flex; align-items: baseline; gap: 0.6rem; min-width: 0; }
header.top .page-title { display: none; font-weight: 500; opacity: 0.85; }
.avatar {
  margin-left: auto; width: 38px; height: 38px; border-radius: 50%; background: rgba(255,255,255,0.18); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; font-weight: 700; text-decoration: none; flex: none;
}
.avatar.active, .avatar:hover { background: rgba(255,255,255,0.32); }
nav.tabs a { display: inline-flex; align-items: center; gap: 0.4rem; }
nav.tabs svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.wide { width: 100%; }

@media (max-width: 700px) {
  header.top .brand { display: none; }
  header.top .page-title { display: inline; font-size: 1.1rem; opacity: 1; font-weight: 650; }
  nav.tabs {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; z-index: 30; padding: 0 0 env(safe-area-inset-bottom);
    border-top: 1px solid var(--line); border-bottom: 0; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; overflow: visible;
  }
  nav.tabs a {
    flex-direction: column; gap: 0.15rem; padding: 0.45rem 0.1rem 0.5rem; font-size: 0.7rem; border-bottom: 0;
    border-top: 3px solid transparent; justify-content: center; min-height: 58px;
  }
  nav.tabs a.active { border-top-color: var(--brand); border-bottom: 0; }
  nav.tabs svg { width: 22px; height: 22px; }
  main { padding-bottom: calc(8rem + env(safe-area-inset-bottom)); }
  .savebar { bottom: calc(61px + env(safe-area-inset-bottom)); }
  #toast { bottom: calc(9rem + env(safe-area-inset-bottom)); }
}
