*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #090b12;
  --surface: #141926;
  --surface2: #1b2233;
  --border: #34415f;
  --text: #f7f8ff;
  --muted: #a8b0c6;
  --accent: #8f7bff;
  --accent2: #6f5bff;
  --green: #49d17d;
  --red: #ff5f66;
  --radius: 14px;
}

body {
  background:
    radial-gradient(900px 420px at 50% -220px, rgba(143,123,255,.25), transparent 60%),
    radial-gradient(650px 380px at 100% 0%, rgba(74,163,255,.16), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 17px;
  line-height: 1.5;
  max-width: 640px;
  min-height: 100dvh;
  margin: 0 auto;
  -webkit-tap-highlight-color: transparent;
}

.screen { display: none; padding: 24px 20px 80px; }
.screen.active { display: block; }
.offline-banner {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 50;
  padding: 8px 16px;
  background: #b83f46;
  color: #fff;
  text-align: center;
  font-size: 13px;
}
body.has-offline-banner .screen { padding-top: 64px; }

.home-header, .header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 24px;
}
.home-logo { flex: 0 0 auto; }
.brand-sm {
  flex: 1;
  text-align: center;
  font-family: "Playfair Display", Georgia, serif;
  font-size: 24px;
  font-weight: 700;
}
.header-title {
  flex: 1;
  min-width: 0;
  font-size: 22px;
  font-weight: 800;
  line-height: 1.25;
}
.back-btn {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  font-size: 34px;
  line-height: 1;
  padding: 2px 6px;
}
.icon-btn {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #1f2740;
  border: 1px solid #43537a;
  border-radius: 10px;
  color: #edf0ff;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
}

.btn {
  display: block;
  width: 100%;
  min-height: 56px;
  padding: 15px 18px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  text-align: center;
  transition: transform .08s ease, filter .12s ease;
}
.btn:active { transform: translateY(1px); filter: brightness(.92); }
.btn-primary {
  background: linear-gradient(135deg, #9a88ff 0%, #7c6af7 58%, #6f5bff 100%);
  border-color: #a799ff;
  box-shadow: 0 10px 24px rgba(124,106,247,.35);
}
.btn-secondary {
  background: linear-gradient(180deg, #242d44 0%, #1c2438 100%);
  border-color: #425174;
  color: #f7f8ff;
}
.btn-red {
  margin-top: 10px;
  background: linear-gradient(180deg, #d4535a 0%, #a33640 100%);
  border-color: #ff7f86;
}
.home-actions { display: grid; gap: 12px; }
.owner-panel { display: grid; gap: 8px; margin-bottom: 16px; }
.viewer-note, .muted-line, .empty-state {
  color: var(--muted);
  font-size: 14px;
}
.viewer-note {
  margin-bottom: 14px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(20,25,38,.78);
}
.toolbar-row { margin-bottom: 12px; }

.topic-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 18px 20px;
  background: linear-gradient(135deg, #9a88ff 0%, #7f6cf7 60%, #705cff 100%);
  border: 1px solid #a799ff;
  border-radius: var(--radius);
  color: #fff;
  box-shadow: 0 12px 28px rgba(124,106,247,.28);
  cursor: pointer;
  font: inherit;
  text-align: left;
}
.topic-name {
  flex: 1;
  min-width: 0;
  font-size: 19px;
  font-weight: 850;
  overflow-wrap: anywhere;
}
.topic-count {
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,.18);
  font-size: 12px;
  font-weight: 800;
}

.cards-list { display: grid; gap: 10px; margin-top: 14px; }
.card-row {
  display: grid;
  grid-template-columns: 74px 1fr;
  gap: 12px;
  align-items: center;
  min-height: 88px;
  padding: 10px;
  border: 1px solid #364767;
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(29,36,54,.96), rgba(21,27,40,.96));
  color: var(--text);
  cursor: pointer;
  font: inherit;
  text-align: left;
}
.card-thumb {
  width: 74px;
  height: 68px;
  object-fit: cover;
  border-radius: 10px;
  background: var(--surface2);
}
.card-title { font-weight: 800; overflow-wrap: anywhere; }
.card-meta { color: var(--muted); font-size: 13px; margin-top: 3px; }

.form-group { margin-bottom: 18px; }
.form-label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .7px;
  text-transform: uppercase;
}
.input {
  width: 100%;
  padding: 13px 15px;
  background: linear-gradient(180deg, #20283a 0%, #1a2233 100%);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  outline: none;
  font: inherit;
  font-size: 16px;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(143,123,255,.2); }
.textarea { min-height: 72px; overflow: hidden; resize: none; }
.add-img-label {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 7px 13px;
  border: 1px solid #3e4d6f;
  border-radius: 999px;
  color: #d9def0;
  cursor: pointer;
  font-size: 14px;
  font-weight: 750;
}
.image-shell, .occlusion-shell, .study-image-shell {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface2);
}
.image-shell { margin-bottom: 10px; }
.image-shell img, .occlusion-shell img, .study-image-shell img {
  display: block;
  width: 100%;
  height: auto;
  user-select: none;
}
.image-remove {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 999px;
  background: rgba(0,0,0,.72);
  color: #fff;
  cursor: pointer;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.segmented.mini { grid-template-columns: repeat(3, 1fr); margin-bottom: 10px; }
.segment {
  min-height: 42px;
  padding: 8px 10px;
  border: 1px solid #3e4d6f;
  border-radius: 10px;
  background: #151d2e;
  color: #d2d8e7;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  font-weight: 800;
}
.segment.active {
  border-color: #a799ff;
  background: rgba(124,106,247,.34);
  color: #fff;
}

.occlusion-shell { cursor: crosshair; touch-action: none; }
.occl-box {
  position: absolute;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(255,255,255,.78);
  border-radius: 9px;
  background: #000;
  box-shadow: 0 4px 14px rgba(0,0,0,.35);
  touch-action: none;
}
.occl-box.dot {
  width: var(--dot-size, 22px) !important;
  height: var(--dot-size, 22px) !important;
  border-radius: 999px;
  border: 2px solid #fff;
  background: #ff3d71;
  box-shadow: 0 2px 10px rgba(255,61,113,.7);
}
.occl-box.dot .occl-drag { color: #fff; font-size: 12px; font-weight: 950; text-shadow: 0 1px 2px #581028; }
.occl-arrow {
  position: absolute;
  z-index: 3;
  width: 56px;
  height: 4px;
  border-radius: 999px;
  --arrow-color: #ef3340;
  --arrow-label-color: #fff;
  background: var(--arrow-color);
  box-shadow: 0 1px 4px rgba(0,0,0,.55);
  transform-origin: left center;
  touch-action: none;
}
.occl-arrow::after {
  content: '';
  position: absolute;
  right: -2px;
  top: 50%;
  width: 12px;
  height: 12px;
  border-top: 4px solid var(--arrow-color);
  border-right: 4px solid var(--arrow-color);
  transform: translateY(-50%) rotate(45deg);
}
.occl-arrow .occl-drag { inset: auto; left: -12px; top: -11px; width: 24px; height: 24px; border-radius: 99px; background: var(--arrow-color); color: var(--arrow-label-color); font-size: 11px; }
.occl-arrow.arrow-red { --arrow-color: #ef3340; --arrow-label-color: #fff; }
.occl-arrow.arrow-black { --arrow-color: #000; --arrow-label-color: #fff; }
.occl-arrow.arrow-white { --arrow-color: #fff; --arrow-label-color: #111; }
.occl-rotate {
  position: absolute;
  right: -13px;
  top: 50%;
  width: 26px;
  height: 26px;
  transform: translateY(-50%);
  border: 2px solid #fff;
  border-radius: 999px;
  background: var(--arrow-color);
  cursor: grab;
  touch-action: none;
}
.occl-rotate:active { cursor: grabbing; }
.study-arrow { pointer-events: none; }
.card-row-actions { display: flex; align-items: stretch; gap: 8px; }
.card-row-actions .card-row { flex: 1; }
.personalize-btn {
  width: 44px;
  flex: 0 0 44px;
  border: 1px solid rgba(167,153,255,.55);
  border-radius: 12px;
  background: rgba(124,106,247,.16);
  color: #fff;
  font-size: 22px;
  font-weight: 900;
  cursor: pointer;
}
.occl-drag {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 0;
  background: transparent;
  color: #fff;
  cursor: move;
  font-weight: 900;
}
.occl-resize {
  position: absolute;
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-radius: 999px;
  background: var(--accent);
}
.occl-resize.nw { top: -9px; left: -9px; cursor: nwse-resize; }
.occl-resize.ne { top: -9px; right: -9px; cursor: nesw-resize; }
.occl-resize.sw { bottom: -9px; left: -9px; cursor: nesw-resize; }
.occl-resize.se { bottom: -9px; right: -9px; cursor: nwse-resize; }
.occlusion-help {
  margin-top: 8px;
  color: var(--muted);
  font-size: 13px;
}
.occlusion-list { display: grid; gap: 8px; margin-top: 10px; }
.occlusion-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
}
.point-size-control { display: flex; align-items: center; gap: 8px; margin-top: 6px; color: var(--muted); font-size: 12px; font-weight: 800; }
.point-size-control input { flex: 1; accent-color: #ff3d71; }
.point-size-control .arrow-color-select { flex: 1; min-height: 34px; padding: 5px 8px; }
.badge {
  min-width: 30px;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(143,123,255,.28);
  color: #e8e3ff;
  text-align: center;
  font-size: 13px;
  font-weight: 900;
}
.small-danger {
  width: 34px;
  height: 34px;
  border: 1px solid rgba(255,95,102,.7);
  border-radius: 10px;
  background: rgba(255,95,102,.18);
  color: #ffd9dc;
  cursor: pointer;
}

.study-card {
  margin-bottom: 14px;
  padding: 14px;
  border: 1px solid #364767;
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(29,36,54,.96), rgba(21,27,40,.96));
}
.card-group { display: grid; gap: 8px; margin: 16px 0 22px; }
.card-group h3 { margin: 0; color: var(--muted); font-size: 13px; letter-spacing: .7px; text-transform: uppercase; }
.study-label {
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .7px;
  text-transform: uppercase;
}
.study-text {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 18px;
}
.occlusion-answer-grid {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}
.occlusion-answer-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
}
.answer-diff { min-height: 18px; margin-top: 4px; font-size: 12px; }
.answer-diff-extra, .answer-diff-missing { color: #ff7179; font-weight: 800; }
.answer-diff-extra { text-decoration: underline; }
.answer-diff-correct { color: var(--green); font-weight: 800; }
.occlusion-answer-input.is-correct {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(73,209,125,.16);
}
.occlusion-answer-input.is-wrong {
  border-color: var(--red);
  box-shadow: 0 0 0 3px rgba(255,95,102,.16);
}
.study-footer {
  position: sticky;
  bottom: 0;
  display: grid;
  gap: 10px;
  padding: 12px 0;
  background: linear-gradient(transparent, var(--bg) 18%);
}
.study-ratings { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.rating-perfect { background: rgba(73,209,125,.18); border: 1px solid rgba(73,209,125,.7); color: #d9ffe6; }
.rating-again { background: rgba(93,164,255,.18); border: 1px solid rgba(93,164,255,.7); color: #dceaff; }
.rating-review { background: rgba(255,95,102,.18); border: 1px solid rgba(255,95,102,.7); color: #ffe0e2; }
.progress-txt { flex: 1; color: var(--muted); font-size: 14px; font-weight: 800; }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(0,0,0,.62);
}
.modal-box {
  width: min(100%, 420px);
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}
.modal-box h3 { margin-bottom: 14px; }
.modal-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

#lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,.92);
}
#lightbox.active { display: flex; }
#lightbox-img {
  max-width: 96vw;
  max-height: 92vh;
  object-fit: contain;
}
#lightbox-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 999px;
  background: rgba(255,255,255,.14);
  color: #fff;
  cursor: pointer;
}

[hidden] { display: none !important; }

@media (max-width: 420px) {
  .screen { padding-inline: 14px; }
  .topic-btn { padding-inline: 16px; }
  .segmented { grid-template-columns: 1fr; }
}

@media (min-width: 900px) {
  body { max-width: 1280px; }
  #screen-study.active:not(.has-answer) #study-question-card,
  #screen-study.active:not(.has-answer) .study-footer {
    width: min(100%, 640px);
    margin-left: auto;
    margin-right: auto;
  }
  #screen-study.active.has-answer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 18px;
    align-items: start;
  }
  #screen-study.has-answer > .header, #screen-study.has-answer > .study-footer { grid-column: 1 / -1; }
  #study-question-card, #study-answer-card { margin-bottom: 0; }
}
