/* =====================================================================
 * Student Library — navy theme with an amber accent (same colour family).
 * Every colour, size and spacing value is a token below: change it here.
 * ===================================================================== */
:root {
  /* palette: navy family + one contrasting amber accent */
  --navy-950: #0a1222;
  --navy-900: #14213d;           /* base */
  --navy-800: #1b2d52;           /* cards, toolbars */
  --navy-700: #24396b;           /* inputs, secondary buttons */
  --amber: #fca311;              /* accent (contrast, same family) */
  --amber-soft: #ffd27a;
  --text: #e9eef8;
  --text-soft: #a9b6d0;
  --line: rgba(233, 238, 248, 0.10);
  --danger: #ff7a7a;
  --page-bg: #5a6a8c;

  /* geometry */
  --topbar-h: 56px;
  --toolbar-h: 56px;
  --tap: 44px;                   /* Apple HIG minimum touch target */
  --swatch: 28px;
  --radius: 14px;
  --radius-lg: 20px;
  --gap: 8px;
  --page-margin: 12px;
  --page-max-w: 920px;           /* widest displayed PDF page */
  --library-max-w: 1280px;
  --card-min-w: 300px;           /* library grid column width */
  --modal-max-w: 520px;

  /* motion: critically damped feel (no overshoot) */
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
  --dur: 240ms;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Vazirmatn", Tahoma, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

/* Fullscreen app: the page itself never scrolls or bounces; only the named scrollers do. */
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  position: fixed;
  inset: 0;
  overscroll-behavior: none;
  background: var(--navy-900);
  color: var(--text);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
input, textarea { -webkit-user-select: text; user-select: text; }
.hidden { display: none !important; }

.view {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  /* --tg-*: space Telegram reserves for its own fullscreen controls (Close, ⌄ ⋯); set by app.js */
  padding:
    calc(env(safe-area-inset-top) + var(--tg-top, 0px))
    calc(env(safe-area-inset-right) + var(--tg-right, 0px))
    calc(env(safe-area-inset-bottom) + var(--tg-bottom, 0px))
    calc(env(safe-area-inset-left) + var(--tg-left, 0px));
  background: var(--navy-900);
}

/* ---------- top bar: translucent material ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: var(--gap);
  height: var(--topbar-h);
  padding: 0 12px;
  flex: none;
  background: rgba(20, 33, 61, 0.82);
  backdrop-filter: blur(18px) saturate(160%);
  -webkit-backdrop-filter: blur(18px) saturate(160%);
  border-bottom: 1px solid var(--line);
  z-index: 2;
}
.topbar h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.grow-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }
.save-status { font-size: 12px; color: var(--text-soft); white-space: nowrap; }

.icon-btn, .tool-btn {
  min-width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0 10px;
  transition: background var(--dur) var(--ease-out), transform 100ms ease-out;
}
.tool-btn { font-size: 15px; }
.icon-btn:active, .tool-btn:active { transform: scale(0.96); background: rgba(233, 238, 248, 0.12); }
.tool-btn:disabled { opacity: 0.4; }
.tool-btn.active { background: var(--amber); color: var(--navy-900); font-weight: 600; }

/* ---------- library: responsive card grid (phone → tablet → laptop) ---------- */
.library {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--card-min-w)), 1fr));
  gap: 14px;
  align-content: start;
  max-width: var(--library-max-w);
  width: 100%;
  margin: 0 auto;
}
.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-soft);
  margin-top: 18vh;
  line-height: 2;
  white-space: pre-line;
}
.term-title {
  grid-column: 1 / -1;
  margin: 10px 4px 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--amber-soft);
}
.course {
  background: var(--navy-800);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: hidden;
  align-self: start;
}
.course-head { display: flex; align-items: center; padding: 8px 8px 8px 14px; font-weight: 600; }
.course-head .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 60px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  cursor: pointer;
  transition: background var(--dur) var(--ease-out);
}
.item:active { background: rgba(233, 238, 248, 0.06); }
.item .icon { font-size: 22px; }
.item .meta { flex: 1; min-width: 0; }
.item .title { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { font-size: 12px; color: var(--text-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item.sub { color: var(--text-soft); font-size: 13px; }

/* ---------- editor ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 12px;
  min-height: var(--toolbar-h);
  padding: 6px 10px;
  flex: none;
  background: rgba(27, 45, 82, 0.9);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--line);
}
.tool-group { display: flex; align-items: center; gap: 4px; }
.pager { margin-inline-start: auto; }
#page-indicator { min-width: 40px; text-align: center; font-variant-numeric: tabular-nums; color: var(--text-soft); }
.swatch {
  width: var(--swatch);
  height: var(--swatch);
  border-radius: 50%;
  border: 2px solid transparent;
  padding: 0;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25);
}
.swatch.active { border-color: var(--amber); outline: 2px solid var(--navy-900); outline-offset: -5px; }
.width-btn {
  width: var(--tap);
  height: var(--tap);
  border: 0;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  cursor: pointer;
}
.width-btn.active { background: rgba(252, 163, 17, 0.25); }
.width-dot { background: var(--text); border-radius: 50%; }

.page-scroller {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: var(--navy-950);
  padding: var(--page-margin);
  scrollbar-width: thin;
}
.page-wrap {
  position: relative;
  margin: 0 auto;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
.page-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
/* Stylus / finger: the ink layer owns the gesture; the scroller handles finger panning in JS. */
#ink-canvas { touch-action: none; cursor: crosshair; }

/* ---------- sheets (bottom on phones, centred card on tablets/laptops) ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(5, 10, 20, 0.55);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  animation: fade-in var(--dur) var(--ease-out);
}
.sheet {
  width: 100%;
  max-width: var(--modal-max-w);
  max-height: 85dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--navy-800);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: sheet-up 280ms var(--ease-out);
}
.sheet h2 { margin: 0; font-size: 17px; font-weight: 600; }
.sheet .sub { color: var(--text-soft); }
.sheet input, .sheet textarea {
  width: 100%;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid rgba(233, 238, 248, 0.14);
  background: var(--navy-700);
  color: var(--text);
  font-size: 16px; /* 16px stops iOS from zooming on focus */
  font-family: var(--font);
  outline: none;
}
.sheet input:focus, .sheet textarea:focus { border-color: var(--amber); }
.sheet textarea { min-height: 100px; resize: vertical; }
.sheet .answer, .sheet pre {
  white-space: pre-wrap;
  background: var(--navy-700);
  padding: 12px;
  border-radius: var(--radius);
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.8;
}
.row { display: flex; gap: var(--gap); }
.btn {
  flex: 1;
  min-height: 48px;
  padding: 12px;
  border: 0;
  border-radius: var(--radius);
  background: var(--navy-700);
  color: var(--text);
  font-size: 15px;
  font-family: var(--font);
  cursor: pointer;
  transition: transform 100ms ease-out, background var(--dur) var(--ease-out);
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--amber); color: var(--navy-900); font-weight: 600; }
.btn.danger { color: var(--danger); }
.picker-grid { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap); }
.picker-grid .btn { padding: 10px; font-size: 14px; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(10, 18, 34, 0.94);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 12px 18px;
  border-radius: var(--radius);
  font-size: 14px;
  z-index: 20;
  max-width: 90vw;
  text-align: center;
  animation: fade-in var(--dur) var(--ease-out);
}

/* ---------- tablets (portrait & landscape) ---------- */
@media (min-width: 600px) {
  :root { --page-margin: 20px; --card-min-w: 320px; }
  .library { padding: 20px; gap: 18px; }
  .modal { align-items: center; }
  .sheet { border-radius: var(--radius-lg); max-height: 80dvh; animation: pop-in 260ms var(--ease-out); }
}

/* ---------- laptops and large screens ---------- */
@media (min-width: 1024px) {
  :root { --topbar-h: 60px; --card-min-w: 340px; }
  .library { padding: 28px 32px; gap: 20px; }
  .topbar { padding: 0 20px; }
  .toolbar { padding: 8px 20px; }
  .page-scroller { padding: 32px; }
}

/* ---------- short landscape screens (phones and tablets sideways) ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --topbar-h: 48px; --toolbar-h: 48px; --tap: 40px; }
}

/* ---------- motion & transparency preferences ---------- */
@keyframes sheet-up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes pop-in { from { transform: scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (prefers-reduced-transparency: reduce) {
  .topbar, .toolbar, .modal { backdrop-filter: none; -webkit-backdrop-filter: none; }
  .topbar, .toolbar { background: var(--navy-900); }
}
