:root {
  color-scheme: light;
  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-2: #eef3f1;
  --text: #1e2826;
  --muted: #63706d;
  --line: #d7e0dd;
  --primary: #23685d;
  --primary-dark: #174941;
  --blue: #2f5fb8;
  --amber: #8a5a00;
  --danger: #b42318;
  --shadow: 0 10px 30px rgba(31, 47, 43, 0.08);
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

a {
  color: inherit;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid rgba(215, 224, 221, 0.86);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(14px);
}

.brand {
  text-decoration: none;
  font-size: 20px;
  font-weight: 800;
}

.topnav {
  display: flex;
  gap: 10px;
  color: var(--muted);
  font-size: 14px;
}

.topnav a {
  text-decoration: none;
}

.page {
  width: min(1040px, 100%);
  margin: 0 auto;
  padding: 18px 16px 88px;
}

.dashboard,
.set-head {
  display: grid;
  gap: 10px;
  margin-bottom: 16px;
}

.dashboard {
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.12;
  letter-spacing: 0;
}

h2 {
  font-size: 18px;
  line-height: 1.2;
}

.subtle,
.hint,
small {
  color: var(--muted);
}

.subtle {
  margin-top: 5px;
}

.metrics {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

.metrics span {
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  white-space: nowrap;
}

.metrics strong {
  color: var(--text);
}

.panel,
.history,
.notice {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 16px;
  box-shadow: var(--shadow);
}

.stack {
  display: grid;
  gap: 16px;
}

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

.field-group {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

legend,
label > span {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 13px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
}

.segmented label,
.grade-options label {
  min-width: 0;
}

.segmented input,
.grade-options input {
  position: absolute;
  width: 1px;
  min-height: 1px;
  opacity: 0;
  pointer-events: none;
}

.segmented span,
.grade-options span {
  display: flex;
  min-height: 40px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-weight: 700;
}

.segmented input:checked + span,
.grade-options input:checked + span {
  border-color: var(--primary-dark);
  background: var(--primary-dark);
  color: #fff;
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.check-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 9px;
  align-items: center;
  min-height: 48px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.check-row input {
  width: 18px;
  height: 18px;
}

.check-row strong,
.set-row strong,
.term-row strong,
.grade-term strong {
  overflow-wrap: anywhere;
}

.check-row small,
.set-row small {
  display: block;
  margin-top: 3px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
}

input,
select,
button,
.button {
  width: 100%;
  min-height: 44px;
  border-radius: var(--radius);
  font: inherit;
}

input,
select {
  border: 1px solid var(--line);
  padding: 10px 11px;
  background: #fff;
  color: var(--text);
}

button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  padding: 10px 14px;
  background: var(--primary);
  color: #fff;
  font-weight: 760;
  text-decoration: none;
  cursor: pointer;
}

button:disabled,
.button:disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.secondary {
  border-color: var(--line);
  background: #fff;
  color: var(--text);
}

.actions {
  display: flex;
  justify-content: flex-end;
}

.actions.inline,
.player-controls {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.back {
  color: var(--blue);
  text-decoration: none;
  font-weight: 700;
}

.notice {
  margin-bottom: 14px;
  box-shadow: none;
}

.notice.error {
  border-color: rgba(180, 35, 24, 0.32);
  color: var(--danger);
  background: #fff7f6;
}

.history {
  margin-top: 18px;
  box-shadow: none;
}

.set-list,
.review-list,
.grade-form {
  display: grid;
  gap: 10px;
}

.set-list {
  margin-top: 12px;
}

.set-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  text-decoration: none;
}

.result {
  color: var(--muted);
  white-space: nowrap;
}

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

.term-row,
.grade-row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  padding: 12px;
}

.term-row,
.grade-term {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 10px;
}

.position {
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--muted);
  font-weight: 800;
}

.term-row p,
.grade-term p {
  margin-top: 4px;
  color: var(--muted);
  line-height: 1.45;
}

.example {
  color: var(--amber) !important;
}

.practice {
  display: grid;
  gap: 14px;
}

.practice-screen {
  min-height: 330px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #172c29;
  color: #fff;
  text-align: center;
}

.practice-screen .subtle {
  color: rgba(255, 255, 255, 0.72);
}

.counter {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 7px;
  font-size: 72px;
  font-weight: 850;
  line-height: 1;
}

.counter small {
  color: rgba(255, 255, 255, 0.68);
  font-size: 28px;
}

.player-controls {
  position: sticky;
  bottom: 10px;
  z-index: 5;
  padding: 10px;
  border: 1px solid rgba(215, 224, 221, 0.9);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
}

.grade-row {
  display: grid;
  gap: 12px;
}

.grade-options {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 7px;
}

.bottom-bar {
  position: sticky;
  bottom: 0;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 84%, rgba(244, 247, 246, 0));
}

@media (max-width: 720px) {
  .page {
    padding: 15px 12px 82px;
  }

  .dashboard {
    grid-template-columns: 1fr;
  }

  .metrics {
    justify-content: flex-start;
  }

  .book-grid,
  .form-grid,
  .set-row,
  .actions.inline,
  .player-controls {
    grid-template-columns: 1fr;
  }

  .grade-options {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .counter {
    font-size: 64px;
  }
}
