:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #4f46e5;
  --primary-d: #4338ca;
  --primary-light: #eef2ff;
  --ok: #059669;
  --ok-bg: #ecfdf5;
  --err: #dc2626;
  --err-bg: #fef2f2;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .10);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, .10);
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 1.75rem; font-weight: 800; letter-spacing: -0.02em; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; font-weight: 700; letter-spacing: -0.01em; margin: 2rem 0 .5rem; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: .85rem 1.5rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar .brand {
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  color: var(--ink);
}
.topbar .brand::before {
  content: "";
  display: inline-block;
  width: 10px; height: 10px;
  margin-right: .5rem;
  border-radius: 3px;
  background: var(--primary);
  vertical-align: middle;
}
.topbar .spacer { flex: 1; }
.topbar .who { color: var(--muted); font-size: .88rem; }
.topbar a { color: var(--muted); font-weight: 600; font-size: .9rem; }
.topbar a:hover { color: var(--primary); text-decoration: none; }
.inline { display: inline; margin: 0; }
.inline button { margin: 0; }

/* ---------- Layout ---------- */
.container { max-width: 1040px; margin: 2rem auto; padding: 0 1.5rem; }
.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem;
}
.page-head h1 { margin: 0; }
.hint { color: var(--muted); font-size: .9rem; }

/* ---------- Auth / centered card ---------- */
.card {
  max-width: 420px;
  margin: 8vh auto;
  background: var(--card);
  padding: 2.25rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
}
.card h1 { margin-top: 0; }

/* ---------- Forms ---------- */
label { display: block; margin: 1.1rem 0 .35rem; font-weight: 600; font-size: .85rem; }
input[type=text], input[type=email], input[type=file], input[type=password] {
  width: 100%;
  padding: .6rem .75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .95rem;
  background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
input[type=text]:focus, input[type=email]:focus, input[type=password]:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .15);
}

/* ---------- Buttons ---------- */
button, .button {
  display: inline-flex; align-items: center; justify-content: center;
  gap: .4rem;
  margin-top: 1rem;
  padding: .6rem 1.15rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background .15s, box-shadow .15s, transform .05s;
  box-shadow: var(--shadow);
}
button:hover, .button:hover { background: var(--primary-d); text-decoration: none; }
button:active, .button:active { transform: translateY(1px); }

.btn-sm { margin-top: 0; padding: .35rem .7rem; font-size: .82rem; box-shadow: none; }
.btn-outline {
  background: #fff; color: var(--primary); border-color: var(--border);
}
.btn-outline:hover { background: var(--primary-light); color: var(--primary-d); }
.btn-danger { background: #fff; color: var(--err); border-color: var(--border); box-shadow: none; }
.btn-danger:hover { background: var(--err-bg); color: var(--err); }

button.link {
  background: none; color: var(--primary); padding: 0; margin: 0;
  box-shadow: none; font-weight: 600; border: 0;
}
button.link:hover { background: none; text-decoration: underline; }

button.is-loading { cursor: progress; opacity: .9; }
.spinner {
  display: inline-block;
  width: .95em; height: .95em;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  margin-right: .5rem;
  vertical-align: -.15em;
  animation: cm-spin .6s linear infinite;
}
@keyframes cm-spin { to { transform: rotate(360deg); } }
.sending-hint { color: var(--muted); font-size: .85rem; margin: .65rem 0 0; }

/* ---------- Messages ---------- */
.msg { padding: .65rem .85rem; border-radius: 8px; font-size: .9rem; margin: .5rem 0; }
.msg.error, .error { color: var(--err); }
.msg.ok, .ok { color: var(--ok); }
.msg.error { background: var(--err-bg); }
.msg.ok { background: var(--ok-bg); }
code { background: var(--primary-light); color: var(--primary-d); padding: .1rem .35rem; border-radius: 4px; font-size: .85em; }

/* ---------- Tables ---------- */
.table-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.table-scroll { overflow-x: auto; }
.empty-state { padding: 2.5rem; text-align: center; }
.panel { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.5rem 1.75rem; max-width: 560px; }
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  background: #f8fafc;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: .8rem 1rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
tbody td { padding: .85rem 1rem; border-bottom: 1px solid var(--border); font-size: .9rem; vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #f8fafc; }
tfoot td { padding: .85rem 1rem; border-top: 2px solid var(--border); font-weight: 700; font-size: .9rem; background: #f8fafc; }
tfoot td:first-child { text-align: right; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-size: .78rem; }

/* Pin the "Total" column to the right so it stays visible when the marks table
   is wider than its card and scrolls horizontally. */
.col-total {
  position: sticky; right: 0; z-index: 1;
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 8px -8px rgba(15, 23, 42, .18);
}
thead th.col-total { background: #f8fafc; }
tbody td.col-total { background: var(--card); }
tbody tr:hover td.col-total { background: #f8fafc; }

/* code/course badge */
.badge {
  display: inline-flex; align-items: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .8rem; font-weight: 700;
  color: var(--primary-d); background: var(--primary-light);
  padding: .2rem .55rem; border-radius: 6px;
}

/* inline editable name */
.inline-edit { display: flex; flex-wrap: nowrap; gap: .5rem; align-items: center; margin: 0; }
.inline-edit input[type=text] { margin: 0; width: 170px; min-width: 140px; padding: .4rem .55rem; font-size: .88rem; }

/* shareable link cell */
.sharecell { display: flex; align-items: center; gap: .5rem; }
.share-url {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .82rem; color: var(--muted);
  background: #f8fafc; border: 1px solid var(--border); border-radius: 6px;
  padding: .35rem .55rem;
  max-width: 200px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ---------- Charts ---------- */
.toolbar { display: flex; align-items: center; gap: .6rem; margin: .5rem 0 1.25rem; }
.toolbar label { margin: 0; }
select {
  padding: .5rem .7rem; border: 1px solid var(--border); border-radius: 8px;
  font-size: .9rem; background: #fff; color: var(--ink); min-width: 200px;
}
select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.15); }

.charts { display: flex; flex-wrap: wrap; gap: 1.5rem; margin-top: .5rem; }
.charts figure {
  margin: 0; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem;
}
.charts figcaption { font-weight: 700; margin-bottom: .75rem; font-size: .95rem; }
.charts figure canvas { max-width: 100%; height: auto; }
.secondary { margin-top: .5rem; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .topbar { gap: .75rem; padding: .7rem 1rem; flex-wrap: wrap; }
  .topbar .who { display: none; }            /* email is long on phones */
  .container { margin: 1.25rem auto; padding: 0 1rem; }
  .page-head { flex-direction: column; align-items: stretch; }
  .page-head .button { width: 100%; text-align: center; }
  .charts { flex-direction: column; }
  .charts figure { width: 100%; }
  h1 { font-size: 1.45rem; }

  /* Stacked-card layout: each row becomes a labelled card instead of a wide table row. */
  table.stack thead { display: none; }
  table.stack, table.stack tbody, table.stack tfoot, table.stack tr, table.stack td { display: block; width: 100%; }
  table.stack tfoot td { border-top: 0; background: transparent; }
  table.stack tfoot tr { border-top: 2px solid var(--border); background: #f8fafc; }
  /* In the stacked card layout the Total is just another labelled row. */
  table.stack td.col-total, table.stack th.col-total {
    position: static; border-left: 0; box-shadow: none; background: transparent;
  }
  table.stack tr { padding: .6rem .25rem; border-bottom: 1px solid var(--border); }
  table.stack tr:last-child { border-bottom: 0; }
  table.stack td {
    display: flex; align-items: center; justify-content: space-between;
    gap: 1rem; border: 0; padding: .4rem 1rem; font-size: .92rem;
  }
  table.stack td::before {
    content: attr(data-label);
    font-weight: 700; color: var(--muted);
    font-size: .72rem; text-transform: uppercase; letter-spacing: .04em;
    flex: 0 0 auto;
  }
  table.stack td[data-label=""]::before { content: none; }
  .inline-edit { flex: 1; justify-content: flex-end; }
  .inline-edit input[type=text] { width: auto; flex: 1; min-width: 0; }
  .sharecell { flex: 1; justify-content: flex-end; min-width: 0; }
  .share-url { max-width: 100%; flex: 1; }
}
