:root {
  --bg: #f6f7f9; --card: #fff; --text: #1d2330; --muted: #5d6677; --accent: #2f6fde;
  --accent-text: #fff; --ok: #1f8a4c; --warn: #b26b00; --err: #c0392b; --border: #dfe3ea;
  --skeleton: #e9edf3;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14171c; --card: #1e232b; --text: #e8ecf2; --muted: #9aa4b5; --accent: #5b91f0;
          --border: #313846; --ok: #4cc27f; --warn: #e3a447; --err: #ef6b5c; --skeleton: #2a303a; }
}
* { box-sizing: border-box; }
/* Our display rules must never override the hidden attribute. */
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text);
       font: 17px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
main { max-width: 560px; margin: 0 auto; padding: 8px 16px calc(16px + env(safe-area-inset-bottom)); }

/* Header */
.top { display: flex; align-items: center; gap: 8px; min-height: 52px; }
.top h1 { margin: 0; font-size: 26px; flex: 1; }
button.icon { width: 44px; height: 44px; margin: 0; padding: 0; font-size: 24px; border-radius: 50%;
              background: transparent; color: var(--text); }
button.chip { display: inline-block; width: auto; margin: 0; padding: 6px 12px; font-size: 14px;
              font-weight: 500; border-radius: 999px; background: var(--card); color: var(--muted);
              border: 1px solid var(--border); }

/* Controls */
.field { display: block; margin: 12px 0; font-weight: 600; }
select, textarea { width: 100%; font: inherit; padding: 12px; border-radius: 12px; margin-top: 6px;
                   border: 1px solid var(--border); background: var(--card); color: var(--text); }
button, .photo-btn { display: block; width: 100%; padding: 15px; margin: 12px 0; border: 0;
  border-radius: 14px; background: var(--accent); color: var(--accent-text); font: inherit;
  font-weight: 600; text-align: center; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.photo-btn.big { padding: 26px 15px; font-size: 20px; margin-top: 20px; }
button.secondary, .photo-btn.secondary { background: var(--card); color: var(--accent);
  border: 1px solid var(--accent); }
button.link, label.link { display: block; width: auto; margin: 10px auto; padding: 8px; background: none;
  color: var(--accent); font-weight: 500; text-align: center; cursor: pointer; }
button:disabled { opacity: .5; cursor: default; }

/* Start screen */
.steps { display: flex; gap: 6px; padding: 0; margin: 8px 0 0; list-style: none; counter-reset: s; }
.steps li { flex: 1; counter-increment: s; font-size: 14px; color: var(--muted); text-align: center;
            background: var(--card); border: 1px solid var(--border); border-radius: 12px; padding: 8px 4px; }
.steps li::before { content: counter(s); display: block; font-weight: 700; font-size: 18px; color: var(--accent); }
.tip, .privacy, .hint-small { color: var(--muted); font-size: 14px; text-align: center; }

/* Photos */
.preview { display: block; width: 100%; max-height: 62vh; object-fit: contain; border-radius: 12px;
           border: 1px solid var(--border); background: var(--card); margin: 8px 0; }
.thumb-small { display: block; width: 100px; height: 100px; object-fit: cover; border-radius: 8px;
               border: 1px solid var(--border); margin-top: 8px; cursor: zoom-in; }
.thumb-small.big { width: 100%; height: auto; cursor: zoom-out; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--border); border-radius: 14px;
        padding: 14px; margin: 12px 0; overflow-wrap: anywhere; }
.card.ok { border-left: 6px solid var(--ok); }
.card.warn { border-left: 6px solid var(--warn); }
.card.err { border-left: 6px solid var(--err); }
.card h3 { margin: 0 0 6px; font-size: 19px; }
.card p { margin: 6px 0; }
.card .kind { color: var(--muted); font-size: 14px; }
.card button.link { margin: 6px 0 0; padding: 4px 0; text-align: left; }
details.card summary { cursor: pointer; color: var(--accent); font-weight: 600; }
ol#transcription { margin: 8px 0 0; padding-left: 28px; }
ol li.flag { background: color-mix(in srgb, var(--err) 16%, transparent); border-radius: 4px; font-weight: 600; }

/* Wait */
.status p { margin: 8px 0 0; text-align: center; color: var(--muted); }
.bar { height: 8px; background: var(--skeleton); border-radius: 4px; overflow: hidden; }
#barFill { height: 100%; width: 0; background: var(--accent); border-radius: 4px; transition: width .8s linear; }
.skeleton { color: var(--muted); background: linear-gradient(90deg, var(--skeleton) 25%, var(--card) 50%, var(--skeleton) 75%);
            background-size: 200% 100%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* Chips */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0; }
.chips button { display: inline-block; width: auto; margin: 0; padding: 9px 14px; font-size: 15px;
                font-weight: 500; border-radius: 999px; background: var(--card); color: var(--accent);
                border: 1px solid var(--accent); }

/* Chat */
h2 { font-size: 20px; margin: 8px 0; }
#chatLog { display: flex; flex-direction: column; gap: 8px; margin: 8px 0; }
.msg { max-width: 88%; padding: 10px 12px; border-radius: 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.msg.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--border); }
.msg.pending { color: var(--muted); font-style: italic; }
.msg ul, .msg ol { margin: 4px 0; padding-left: 20px; white-space: normal; }
.msg p { margin: 0 0 6px; }
.msg.streaming::after { content: "▍"; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
#chatForm { display: flex; gap: 8px; align-items: flex-end; position: sticky; bottom: 0;
            background: var(--bg); padding: 8px 0 calc(8px + env(safe-area-inset-bottom)); }
#chatForm textarea { flex: 1; margin: 0; }
#chatSend { width: 52px; margin: 0; padding: 12px 0; font-size: 18px; }

.error { color: var(--err); font-weight: 600; text-align: center; }
.toast { position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
         background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 15px;
         max-width: 90vw; text-align: center; z-index: 10; }

/* Helpline card (app/safety.py) */
.helpline { border-left: 4px solid #2f9e6b; }
.helpline-call { display: block; margin: .4rem 0; padding: .7rem 1rem; border-radius: 12px; background: #2f9e6b;
                 color: #fff; font-weight: 700; text-align: center; text-decoration: none; font-size: 1.1rem; }

/* Invite gate: while locked, only the code box is shown on the start screen. */
#start.locked > :not(#inviteBox) { display: none !important; }
#inviteBox input { width: 100%; font-size: 1.2rem; padding: .6rem; margin: .5rem 0; box-sizing: border-box; }

/* Wide formulas scroll inside their box instead of dragging the whole page sideways. */
.katex-display { overflow-x: auto; overflow-y: hidden; max-width: 100%; }
.msg, .card { overflow-wrap: anywhere; min-width: 0; }

/* "Is this the problem?" asked before the verdict when the problem was guessed from the work */
.confirm-text { font-size: 1.15rem; font-weight: 600; }
#confirmProblem .row { display: flex; gap: 8px; }
#confirmProblem .row button { flex: 1; }

/* Crop on the preview: drag the corners around one problem (the default box is the whole photo) */
.crop-wrap { position: relative; width: fit-content; max-width: 100%; margin: 0 auto; overflow: hidden;
             border-radius: 12px; touch-action: none; user-select: none; -webkit-user-select: none; }
.crop-wrap img { display: block; max-width: 100%; max-height: 62vh; }
.crop-box { position: absolute; outline: 2px solid #fff; box-shadow: 0 0 0 9999px rgba(0, 0, 0, .45); }
.crop-box i { position: absolute; width: 34px; height: 34px; border: 0 solid #fff; }
.crop-box i[data-c="nw"] { left: 0; top: 0; border-width: 5px 0 0 5px; }
.crop-box i[data-c="ne"] { right: 0; top: 0; border-width: 5px 5px 0 0; }
.crop-box i[data-c="sw"] { left: 0; bottom: 0; border-width: 0 0 5px 5px; }
.crop-box i[data-c="se"] { right: 0; bottom: 0; border-width: 0 5px 5px 0; }
.crop-hint { text-align: center; margin: 6px 0 0; }

/* Diagrams the tutor draws (```svg in a reply, shown as an image) */
.diagram { display: block; width: 100%; max-width: 420px; height: auto; margin: 8px 0; background: #fff;
           border-radius: 10px; border: 1px solid var(--border); }
.diagram-pending { display: block; padding: 24px; text-align: center; color: var(--muted, #888);
                   background: var(--skeleton); border-radius: 10px; margin: 8px 0; }

/* "Stuck" card: full explanation after a failed fix */
.card.struggle { border-left: 6px solid var(--accent); }
.card.struggle .pending { color: var(--muted); }

/* Confidence (before a check / a practice answer) */
.confidence { margin: 10px 0 0; }
.confidence .chips, #practiceConf { justify-content: center; }
.chips button.on { background: var(--accent); color: #fff; }
/* Review on the start screen + practice */
.card.review { border-left: 6px solid var(--accent); }
.card.review button { margin: 0; }
#mistakes { margin-top: 8px; font-size: 15px; }
#mistakeList { padding-left: 18px; margin: 6px 0 0; }
#mistakeList li { margin: 6px 0; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; margin-left: 6px;
         background: var(--skeleton); color: var(--text); }
.badge.retained { background: var(--ok); color: #fff; }
.badge.fixed { background: var(--warn); color: #111; }
#practiceAnswer { width: 100%; font-size: 20px; padding: 12px; border-radius: 10px; border: 1px solid var(--border);
                  background: var(--card); color: var(--text); box-sizing: border-box; margin: 6px 0; }

.card.review h3 { margin-bottom: 8px; }
.card.review button { margin: 6px 0; }
#examTopics { margin: 6px 0; }
#mistakeList li { cursor: pointer; }
.options { display: grid; gap: 8px; margin: 8px 0; }
.options button { margin: 0; text-align: left; background: var(--card); color: var(--text);
                  border: 1px solid var(--border); font-weight: 500; }
.options button.on { border: 2px solid var(--accent); }
.options button { padding: 10px 14px; }
.options button p { margin: 0; }
