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

:root {
  --bg: #0f172a;
  --bg-elev: #1e293b;
  --bg-elev-2: #334155;
  --text: #f1f5f9;
  --text-muted: #94a3b8;
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --pin-active: #f59e0b;
  --danger: #ef4444;
  --border: #334155;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

body {
  min-height: 100dvh;
}

.app {
  display: grid;
  grid-template-rows: 1fr 1fr auto;
  gap: 12px;
  padding: calc(12px + var(--safe-top)) 12px calc(12px + var(--safe-bottom));
  min-height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

.panel {
  background: var(--bg-elev);
  border-radius: var(--radius);
  padding: 18px 20px;
  display: grid;
  grid-template-rows: auto 1fr auto;
  position: relative;
  box-shadow: var(--shadow);
  min-height: 0;
  overflow: hidden;
  text-align: center;
}

.panel-label {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.panel-text {
  font-size: clamp(22px, 5.5vw, 32px);
  line-height: 1.3;
  font-weight: 500;
  word-wrap: break-word;
  overflow-y: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  border-radius: 10px;
  transition: background 0.15s;
  user-select: none;
}
.panel-text:active { background: rgba(255, 255, 255, 0.05); }
.panel-text.speaking { background: rgba(99, 102, 241, 0.18); }
.panel-text.tts-error { background: rgba(239, 68, 68, 0.18); }

.meta {
  font-size: 12px;
  color: var(--text-muted);
}

.answer-panel .panel-text.hidden-answer { visibility: hidden; }

.reveal-btn {
  position: absolute;
  inset: 40px 16px 16px;
  background: rgba(99, 102, 241, 0.15);
  color: var(--text);
  border: 1px dashed var(--accent);
  border-radius: 10px;
  font-size: 16px;
  cursor: pointer;
}
.reveal-btn:active { background: rgba(99, 102, 241, 0.3); }

.pinned-badge {
  position: absolute;
  top: 14px;
  right: 16px;
  background: var(--pin-active);
  color: #1f2937;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
}

.actions {
  display: grid;
  grid-template-columns: 56px 1fr 56px;
  gap: 12px;
  align-items: stretch;
}

.btn {
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.05s, background 0.15s, opacity 0.15s;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  height: 64px;
  font-size: 18px;
}
.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-elev);
  color: var(--text);
  height: 64px;
}
.btn-secondary:hover { background: var(--bg-elev-2); }

.btn-secondary.pinned {
  background: var(--pin-active);
  color: #1f2937;
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  height: 40px;
  font-size: 14px;
  border-radius: 10px;
}

.btn.wide { width: 100%; height: 52px; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
}
.modal[hidden] { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  display: flex;
  flex-direction: column;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.5);
  animation: slide-up 0.2s ease-out;
}

@media (min-width: 600px) {
  .modal { align-items: center; }
  .modal-content { border-radius: 18px; max-height: 86dvh; }
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 20px; }

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
}
.icon-btn:hover { background: var(--bg-elev); color: var(--text); }

.modal-body {
  padding: 16px 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 12px 20px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
}

.settings-section { margin-bottom: 22px; }

.field-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.select-actions { display: flex; gap: 8px; }

.row { display: flex; gap: 10px; align-items: center; }

.select {
  flex: 1;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 15px;
  font-family: inherit;
}

.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg-elev);
  border-radius: 10px;
  padding: 4px;
  gap: 4px;
}
.seg-item {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
  color: var(--text-muted);
}
.seg-item input { position: absolute; opacity: 0; pointer-events: none; }
.seg-item input:checked + span {
  color: var(--text);
}
.seg-item:has(input:checked) { background: var(--accent); }
.seg-item:has(input:checked) span { color: white; font-weight: 600; }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  cursor: pointer;
  font-size: 15px;
}
.checkbox-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  cursor: pointer;
}

.lessons-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--bg-elev);
  border-radius: 10px;
  padding: 6px 12px;
  max-height: 220px;
  overflow-y: auto;
}
.lessons-list .checkbox-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.lessons-list .checkbox-row:last-child { border-bottom: none; }

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
  font-family: inherit;
}
.link-btn.danger { color: var(--danger); }
.link-btn:hover { text-decoration: underline; }

.pinned-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
}
.pinned-item {
  background: var(--bg-elev);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
}
.pinned-item .pq-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pinned-item .pq-remove {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
}
.pinned-item .pq-remove:hover { color: var(--danger); }

.muted { color: var(--text-muted); font-weight: 400; }

.toast {
  position: fixed;
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: var(--shadow);
  z-index: 100;
  animation: fade 1.6s ease-out forwards;
}
.toast[hidden] { display: none; }

@keyframes fade {
  0% { opacity: 0; transform: translate(-50%, 10px); }
  15%, 85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, 10px); }
}
