:root {
  --bg: #1a1a2e;
  --card: #16213e;
  --accent: #0f3460;
  --accent-2: #1a4a7a;
  --danger: #e94560;
  --success: #2ecc71;
  --warn: #f39c12;
  --text: #eaeaea;
  --text-dim: #9aa0b5;
  --border: #2a2f4a;
  --radius: 12px;
  --tap: 48px;
  --maxw: 480px;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

#app {
  max-width: var(--maxw);
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 14px;
  overflow-y: auto;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

[hidden] { display: none !important; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
  min-height: var(--tap);
}
.topbar h1 {
  font-size: 1.15rem;
  flex: 1;
}
.q-indicator {
  flex: 1;
  text-align: center;
  font-weight: 600;
  color: var(--text-dim);
}
.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--warn);
  background: var(--card);
  padding: 6px 10px;
  border-radius: 8px;
}
.timer.low { color: var(--danger); }

/* Buttons */
button {
  font-family: inherit;
  font-size: 1rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  min-height: var(--tap);
  transition: filter 0.15s, transform 0.05s;
  -webkit-user-select: none;
  user-select: none;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  padding: 0 18px;
}
.btn-secondary {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0 18px;
}
.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  padding: 0 18px;
}
.btn-icon {
  background: transparent;
  color: var(--text);
  font-size: 1.25rem;
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.full { width: 100%; }

/* Login */
#screen-login {
  justify-content: center;
  align-items: center;
  background: var(--bg);
}
.login-card {
  background: var(--card);
  border-radius: 18px;
  padding: 32px 24px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.35);
}
.login-card h1 { font-size: 1.6rem; margin-bottom: 4px; }
.subtitle { color: var(--text-dim); margin-bottom: 24px; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-password {
  width: 100%;
  min-height: var(--tap);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 14px;
  font-size: 1rem;
}
.error { color: var(--danger); font-size: 0.9rem; }

/* Dashboard cards */
.overall-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
}
.overall-card .score {
  font-size: 1.8rem;
  font-weight: 700;
}
.overall-card .label { color: var(--text-dim); font-size: 0.85rem; }
.overall-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 12px;
}
.overall-grid .cell {
  background: var(--bg);
  border-radius: 8px;
  padding: 10px;
  text-align: center;
}
.overall-grid .num { font-size: 1.2rem; font-weight: 700; }
.overall-grid .cap { font-size: 0.7rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }

.today-plan {
  background: var(--accent);
  border-radius: var(--radius);
  padding: 14px;
  border-left: 4px solid var(--danger);
}
.today-plan .day-title { font-weight: 700; margin-bottom: 6px; }
.today-plan ul { list-style: none; padding-left: 4px; }
.today-plan li { color: var(--text); margin: 4px 0; font-size: 0.92rem; }
.today-plan li::before { content: "▸ "; color: var(--danger); }

.paper-cards { display: flex; flex-direction: column; gap: 10px; }
.paper-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
}
.paper-card .pc-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 8px;
}
.paper-card .pc-title { font-weight: 600; }
.paper-card .pc-stats { font-size: 0.8rem; color: var(--text-dim); }
.progress-bar {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  margin: 8px 0;
}
.progress-bar .fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-2), var(--danger));
  border-radius: 4px;
  transition: width 0.3s;
}
.paper-card .pc-actions { display: flex; gap: 8px; margin-top: 8px; }
.paper-card .pc-actions button { flex: 1; font-size: 0.9rem; min-height: 42px; }

.quick-stats {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
}
.quick-stats .qs-title { font-weight: 600; margin-bottom: 8px; }
.quick-stats .qs-row {
  display: flex; justify-content: space-between;
  padding: 4px 0; font-size: 0.9rem;
}
.quick-stats .qs-row span:last-child { color: var(--text-dim); }

/* Test mode */
.section-label {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  align-self: flex-start;
}
.section-label.reasoning { background: #3a3f7a; color: #c8ccff; }
.section-label.gk { background: #1a4a3a; color: #c8ffcc; }
.section-label.english { background: #4a3a1a; color: #ffe8c8; }

.question-text {
  font-size: 1.05rem;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.figure-area {
  background: #fff;
  color: #111;
  border-radius: var(--radius);
  padding: 12px;
  overflow-x: auto;
  margin: 4px 0;
}
.figure-area table { border-collapse: collapse; margin: 0 auto; }
.figure-area th, .figure-area td {
  border: 1px solid #333;
  padding: 6px 12px;
  text-align: center;
}
.figure-area img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  display: block;
  margin: 4px auto;
}
.view-larger {
  display: block;
  margin: 6px auto 0;
  font-size: 0.8rem;
  color: var(--accent-2);
  background: transparent;
  border: none;
  min-height: auto;
  text-decoration: underline;
}

.options { display: flex; flex-direction: column; gap: 8px; }
.option {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-height: var(--tap);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.option.selected { border-color: var(--accent-2); background: #1a2a4a; }
.option.correct { border-color: var(--success); background: rgba(46,204,113,0.15); }
.option.wrong { border-color: var(--danger); background: rgba(233,69,96,0.15); }
.option.disabled { cursor: default; opacity: 0.85; }
.option .opt-letter {
  flex-shrink: 0;
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.9rem;
}
.option.correct .opt-letter { background: var(--success); color: #062; }
.option.wrong .opt-letter { background: var(--danger); color: #fff; }
.option .opt-text { flex: 1; word-break: break-word; }

.feedback {
  border-radius: var(--radius);
  padding: 12px;
  font-weight: 600;
}
.feedback.correct { background: rgba(46,204,113,0.15); color: var(--success); border: 1px solid var(--success); }
.feedback.wrong { background: rgba(233,69,96,0.15); color: var(--danger); border: 1px solid var(--danger); }

.explanation-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px;
  border-left: 4px solid var(--accent-2);
}
.explanation-card .ex-label {
  font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.5px;
  color: var(--text-dim); margin-bottom: 6px;
}
.explanation-text { white-space: pre-wrap; word-break: break-word; line-height: 1.5; }

.test-actions { display: flex; flex-direction: column; gap: 8px; margin-top: auto; }

/* Results */
#results-content { display: flex; flex-direction: column; gap: 14px; }
.results-score {
  background: var(--card); border-radius: var(--radius); padding: 20px; text-align: center;
}
.results-score .big { font-size: 2.4rem; font-weight: 700; }
.results-score .sub { color: var(--text-dim); }
.section-breakdown { background: var(--card); border-radius: var(--radius); padding: 14px; }
.section-breakdown .sb-row { margin: 10px 0; }
.section-breakdown .sb-head {
  display: flex; justify-content: space-between; font-size: 0.9rem; margin-bottom: 4px;
}
.section-bar { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.section-bar .fill { height: 100%; background: var(--accent-2); border-radius: 4px; }

/* Chat */
#screen-chat { padding: 0; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.45;
}
.bubble.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.bubble.assistant { align-self: flex-start; background: var(--card); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.assistant.streaming::after {
  content: "▋"; animation: blink 0.8s infinite; color: var(--text-dim);
}
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0} }
.chat-input {
  display: flex;
  gap: 8px;
  padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
}
#chat-input {
  flex: 1;
  min-height: var(--tap);
  border-radius: 24px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  padding: 0 16px;
  font-size: 1rem;
}

/* Modals */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-content {
  background: var(--card);
  border-radius: var(--radius);
  max-width: 100%; max-height: 90vh;
  width: 100%; overflow: hidden;
  display: flex; flex-direction: column;
}
.modal-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px; border-bottom: 1px solid var(--border);
}
.modal-body { padding: 14px; overflow-y: auto; white-space: pre-wrap; line-height: 1.55; }
.modal-body h1, .modal-body h2, .modal-body h3 { margin: 10px 0 6px; }
.modal-body table { border-collapse: collapse; width: 100%; margin: 8px 0; }
.modal-body th, .modal-body td { border: 1px solid var(--border); padding: 6px 8px; text-align: left; }

.figure-modal { background: rgba(0,0,0,0.92); }
.figure-modal img { max-width: 95%; max-height: 95%; object-fit: contain; border-radius: 8px; }
.figure-close {
  position: absolute; top: 16px; right: 16px;
  background: rgba(255,255,255,0.15); color: #fff;
  width: 44px; height: 44px; border-radius: 50%;
  min-height: auto; font-size: 1.2rem;
}

/* Utility */
.dim { color: var(--text-dim); }
.muted { font-size: 0.85rem; color: var(--text-dim); }
.spinner {
  width: 18px; height: 18px; border: 2px solid var(--border);
  border-top-color: var(--text); border-radius: 50%;
  animation: spin 0.7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Hide scrollbars subtly on webkit */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
