:root {
  --blue: #1f8fc9;
  --blue-deep: #0d6fa0;
  --blue-soft: #e7f4fb;
  --yellow: #e8a317;
  --bg: #f5f7fa;
  --card: #ffffff;
  --ink: #152433;
  --muted: #5b6b7c;
  --line: #dce3ea;
  --green: #15803d;
  --danger: #dc2626;
  --radius: 16px;
  --shadow: 0 4px 16px rgba(21, 36, 51, 0.06);
  --font: "Noto Sans Khmer", "Outfit", system-ui, sans-serif;
  --fs-xs: 14px;
  --fs-sm: 15px;
  --fs-md: 17px;
  --fs-lg: 20px;
  --fs-xl: 24px;
  --fs-hero: 44px;
}

.app.dark {
  --bg: #121820;
  --card: #1c2430;
  --ink: #eef3f8;
  --muted: #9aabb9;
  --line: #2d3845;
  --blue-soft: rgba(31, 143, 201, 0.18);
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.28);
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  font-family: var(--font);
  font-size: var(--fs-md);
  color: var(--ink);
  background: #d8e0e8;
  -webkit-font-smoothing: antialiased;
}

button, input, select { font: inherit; color: inherit; }
button { border: 0; background: none; cursor: pointer; }

.app {
  width: min(100vw, 430px);
  height: min(100dvh, 900px);
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  background: var(--bg);
  overflow: hidden;
  box-shadow: 0 20px 50px rgba(21, 36, 51, 0.18);
  color: var(--ink);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 18px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}
.brand h1 {
  margin: 0;
  font-size: var(--fs-xl);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: var(--fs-sm);
  font-weight: 600;
}
.lang-switch {
  display: flex;
  flex-shrink: 0;
  gap: 2px;
  padding: 3px;
  border-radius: 10px;
  background: var(--blue-soft);
  border: 1px solid rgba(31, 143, 201, 0.18);
}
.lang-opt {
  min-height: 32px;
  min-width: 44px;
  padding: 0 8px;
  border-radius: 7px;
  font-size: 12px;
  font-weight: 800;
  color: var(--muted);
  line-height: 1;
}
.lang-opt.active {
  background: var(--card);
  color: var(--blue-deep);
  box-shadow: 0 1px 4px rgba(31, 143, 201, 0.14);
}

.content {
  min-height: 0;
  overflow: auto;
  padding: 14px 14px 16px;
  scrollbar-width: thin;
}

.screen { display: none; gap: 12px; }
.screen.active { display: grid; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  padding: 14px;
}

.btn {
  min-height: 46px;
  padding: 0 18px;
  border-radius: 14px;
  font-weight: 700;
  font-size: var(--fs-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.btn:active { opacity: 0.92; }
.btn.outline {
  background: var(--card);
  color: var(--blue-deep);
  box-shadow: inset 0 0 0 2px var(--blue);
}
.btn.solid {
  background: var(--blue);
  color: #fff;
}
.btn.yellow {
  background: var(--yellow);
  color: #152433;
}
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.4; cursor: default; }

.hint {
  margin: 0;
  color: var(--muted);
  font-size: var(--fs-sm);
  line-height: 1.5;
  font-weight: 600;
}
.score-error {
  margin: 0;
  padding: 9px 11px;
  border: 1px solid #fecaca;
  border-radius: 10px;
  background: #fef2f2;
  color: var(--danger);
  font-size: var(--fs-xs);
  font-weight: 800;
  line-height: 1.45;
}
.course-row input.score-input-invalid {
  border-color: var(--danger);
  background: #fff7f7;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.12);
}

.course-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.sem-tabs {
  display: flex;
  flex: 1;
  gap: 6px;
  padding: 4px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--line);
}
.sem-tab {
  flex: 1;
  min-height: 36px;
  border-radius: 9px;
  font-size: var(--fs-sm);
  font-weight: 800;
  color: var(--muted);
}
.sem-tab.active {
  background: var(--blue);
  color: #fff;
  box-shadow: 0 2px 8px rgba(31, 143, 201, 0.28);
}
.btn-sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: var(--fs-sm);
  border-radius: 10px;
  flex-shrink: 0;
}
.sem-panel { display: block; }
.sem-panel[hidden] { display: none !important; }

.course-sheet-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.course-sheet {
  display: grid;
  gap: 6px;
  min-width: 360px;
  padding: 8px;
  min-width: 0;
}
.course-table-head {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 74px 74px 26px;
  gap: 8px;
  padding: 0 2px;
  font-size: var(--fs-xs);
  font-weight: 800;
  color: var(--muted);
  text-align: center;
}
.course-table-head span:first-child { text-align: left; }
.course-table-head .spacer { visibility: hidden; }
.course-list { display: grid; gap: 5px; }
.course-row {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) 74px 74px 26px;
  gap: 8px;
  align-items: center;
}
.course-row .name,
.course-row .mid,
.course-row .sem {
  height: 44px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  font-size: var(--fs-sm);
  font-weight: 700;
}
.course-row .name {
  padding: 0 8px;
  font-size: var(--fs-sm);
}
.course-row .mid,
.course-row .sem {
  text-align: center;
  padding-inline: 8px;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.course-row .mid::-webkit-outer-spin-button,
.course-row .mid::-webkit-inner-spin-button,
.course-row .sem::-webkit-outer-spin-button,
.course-row .sem::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.course-row .name:focus,
.course-row .mid:focus,
.course-row .sem:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(31, 143, 201, 0.18);
  background: var(--card);
}
.course-row .remove {
  width: 26px;
  height: 36px;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  font-weight: 600;
}
.course-row .remove:hover,
.course-row .remove:active {
  background: #fee2e2;
  color: var(--danger);
}
.course-row .remove:disabled {
  opacity: 0.25;
  cursor: default;
  background: transparent;
}
.gpa-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}
.gpa-card {
  padding: 10px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  text-align: center;
}
.gpa-card.primary {
  grid-column: 1 / -1;
  background: var(--blue);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 8px 24px rgba(31, 143, 201, 0.25);
}
.gpa-card .tag {
  margin: 0;
  font-size: var(--fs-xs);
  font-weight: 700;
  opacity: 0.85;
}
.gpa-card:not(.primary) .tag { color: var(--muted); }
.gpa-card .val {
  margin: 4px 0 2px;
  font-size: 24px;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1;
}
.gpa-card.primary .val { font-size: var(--fs-hero); margin: 6px 0 4px; }
.gpa-card .sub {
  margin: 0;
  font-size: var(--fs-xs);
  font-weight: 600;
  opacity: 0.8;
}
.gpa-card:not(.primary) .sub { color: var(--muted); }
.gpa-card.pending { opacity: 0.72; }
.gpa-card.pending .val { font-size: 20px; }
.gpa-card.home-placeholder { grid-column: 1 / -1; }

.result-year-bar {
  padding: 10px 12px;
  text-align: center;
  background: var(--blue);
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 18px rgba(31, 143, 201, 0.22);
}
.result-year-bar .year-gpa {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.result-year-bar .year-note {
  margin: 4px 0 0;
  font-size: var(--fs-xs);
  font-weight: 600;
  opacity: 0.9;
}
#resultPanels { display: grid; gap: 8px; }
.result-toolbar { margin: 0; }

.result-table-card {
  padding: 8px 10px;
  overflow: hidden;
}
.result-table-wrap { overflow-x: auto; }
.grade-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: var(--fs-xs);
}
.grade-table th {
  padding: 4px 2px 6px;
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  border-bottom: 1px solid var(--line);
  background: transparent;
}
.grade-table th:first-child,
.grade-table td:first-child { text-align: left; padding-left: 0; }
.grade-table td {
  padding: 7px 2px;
  border-bottom: 1px solid var(--line);
  text-align: center;
  vertical-align: middle;
  font-weight: 700;
  font-size: var(--fs-sm);
}
.grade-table tbody tr:last-child td { border-bottom: 0; }
.grade-table .col-module {
  width: 30%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.grade-table .col-grade {
  font-weight: 800;
  color: var(--blue-deep);
}
.result-gpa-line {
  margin: 8px 0 0;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  text-align: center;
  font-weight: 800;
  font-size: var(--fs-md);
  color: var(--blue-deep);
}
.result-sem-panel[hidden] { display: none !important; }

.course-bar {
  padding: 8px 14px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.course-bar[hidden] { display: none !important; }

input:not(.name):not(.mid):not(.sem), select {
  width: 100%;
  height: 52px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  padding: 0 14px;
  outline: none;
  font-size: var(--fs-md);
  font-weight: 700;
}
input:focus, select:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(31, 143, 201, 0.18);
  background: var(--card);
}

.msg {
  margin: 0;
  text-align: center;
  font-size: var(--fs-xl);
  font-weight: 800;
}
.msg.excellent { color: var(--green); }
.msg.good { color: var(--blue-deep); }
.msg.improve { color: #b45309; }

.settings-list {
  background: var(--card);
  border-radius: 18px;
  border: 1px solid var(--line);
  overflow: hidden;
}
.settings-row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 52px;
  padding: 0 18px;
  text-align: left;
  font-size: var(--fs-md);
  font-weight: 700;
  color: var(--ink);
  border-bottom: 1px solid var(--line);
}
.settings-row:last-child { border-bottom: 0; }
.settings-row:active { background: var(--blue-soft); }
.settings-row .value {
  color: var(--muted);
  font-size: var(--fs-sm);
  font-weight: 600;
}

.switch {
  position: relative;
  width: 50px;
  height: 30px;
  flex: 0 0 auto;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}
.switch span {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: #c5ced6;
  transition: 0.2s ease;
}
.switch span::before {
  content: "";
  position: absolute;
  width: 24px;
  height: 24px;
  left: 3px;
  top: 3px;
  border-radius: 50%;
  background: #fff;
  transition: 0.2s ease;
  box-shadow: 0 2px 6px rgba(0,0,0,.15);
}
.switch input:checked + span { background: var(--blue); }
.switch input:checked + span::before { transform: translateX(20px); }

.settings-panel { display: none; gap: 12px; }
.settings-panel.open { display: grid; }
.settings-panel h3 {
  margin: 0;
  font-size: var(--fs-lg);
  font-weight: 800;
}
.term-list {
  display: grid;
  gap: 0;
  margin: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.term-item {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 10px;
  padding: 11px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.term-item:last-child { border-bottom: 0; }
.term-item dt { font-weight: 900; color: var(--blue-dark); }
.term-item dd { margin: 0; color: var(--muted); font-size: var(--fs-sm); line-height: 1.45; }

.tabbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 10px 10px max(12px, env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
}
.tab {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 10px 4px;
  border-radius: 12px;
  color: var(--muted);
  font-size: var(--fs-sm);
  font-weight: 700;
}
.tab.active {
  color: var(--blue-deep);
  background: var(--blue-soft);
}
.tab .ico {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.45;
}
.tab.active .ico {
  opacity: 1;
  background: var(--blue);
}

.app.dark .course-row .remove { background: transparent; }
.app.dark input,
.app.dark .course-row .name,
.app.dark .course-row .mid,
.app.dark .course-row .sem,
.app.dark .gpa-card:not(.primary) {
  background: #151c26;
  color: var(--ink);
}
.app.dark .course-row .mid,
.app.dark .course-row .sem { background: #1c2430; }
.app.dark .gpa-card:not(.primary) { background: #1c2430; }
.app.dark .result-year-bar { background: var(--blue-deep); }
