/* === Variables === */
:root {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --bg3: #0f3460;
  --accent: #e94560;
  --accent2: #c73e54;
  --text: #eee;
  --text-dim: #999;
  --card-bg: #fff;
  --card-text: #1a1a2e;
  --green: #2ecc71;
  --red: #e74c3c;
  --yellow: #f1c40f;
  --radius: 12px;
  --transition: 0.3s ease;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* === Reset === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* === Screens === */
.screen { display: none; min-height: 100vh; padding: 16px; max-width: 600px; margin: 0 auto; }
.screen.active { display: flex; flex-direction: column; }

/* === Home === */
.app-header { text-align: center; padding: 32px 0 8px; }
.app-header h1 { font-size: 2rem; color: var(--accent); }
.subtitle { color: var(--text-dim); font-size: 0.9rem; margin-top: 4px; }

.home-buttons { display: flex; flex-direction: column; gap: 16px; margin-top: 32px; }
.home-btn {
  display: flex; align-items: center; gap: 16px;
  background: var(--bg2); border-radius: var(--radius);
  padding: 20px; text-decoration: none; color: var(--text);
  transition: transform var(--transition), background var(--transition);
  border: 1px solid transparent;
}
.home-btn:hover, .home-btn:focus { background: var(--bg3); transform: scale(1.02); border-color: var(--accent); }
.home-btn__icon { font-size: 2rem; }
.home-btn__label { font-size: 1.15rem; font-weight: 600; flex: 1; }
.home-btn__count { font-size: 0.85rem; color: var(--text-dim); }

.home-progress { margin-top: 32px; background: var(--bg2); border-radius: var(--radius); padding: 20px; }
.home-progress h3 { font-size: 1rem; margin-bottom: 12px; color: var(--text-dim); }
.progress-row { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.progress-row span:first-child { min-width: 70px; font-size: 0.85rem; color: var(--text-dim); }
.progress-bar { flex: 1; height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.progress-bar__fill { height: 100%; border-radius: 4px; transition: width var(--transition); }
.progress-bar__fill--known { background: var(--green); }
.progress-bar__fill--unknown { background: var(--red); }
.progress-row span:last-child { min-width: 36px; text-align: right; font-size: 0.85rem; }

.stats-link {
  display: block; text-align: center; margin-top: 24px;
  color: var(--text-dim); text-decoration: none; font-size: 0.95rem;
}
.stats-link:hover { color: var(--accent); }

/* === List screens === */
.list-header { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; padding: 8px 0; }
.list-header h2 { flex: 1; font-size: 1.3rem; }
.back-btn {
  color: var(--accent); text-decoration: none; font-size: 0.95rem;
  white-space: nowrap;
}
.back-btn:hover { text-decoration: underline; }

.list-container { display: flex; flex-direction: column; gap: 10px; flex: 1; padding-bottom: 24px; }
.list-item {
  display: block; background: var(--bg2); border-radius: var(--radius);
  padding: 16px; text-decoration: none; color: var(--text);
  transition: background var(--transition); border: 1px solid transparent;
}
.list-item:hover { background: var(--bg3); border-color: var(--accent); }
.list-item__title { font-weight: 600; font-size: 1rem; margin-bottom: 4px; }
.list-item__sub { color: var(--text-dim); font-size: 0.85rem; margin-bottom: 8px; }
.list-item__bar { display: flex; align-items: center; gap: 8px; }
.list-item__bar .progress-bar { flex: 1; }
.list-item__bar span { font-size: 0.8rem; color: var(--text-dim); min-width: 60px; text-align: right; }

/* === Study screen === */
.study-header { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.study-title { flex: 1; font-size: 0.95rem; font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.study-counter { font-size: 0.9rem; color: var(--text-dim); white-space: nowrap; }

.study-progress-bar { height: 4px; background: var(--bg2); border-radius: 2px; margin: 10px 0; }
.study-progress-fill { height: 100%; background: var(--accent); border-radius: 2px; transition: width var(--transition); }

/* Card area */
.card-area {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  perspective: 1000px; min-height: 300px; position: relative;
  touch-action: pan-y; user-select: none; -webkit-user-select: none;
}
.card {
  width: 100%; max-width: 500px; min-height: 280px;
  cursor: pointer; position: relative;
}
.card-inner {
  width: 100%; min-height: 280px; position: relative;
  transition: transform 0.5s; transform-style: preserve-3d;
}
.card-inner.flipped { transform: rotateY(180deg); }
.card-front, .card-back {
  position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--card-bg); color: var(--card-text); border-radius: var(--radius);
  padding: 24px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 1.05rem; line-height: 1.6; overflow-y: auto; text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,0.3);
}
.card-back { transform: rotateY(180deg); background: var(--bg3); color: var(--text); }

.flip-hint { color: var(--text-dim); font-size: 0.8rem; margin-top: 10px; }

/* Study controls */
.study-controls { display: flex; gap: 10px; margin-top: 16px; justify-content: center; align-items: center; }
.ctrl-btn {
  border: none; border-radius: var(--radius); padding: 12px 18px;
  font-size: 1rem; cursor: pointer; transition: transform var(--transition), opacity var(--transition);
  font-weight: 600; color: #fff;
}
.ctrl-btn:hover { transform: scale(1.05); }
.ctrl-btn:active { transform: scale(0.95); }
.ctrl-btn--prev, .ctrl-btn--next { background: var(--bg3); font-size: 1.5rem; padding: 12px 16px; }
.ctrl-btn--known { background: var(--green); }
.ctrl-btn--unknown { background: var(--red); }

.study-toolbar { display: flex; gap: 10px; margin-top: 12px; justify-content: center; }
.tool-btn {
  background: transparent; border: 1px solid var(--bg3); color: var(--text-dim);
  border-radius: 8px; padding: 8px 14px; font-size: 0.85rem; cursor: pointer;
}
.tool-btn:hover { border-color: var(--accent); color: var(--text); }

.filter-menu { display: flex; gap: 8px; justify-content: center; margin-top: 8px; flex-wrap: wrap; }
.filter-menu.hidden { display: none; }
.filter-opt {
  background: var(--bg2); border: 1px solid var(--bg3); color: var(--text-dim);
  border-radius: 20px; padding: 6px 14px; font-size: 0.8rem; cursor: pointer;
}
.filter-opt.active { border-color: var(--accent); color: var(--accent); }

/* === Stats === */
.stats-container { flex: 1; padding-bottom: 24px; }
.stats-section { background: var(--bg2); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; }
.stats-section h3 { font-size: 1rem; margin-bottom: 12px; color: var(--accent); }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
.stat-box { text-align: center; }
.stat-box__num { font-size: 1.8rem; font-weight: 700; }
.stat-box__num--green { color: var(--green); }
.stat-box__num--red { color: var(--red); }
.stat-box__num--dim { color: var(--text-dim); }
.stat-box__label { font-size: 0.75rem; color: var(--text-dim); margin-top: 2px; }

.stats-question { margin-bottom: 10px; }
.stats-question__title { font-size: 0.85rem; font-weight: 600; margin-bottom: 4px; }
.stats-question__bar { display: flex; height: 6px; border-radius: 3px; overflow: hidden; background: var(--bg); }
.stats-question__bar div { height: 100%; transition: width var(--transition); }
.stats-question__bar .sq-known { background: var(--green); }
.stats-question__bar .sq-unknown { background: var(--red); }
.stats-question__nums { display: flex; gap: 12px; font-size: 0.75rem; color: var(--text-dim); margin-top: 2px; }

.reset-btn {
  background: transparent; border: 1px solid var(--red); color: var(--red);
  border-radius: var(--radius); padding: 12px 24px; font-size: 0.95rem;
  cursor: pointer; display: block; margin: 24px auto 0;
}
.reset-btn:hover { background: var(--red); color: #fff; }

/* === Formula rendering === */
.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; margin: 0 4px; }
.frac-num { border-bottom: 2px solid currentColor; padding: 0 4px 2px; font-size: 0.95em; }
.frac-den { padding: 2px 4px 0; font-size: 0.95em; }
.math-block { background: #f0f0f0; border-radius: 8px; padding: 12px; margin: 8px 0; font-size: 1rem; font-family: 'Cambria Math', Georgia, serif; }
.card-back .math-block { background: rgba(255,255,255,0.1); }
.var { font-style: italic; font-family: 'Cambria Math', Georgia, serif; }
.formula { font-family: 'Cambria Math', Georgia, serif; font-size: 1.05em; }
.card-front .tag { display: inline-block; background: var(--accent); color: #fff; font-size: 0.7rem; padding: 2px 8px; border-radius: 10px; margin-bottom: 8px; }
.card-back ul, .card-back ol { text-align: left; padding-left: 20px; }
.card-back li { margin-bottom: 4px; }
.card-front strong, .card-back strong { color: var(--accent); }
.card-back strong { color: var(--yellow); }

/* === Responsive === */
@media (min-width: 768px) {
  .screen { padding: 24px; }
  .card { min-height: 340px; }
  .card-inner { min-height: 340px; }
  .card-front, .card-back { padding: 32px; font-size: 1.1rem; }
}

/* === Reduced motion === */
@media (prefers-reduced-motion: reduce) {
  .card-inner { transition: none; }
  .progress-bar__fill, .study-progress-fill { transition: none; }
  .home-btn { transition: none; }
}
