/* S&S Flashcards — mobile-first */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; padding: 0;
  height: 100%;
  background: #f1f3f6;
  color: #1c1f24;
  font-family: -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* === Top bar === */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: #fff;
  border-bottom: 1px solid #e3e6ec;
  flex: 0 0 auto;
}
.deck-tabs { display: flex; gap: 4px; }
.deck-tab {
  border: 0; background: transparent;
  font-size: 15px; font-weight: 600;
  padding: 8px 14px; border-radius: 8px;
  color: #555; cursor: pointer;
}
.deck-tab.active { background: #0a5; color: #fff; }

.topbar-actions { display: flex; gap: 6px; }
#filter-btn, #index-btn {
  border: 1px solid #d6dae0; background: #fff;
  font-size: 14px; padding: 6px 12px; border-radius: 6px;
  cursor: pointer;
}

/* === Index overlay === */
#index-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: #fff;
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
.index-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #e3e6ec;
}
.index-head strong { font-size: 16px; }
#index-close {
  border: 0; background: #0a5; color: #fff;
  padding: 8px 14px; border-radius: 6px;
  font-size: 14px; cursor: pointer;
}
#index-search {
  margin: 10px 14px 6px;
  padding: 10px 12px;
  border: 1px solid #d6dae0; border-radius: 8px;
  font-size: 15px; -webkit-appearance: none;
}
#index-list {
  list-style: none; margin: 0; padding: 0 0 env(safe-area-inset-bottom);
  flex: 1 1 auto; overflow-y: auto;
}
#index-list li {
  padding: 13px 16px;
  border-bottom: 1px solid #f1f3f6;
  cursor: pointer;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
}
#index-list li:active { background: #eef9ee; }
#index-list .ix-title { flex: 1 1 auto; font-size: 15px; color: #1c1f24; }
#index-list .ix-meta  { flex: 0 0 auto; font-size: 11px; color: #888; }
#index-list .ix-mark  { font-size: 12px; padding: 2px 8px; border-radius: 10px; }
#index-list .ix-known  { background: #d8f3d8; color: #0a5; }
#index-list .ix-review { background: #ffeaea; color: #c33; }
#index-list .ix-group {
  background: #f5f7fa; font-weight: 600; font-size: 12px; color: #555;
  text-transform: uppercase; letter-spacing: 0.5px;
  padding: 8px 16px; cursor: default;
}

#filter-panel, #exam-year-filter {
  background: #fff; padding: 10px 14px;
  border-bottom: 1px solid #e3e6ec;
  display: flex; flex-wrap: wrap; gap: 6px;
}
.filter-opt, .year-opt {
  border: 1px solid #d6dae0; background: #fff;
  padding: 6px 12px; border-radius: 16px;
  font-size: 13px; cursor: pointer;
}
.filter-opt.active, .year-opt.active { background: #0a5; color: #fff; border-color: #0a5; }
#filter-panel hr { width: 100%; border: 0; border-top: 1px solid #eee; margin: 6px 0; }
#reset-progress { width: 100%; padding: 6px; font-size: 13px; color: #c33; background: #fff; border: 1px solid #f3c2c2; border-radius: 6px; cursor: pointer; }

/* === Card area === */
#card-area {
  flex: 1 1 auto;
  position: relative;
  padding: 14px;
  overflow: hidden;
}

#card {
  position: relative;
  width: 100%; height: 100%;
  perspective: 1500px;
  cursor: pointer;
}
.face {
  position: absolute; inset: 0;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.08), 0 0 0 1px rgba(0,0,0,0.04);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transition: transform 0.5s cubic-bezier(.2,.7,.3,1);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.face.front {
  transform: rotateY(0deg);
  align-items: center; justify-content: center;
  text-align: center;
  padding: 36px 28px;
}
.face.front #front-inner { max-width: 100%; word-wrap: break-word; overflow-wrap: break-word; }
.face.back {
  transform: rotateY(180deg);
  padding: 0;
}
#card.flipped .face.front { transform: rotateY(-180deg); }
#card.flipped .face.back  { transform: rotateY(0deg); }

.card-inner { width: 100%; }
.scrollable {
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 26px 26px 22px;
  flex: 1 1 auto;
  height: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.tap-hint { position: absolute; bottom: 16px; font-size: 12px; color: #aaa; letter-spacing: 0.5px; }

/* Front title big */
#front-inner {
  font-size: 22px;
  font-weight: 600;
  color: #1c1f24;
  line-height: 1.35;
}
#front-inner .tags {
  margin-top: 14px;
  font-size: 12px; font-weight: 500;
  color: #666;
}
#front-inner .tags span {
  display: inline-block; padding: 3px 9px; margin: 2px;
  border-radius: 10px; background: #eef4ff; color: #36a;
}

/* Back content — reuse the deck styling */
.back .card-inner h2 { color: #0a5; margin: 18px 0 10px; font-size: 17px; border-left: 4px solid #0a5; padding-left: 10px; }
.back .card-inner h3 { color: #333; margin: 14px 0 6px; font-size: 16px; }
.back .card-inner p, .back .card-inner li { font-size: 15px; }
.back .card-inner table { border-collapse: collapse; margin: 10px 0; font-size: 14px; }
.back .card-inner td, .back .card-inner th { border: 1px solid #ccc; padding: 5px 8px; }
.back .card-inner code { background: #eee; padding: 1px 5px; border-radius: 3px; font-size: 14px; }
.back .card-inner pre.ascii { background: #1c1c20; color: #cfc; padding: 10px 12px; border-radius: 4px; font-family: 'SF Mono','Courier New',monospace; font-size: 12px; overflow-x: auto; max-width: 100%; }

/* === Make MathJax + tables fit phone screens ===
   No horizontal scroll anywhere — equations that don't fit are
   auto-scaled by JS after MathJax typesets. */
.back .card-inner mjx-container,
.back .card-inner mjx-container[display="true"] {
  max-width: 100%;
  overflow: hidden;
}
.back .card-inner mjx-container svg { max-width: 100%; height: auto; }
.back .card-inner mjx-container[display="true"] {
  display: block;
  margin: 10px 0;
  text-align: left;
}
.back .card-inner .mj-scaled {
  display: block;
  transform-origin: left top;
  margin: 10px 0;
}
/* Scale display math down on narrow screens so it usually fits without scrolling */
@media (max-width: 480px) {
  .back .card-inner mjx-container[display="true"] { font-size: 92%; }
  .back .card-inner { font-size: 14.5px; }
  .back .card-inner h2 { font-size: 16px; }
  .back .card-inner p, .back .card-inner li { font-size: 14.5px; }
  .scrollable { padding: 22px 20px 20px; }
}
@media (max-width: 380px) {
  .back .card-inner mjx-container[display="true"] { font-size: 84%; }
  .back .card-inner { font-size: 14px; }
}

/* Tables: shrink to fit. Force layout so cells share width and content wraps. */
.back .card-inner table {
  display: table;
  width: 100%;
  max-width: 100%;
  table-layout: fixed;
  word-wrap: break-word;
}
.back .card-inner td, .back .card-inner th { word-wrap: break-word; overflow-wrap: anywhere; }
/* Boxes shouldn't overflow either */
.back .card-inner .box { max-width: 100%; overflow-wrap: break-word; }
/* All children defensively clamped */
.back .card-inner img,
.back .card-inner svg { max-width: 100%; height: auto; }

/* Boxes — copied from source deck conventions */
.back .card-inner .box { padding: 12px 14px; border-radius: 6px; margin: 12px 0; border-left: 4px solid; }
.back .card-inner .concept  { background: #eef4ff; border-color: #36a; }
.back .card-inner .formula  { background: #fff7e6; border-color: #e69500; }
.back .card-inner .warn     { background: #ffeeee; border-color: #c33; }
.back .card-inner .pro-tip  { background: #eef9ee; border-color: #0a5; }
.back .card-inner .question { background: #f3eaff; border-color: #639; }
.back .card-inner .answer   { background: #d8f3d8; border-color: #0a5; font-weight: 500; }
.back .card-inner .twocol { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 600px) { .back .card-inner .twocol { grid-template-columns: 1fr; } }

/* === Empty state === */
#empty-state {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: #888; text-align: center; padding: 20px;
}

/* === Bottom bar === */
#bottombar {
  display: flex; gap: 8px;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid #e3e6ec;
  flex: 0 0 auto;
}
.action {
  flex: 1 1 0;
  border: 0; border-radius: 10px;
  font-size: 14px; font-weight: 600;
  padding: 14px 6px;
  cursor: pointer;
  color: #fff;
}
.action.review { background: #c43; }
.action.skip   { background: #888; }
.action.known  { background: #0a5; }
.action:active { opacity: 0.7; }

/* === Counter === */
#card-counter {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: rgba(28, 31, 36, 0.7); color: #fff;
  font-size: 11px; font-family: 'SF Mono', monospace;
  padding: 3px 10px; border-radius: 10px;
  pointer-events: none;
  z-index: 10;
}

/* === Swipe affordance === */
#card.swipe-left  { animation: swipeOutLeft 0.25s forwards; }
#card.swipe-right { animation: swipeOutRight 0.25s forwards; }
@keyframes swipeOutLeft  { to { transform: translateX(-110%); opacity: 0; } }
@keyframes swipeOutRight { to { transform: translateX( 110%); opacity: 0; } }

/* === Larger screens === */
@media (min-width: 700px) {
  #card-area { padding: 24px; max-width: 720px; margin: 0 auto; }
  #front-inner { font-size: 28px; }
  .scrollable { padding: 28px 32px 24px; }
}
