:root {
  --bg: #0b0d12;
  --bg-elev: #12151d;
  --bg-elev-2: #171b25;
  --surface: #14171f;
  --border: #232734;
  --border-soft: #1c202b;
  --text: #e8eaf0;
  --text-muted: #99a1b3;
  --text-faint: #6b7284;
  --accent: #22c55e;
  --accent-strong: #16a34a;
  --accent-soft: rgba(34, 197, 94, 0.12);
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --warn: #fbbf24;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 30px -12px rgba(0, 0, 0, 0.6);
  --ring: 0 0 0 3px rgba(34, 197, 94, 0.25);
  --container: 960px;
}

* { box-sizing: border-box; }

/* `display` on a specific selector otherwise overrides the browser default for the
   `hidden` attribute, so keep hidden views hidden explicitly. */
[hidden] { display: none !important; }

html { -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; }

body {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(1100px 600px at 100% -10%, rgba(34, 197, 94, 0.08), transparent 60%),
    radial-gradient(900px 500px at -10% 0%, rgba(56, 189, 248, 0.05), transparent 55%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  margin: 0;
  padding: 2.5rem 1.25rem 4rem;
  line-height: 1.5;
  font-size: 15px;
}

h1, h2, h3 { letter-spacing: -0.02em; }

/* ---------- Brand ---------- */

.brand { display: flex; align-items: center; gap: 0.85rem; }

.brand-text h1 { margin: 0; font-size: 1.35rem; line-height: 1.1; }
.brand-sub { margin: 0.1rem 0 0; color: var(--text-faint); font-size: 0.8rem; letter-spacing: 0.02em; }

.brand-logo {
  display: block;
  width: 44px;
  height: 44px;
  object-fit: contain;
  background: #ffffff;
  border-radius: 11px;
  box-shadow: 0 6px 18px -6px rgba(34, 197, 94, 0.35);
}

.brand-logo--lg {
  width: 168px;
  height: 168px;
  border-radius: 20px;
}

/* ---------- Login ---------- */

#login-view {
  min-height: 70vh;
  display: grid;
  place-items: center;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: linear-gradient(180deg, var(--bg-elev-2), var(--surface));
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 2.25rem 2rem;
  text-align: center;
  box-shadow: var(--shadow);
}

.login-card .brand-logo { margin: 0 auto 1rem; }
.login-card h1 { margin: 0; font-size: 1.6rem; }
.login-sub { margin: 0.35rem 0 1.5rem; color: var(--text-muted); font-size: 0.9rem; }
.login-card form { margin-top: 0; }

/* ---------- Forms ---------- */

form { display: flex; flex-direction: column; gap: 0.7rem; }

input, select, button {
  font-family: inherit;
  font-size: 0.92rem;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease, transform 0.05s ease;
}

input::placeholder { color: var(--text-faint); }

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--ring);
}

select { cursor: pointer; }

input[type="file"] {
  padding: 0.55rem 0.75rem;
  font-size: 0.85rem;
}

input[type="file"]::file-selector-button {
  margin-right: 0.75rem;
  padding: 0.45rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-elev-2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

button {
  background: linear-gradient(145deg, var(--accent), var(--accent-strong));
  color: #04140a;
  font-weight: 650;
  border: none;
  cursor: pointer;
}

button:hover { filter: brightness(1.05); }
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: not-allowed; filter: none; }

.btn-secondary {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  font-weight: 550;
}
.btn-secondary:hover { background: var(--bg-elev); color: var(--text); filter: none; }

.error { color: var(--danger); font-size: 0.85rem; min-height: 1rem; margin: 0.75rem 0 0; }

.auth-toggle { margin: 1.1rem 0 0; font-size: 0.85rem; color: var(--text-muted); }
.auth-toggle a { color: var(--accent); text-decoration: none; font-weight: 600; }
.auth-toggle a:hover { text-decoration: underline; }

/* ---------- Layout ---------- */

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  max-width: var(--container);
  margin: 0 auto 1.75rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.header-link {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  padding: 0.65rem 0.85rem;
}

.card {
  max-width: var(--container);
  margin: 0 auto 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 1.4rem 1.6rem;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.02) inset;
}

.card h2 {
  margin: 0 0 0.15rem;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.card h2::before {
  content: "";
  width: 3px;
  height: 1.05em;
  border-radius: 2px;
  background: linear-gradient(180deg, var(--accent), var(--accent-strong));
}

.card form { flex-direction: row; flex-wrap: wrap; align-items: flex-end; gap: 0.8rem; margin-top: 1.1rem; }
.card form > input, .card form > select { flex: 1 1 190px; min-width: 0; }
.card form button { flex: 0 0 auto; align-self: flex-end; }

/* Labeled field: a stacked label + input that flexes within a row form. */
.field { display: flex; flex-direction: column; gap: 0.35rem; flex: 1 1 190px; min-width: 0; }
.field > label { font-size: 0.76rem; font-weight: 600; color: var(--text-muted); letter-spacing: 0.01em; }
.field input, .field select { width: 100%; }

/* Group heading that sits above a set of cards. */
.section-label {
  max-width: var(--container);
  margin: 2rem auto 0.85rem;
  padding: 0 0.25rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.section-label-hint { font-weight: 500; letter-spacing: 0.02em; text-transform: none; color: var(--text-faint); }

.status-line {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-top: 1rem;
  padding: 0.7rem 0.85rem;
  background: var(--bg-elev);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  font-size: 0.86rem;
}
.status-detail { color: var(--text-muted); }

.hint { color: var(--text-muted); font-size: 0.85rem; margin: 0.4rem 0 0; }
.card > .hint { margin-top: 0.35rem; }

.form-note { color: var(--text-muted); font-size: 0.85rem; margin: 0.85rem 0 0; }

#upload-result { margin: 0.9rem 0 0; font-size: 0.9rem; color: var(--text); }

#upload-warnings {
  white-space: pre-wrap;
  color: var(--warn);
  font-size: 0.8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  margin: 0.6rem 0 0;
  line-height: 1.55;
}

/* ---------- Tables ---------- */

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 1.1rem;
  font-size: 0.87rem;
}

thead th {
  color: var(--text-faint);
  font-weight: 600;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  border-bottom: 1px solid var(--border);
  padding: 0 0.65rem 0.6rem;
  text-align: left;
}

tbody td {
  padding: 0.7rem 0.65rem;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
}

tbody tr { transition: background 0.12s ease; }
tbody tr:hover { background: var(--bg-elev); }
tbody tr:last-child td { border-bottom: none; }

td button {
  padding: 0.35rem 0.7rem;
  font-size: 0.78rem;
  font-weight: 550;
  margin-right: 0.35rem;
}

.toggle-btn, .recompute-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.toggle-btn:hover, .recompute-btn:hover { background: var(--bg-elev-2); filter: none; }

.delete-grade-btn, .delete-course-btn, .delete-band-btn {
  background: transparent;
  color: var(--danger);
  border: 1px solid transparent;
}
.delete-grade-btn:hover, .delete-course-btn:hover, .delete-band-btn:hover {
  background: var(--danger-soft);
  filter: none;
}

/* ---------- Pills ---------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  border: 1px solid transparent;
}

.pill::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.pill.open { background: var(--accent-soft); color: #6ee7a0; border-color: rgba(34, 197, 94, 0.3); }
.pill.closed { background: var(--danger-soft); color: #fca5a5; border-color: rgba(248, 113, 113, 0.28); }

/* ---------- Results preview ---------- */

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}
.card-head h2 { margin: 0; }

.results-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem;
  margin-top: 1.1rem;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.table-wrap table { margin-top: 0.9rem; }

.action-group {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

/* ---------- Responsive ---------- */

@media (max-width: 768px) {
  body {
    padding: 1rem max(0.85rem, env(safe-area-inset-left)) calc(2.5rem + env(safe-area-inset-bottom)) max(0.85rem, env(safe-area-inset-right));
    font-size: 16px;
  }

  #login-view {
    min-height: auto;
    padding: 1.5rem 0 2rem;
  }

  .login-card {
    padding: 1.75rem 1.25rem;
    border-radius: 16px;
  }

  .brand-logo--lg {
    width: 132px;
    height: 132px;
  }

  header {
    flex-wrap: wrap;
    position: sticky;
    top: 0;
    z-index: 20;
    background: rgba(11, 13, 18, 0.92);
    backdrop-filter: blur(10px);
    margin: -1rem max(-0.85rem, calc(-1 * env(safe-area-inset-left))) 1.25rem max(-0.85rem, calc(-1 * env(safe-area-inset-right)));
    padding: 0.85rem max(0.85rem, env(safe-area-inset-right)) 0.85rem max(0.85rem, env(safe-area-inset-left));
    border-bottom: 1px solid var(--border-soft);
  }

  .header-actions {
    width: 100%;
    justify-content: stretch;
  }

  .header-actions .btn-secondary,
  .header-actions .header-link {
    flex: 1;
    justify-content: center;
    min-height: 44px;
  }

  .card {
    padding: 1.1rem 1rem;
    border-radius: 12px;
  }

  .card h2 {
    font-size: 1rem;
  }

  .card form {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
  }

  .card form .field {
    flex: 1 1 auto;
    width: 100%;
  }

  .card form button {
    width: 100%;
    min-height: 44px;
  }

  .card-head {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }

  .card-head .btn-secondary {
    width: 100%;
    min-height: 44px;
  }

  .results-controls {
    flex-direction: column;
    gap: 0.75rem;
  }

  .results-controls .field {
    width: 100%;
  }

  input, select, button {
    font-size: 16px;
    min-height: 44px;
  }

  input[type="file"] {
    width: 100%;
    min-height: auto;
    padding: 0.65rem 0.75rem;
  }

  td button {
    min-height: 40px;
    margin-right: 0;
  }

  .action-group {
    flex-direction: column;
    width: 100%;
  }

  .action-group button {
    width: 100%;
  }

  .section-label {
    margin-top: 1.5rem;
    font-size: 0.68rem;
    line-height: 1.4;
  }

  .section-label-hint {
    display: block;
    margin-top: 0.2rem;
  }

  .status-line {
    flex-direction: column;
    align-items: flex-start;
  }

  #upload-warnings {
    font-size: 0.78rem;
    overflow-x: auto;
  }

  /* Card-style tables on small screens */
  .table-responsive thead {
    display: none;
  }

  .table-responsive tbody tr {
    display: block;
    margin-bottom: 0.85rem;
    padding: 0.85rem;
    background: var(--bg-elev);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-sm);
  }

  .table-responsive tbody tr:hover {
    background: var(--bg-elev);
  }

  .table-responsive tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.45rem 0;
    border-bottom: 1px solid var(--border-soft);
    white-space: normal;
    text-align: right;
  }

  .table-responsive tbody td:last-child {
    border-bottom: none;
    padding-bottom: 0;
  }

  .table-responsive tbody td:first-child {
    padding-top: 0;
  }

  .table-responsive tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.68rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-faint);
    text-align: left;
    flex-shrink: 0;
  }

  .table-responsive tbody td[data-label=""]::before,
  .table-responsive tbody td:not([data-label])::before {
    content: none;
  }

  .table-responsive tbody td.actions-cell {
    flex-direction: column;
    align-items: stretch;
  }

  .table-responsive tbody td.actions-cell::before {
    margin-bottom: 0.25rem;
  }

  .modal {
    padding: 0;
    align-items: flex-end;
  }

  .modal-card {
    max-width: none;
    width: 100%;
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
    padding: 1.25rem 1rem calc(1.25rem + env(safe-area-inset-bottom));
  }

  .modal-actions .btn-secondary {
    width: 100%;
    min-height: 44px;
  }
}

@media (min-width: 769px) and (max-width: 960px) {
  body { padding: 1.5rem 1rem 3rem; }
  .card form > input, .card form > select, .field { flex: 1 1 100%; }
}

.tag {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 600;
  border: 1px solid var(--border);
  color: var(--text-muted);
  background: var(--bg-elev);
}
.tag-computed { color: #93c5fd; border-color: rgba(147, 197, 253, 0.3); background: rgba(147, 197, 253, 0.1); }
.tag-manual { color: var(--text-muted); }

.grade-fail { color: var(--danger); font-weight: 600; }

/* ---------- Modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(4, 6, 10, 0.72);
  backdrop-filter: blur(3px);
}

.modal-card {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: 82vh;
  overflow-y: auto;
  background: linear-gradient(180deg, var(--bg-elev-2), var(--surface));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem 1.6rem 1.6rem;
  box-shadow: var(--shadow);
}

.modal-card h3 { margin: 0 0 0.3rem; font-size: 1.15rem; padding-right: 2rem; }

.modal-actions { margin: 0.85rem 0 1rem; display: flex; gap: 0.6rem; }

.modal-close {
  position: absolute;
  top: 0.9rem;
  right: 0.9rem;
  width: 2rem;
  height: 2rem;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1.3rem;
  line-height: 1;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.modal-close:hover { background: var(--bg-elev); color: var(--text); filter: none; }
