/* =========================================================
   Grade Ledger — styles
   1. Tokens (light + dark)  2. Base  3. Layout  4. Components
   5. Charts  6. Motion & responsive
   ========================================================= */

/* ---------- 1. Tokens ---------- */
:root {
  --bg: #e9edf1;
  --surface: #ffffff;
  --surface-2: #f2f5f8;
  --ink: #101b26;
  --muted: #5b6b7b;
  --line: #d3dae2;
  --accent: #b3202a;          /* a nod to IIT Delhi's crimson */
  --accent-ink: #ffffff;
  --good: #1b7f5c;
  --warn: #b7791f;
  --bad: #c0392b;
  --radius: 14px;
  --radius-sm: 8px;
  --font-ui: "Avenir Next", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-num: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0e1318;
  --surface: #161d24;
  --surface-2: #1d262f;
  --ink: #e8eef3;
  --muted: #8fa0b0;
  --line: #2a3540;
  --accent: #f0646c;
  --accent-ink: #1b0709;
  --good: #4cc79b;
  --warn: #e0a94a;
  --bad: #ef7b6e;
  color-scheme: dark;
}

/* ---------- 2. Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px/1.5 var(--font-ui);
  transition: background .3s, color .3s;
}
h1, h2, p { margin: 0; }
h2 { font-size: 1.05rem; font-weight: 650; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- 3. Layout ---------- */
.top, main { max-width: 980px; margin: 0 auto; padding-left: 20px; padding-right: 20px; }
.top { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding-top: 22px; padding-bottom: 6px; flex-wrap: wrap; }
.brand h1 { font: 700 1.5rem/1.1 var(--font-num); letter-spacing: -.01em; }
.brand p { color: var(--muted); font-size: .9rem; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
main { padding-bottom: 60px; }

/* Dashboard */
.summary {
  display: grid; grid-template-columns: 1.3fr 1fr; gap: 28px;
  margin-top: 18px; padding: 28px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 20px;
}
.label { color: var(--muted); font-size: .9rem; }
.big { font: 600 clamp(4rem, 12vw, 6.5rem)/1 var(--font-num); letter-spacing: -.03em; font-variant-numeric: tabular-nums; margin: 6px 0 22px; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 18px 16px; margin: 0; align-content: center; }
.stats div { border-left: 2px solid var(--line); padding-left: 12px; }
.stats dt { color: var(--muted); font-size: .85rem; }
.stats dd { margin: 0; font: 600 1.7rem/1.2 var(--font-num); font-variant-numeric: tabular-nums; }

/* CGPA ruler: a 0-10 scale with the CGPA marker and optional target marker */
.ruler { position: relative; padding-top: 10px; }
.track { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.fill { height: 100%; width: 0; background: var(--accent); border-radius: 99px; transition: width .7s cubic-bezier(.2,.8,.2,1); }
.mark { position: absolute; top: 2px; left: 0; width: 3px; height: 24px; margin-left: -1.5px; background: var(--ink); border-radius: 2px; transition: left .7s cubic-bezier(.2,.8,.2,1); }
.mark.target { background: none; border-left: 2px dashed var(--good); }
.ticks { display: flex; justify-content: space-between; color: var(--muted); font-size: .75rem; margin-top: 6px; }

/* Tabs */
.tabs { display: flex; gap: 4px; margin: 26px 0 16px; border-bottom: 1px solid var(--line); }
.tab { background: none; border: 0; padding: 10px 14px; color: var(--muted); font-weight: 600; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color .2s, border-color .2s; }
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.panel { animation: fade .25s ease; }
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.hint { color: var(--muted); font-size: .88rem; margin: 4px 0 14px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.foot { margin-top: 36px; color: var(--muted); font-size: .82rem; text-align: center; }

/* ---------- 4. Components ---------- */
.btn { border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 10px; cursor: pointer; font-weight: 600; font-size: .9rem; transition: transform .15s, background .2s, border-color .2s; }
.btn:hover { border-color: var(--muted); }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger:hover { color: var(--bad); border-color: var(--bad); }
.btn.small { padding: 6px 10px; font-size: .85rem; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; cursor: pointer; transition: transform .15s, color .2s, border-color .2s; }
.icon-btn:hover { border-color: var(--muted); }
.icon-btn:active { transform: scale(.92); }
.icon-btn.danger:hover { color: var(--bad); border-color: var(--bad); }

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; font-size: .92rem; }
.switch input { appearance: none; width: 38px; height: 22px; border-radius: 99px; background: var(--line); position: relative; cursor: pointer; transition: background .2s; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(16px); }

/* Semester card */
.sem { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; animation: pop .3s cubic-bezier(.2,.8,.2,1); }
.sem-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.sem-name { flex: 1 1 160px; min-width: 0; font: 650 1.15rem var(--font-ui); background: transparent; border: 1px solid transparent; border-radius: var(--radius-sm); padding: 4px 8px; margin-left: -8px; }
.sem-name:hover, .sem-name:focus { border-color: var(--line); background: var(--surface-2); }
.sem-stats { display: flex; gap: 16px; color: var(--muted); font-size: .85rem; }
.sem-stats b { color: var(--ink); font: 600 1.1rem var(--font-num); font-variant-numeric: tabular-nums; }

.course { display: grid; grid-template-columns: 110px 1fr 90px 104px 36px; gap: 8px; margin-bottom: 8px; animation: pop .25s cubic-bezier(.2,.8,.2,1); }
.course-head { display: grid; grid-template-columns: 110px 1fr 90px 104px 36px; gap: 8px; color: var(--muted); font-size: .8rem; margin-bottom: 4px; padding: 0 2px; }
.course input, .course select, .fields input, .fields select {
  width: 100%; min-width: 0; padding: 8px 10px; background: var(--surface-2); border: 1px solid transparent; border-radius: var(--radius-sm); transition: border-color .2s, background .2s;
}
.course input:hover, .course select:hover, .fields input:hover, .fields select:hover { border-color: var(--line); }
.course input.invalid { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 10%, var(--surface-2)); }
.course select { cursor: pointer; font-weight: 600; border-left: 4px solid var(--line); }
.course select[data-tone="pass"] { border-left-color: var(--good); }
.course select[data-tone="fail"] { border-left-color: var(--bad); }
.course select[data-tone="other"] { border-left-color: var(--warn); }
.empty { color: var(--muted); font-size: .9rem; padding: 6px 2px 12px; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 10px 0; }
.fields label, .wi-row { font-size: .85rem; color: var(--muted); display: grid; gap: 4px; }
.result { margin: 12px 0; padding: 12px 14px; background: var(--surface-2); border-radius: var(--radius-sm); min-height: 52px; font-size: .92rem; border-left: 3px solid var(--line); }
.result.ok { border-left-color: var(--good); }
.result.bad { border-left-color: var(--bad); }
.result strong { font: 600 1.5rem var(--font-num); font-variant-numeric: tabular-nums; }
.wi-row { grid-template-columns: 1fr 110px; align-items: center; gap: 10px; margin-bottom: 6px; color: var(--ink); }
.wi-row small { color: var(--muted); }
.wi-sem { color: var(--muted); font-size: .8rem; margin: 10px 0 4px; font-weight: 600; }

.rules { margin-top: 20px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; }
.rules summary { cursor: pointer; font-weight: 600; }
.rules-body { font-size: .9rem; color: var(--muted); padding-top: 10px; }
.rules table { border-collapse: collapse; width: 100%; margin-bottom: 10px; font-variant-numeric: tabular-nums; }
.rules th, .rules td { border: 1px solid var(--line); padding: 5px 6px; text-align: center; color: var(--ink); }
.rules li { margin-bottom: 4px; }

dialog { border: 1px solid var(--line); border-radius: 16px; background: var(--surface); color: var(--ink); padding: 22px; width: min(92vw, 400px); }
dialog::backdrop { background: rgba(5, 10, 15, .55); backdrop-filter: blur(2px); }
dialog[open] { animation: pop .22s cubic-bezier(.2,.8,.2,1); }
dialog p { color: var(--muted); margin: 8px 0 18px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem; opacity: 0; pointer-events: none; transition: opacity .25s, transform .25s; max-width: 90vw; z-index: 20; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- 5. Charts ---------- */
.legend { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: .85rem; }
.key { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.key.sgpa { background: var(--accent); }
.key.cgpa { background: var(--ink); margin-left: 8px; }
.chart svg { width: 100%; height: auto; display: block; }
#trendChart svg { width: 100%; height: auto; display: block; margin-top: 8px; }
.g-line { fill: none; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 1200; stroke-dashoffset: 1200; animation: draw 1s .1s ease forwards; }
.g-line.sgpa { stroke: var(--accent); }
.g-line.cgpa { stroke: var(--ink); }
.g-dot { stroke: var(--surface); stroke-width: 2; transition: r .15s; cursor: default; }
.g-dot:hover { r: 7; }
.g-dot.sgpa { fill: var(--accent); }
.g-dot.cgpa { fill: var(--ink); }
.g-grid { stroke: var(--line); stroke-width: 1; }
.g-text { fill: var(--muted); font-size: 11px; }
.bar-row { display: grid; grid-template-columns: 56px 1fr 34px; align-items: center; gap: 10px; margin-bottom: 8px; font-size: .88rem; }
.bar-row.wide { grid-template-columns: 96px 1fr 70px; }
.bar { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 99px; transition: width .6s cubic-bezier(.2,.8,.2,1); }
.bar > i.good { background: var(--good); }
.bar > i.bad { background: var(--bad); }
.bar-row span:last-child { color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- 6. Motion & responsive ---------- */
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.985); } to { opacity: 1; transform: none; } }
@keyframes draw { to { stroke-dashoffset: 0; } }

@media (max-width: 760px) {
  .summary { grid-template-columns: 1fr; padding: 20px; gap: 22px; }
  .two-col { grid-template-columns: 1fr; }
  .course-head { display: none; }
  /* On phones each course becomes two rows: code/credits/grade/delete, then name */
  .course { grid-template-columns: 1fr 78px 92px 36px; grid-template-areas: "name name name name" "code cr gr del"; }
  .course [data-field="name"] { grid-area: name; }
  .course [data-field="code"] { grid-area: code; }
  .course [data-field="credits"] { grid-area: cr; }
  .course [data-field="grade"] { grid-area: gr; }
  .course .icon-btn { grid-area: del; }
  .course { padding-bottom: 8px; border-bottom: 1px dashed var(--line); }
  .sem-stats { width: 100%; order: 3; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
  .g-line { stroke-dashoffset: 0; }
}
