:root {
  --bg-dark: #0f172a;
  --card-bg: #1e293b;
  --teal-accent: #2dd4bf;
  --gold-accent: #fbbf24;
  --text-main: #f3f4f6;
  --text-sub: #9ca3af;
  --border-dark: rgba(255, 255, 255, 0.08);
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html {
  scrollbar-width: thin;
  scrollbar-color: #475569 #0f172a;
}
body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  font-family: system-ui, -apple-system, "Hiragino Kaku Gothic ProN", "Yu Gothic", sans-serif;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding-top: env(safe-area-inset-top, 10px);
  padding-bottom: env(safe-area-inset-bottom, 10px);
  scrollbar-width: thin;
  scrollbar-color: #475569 #0f172a;
}
.app-viewport {
  width: 100%;
  max-width: 430px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
}
.screen {
  display: none;
  flex-direction: column;
  padding: 12px 18px 28px;
  width: 100%;
}
.screen.active { display: flex; }

/* Top Header */
.top-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  margin-bottom: 12px;
  position: relative;
  gap: 8px;
}
.top-header h1 {
  font-size: 18px;
  font-weight: 800;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.home-pill-btn, .apptop-pill-btn {
  background: #1e293b;
  border: 1px solid #334155;
  color: var(--teal-accent);
  font-size: 12px;
  font-weight: 800;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  white-space: nowrap;
  text-decoration: none;
}
.home-pill-btn:active, .apptop-pill-btn:active { background: #334155; }

.header-icon-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
}
.header-icon-btn:active { background: rgba(255,255,255,0.1); }

/* Dropdown Menu */
.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 48px;
  width: 230px;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.5);
  z-index: 50;
  overflow: hidden;
}
.dropdown-menu.active { display: block; }
.dropdown-item {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 700;
  color: #f3f4f6;
  background: none;
  border: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
}
.dropdown-item:active { background: #334155; }
.dropdown-item.danger { color: #f87171; }

/* 上部タイル */
.stats-row { display: flex; gap: 12px; margin-bottom: 10px; }
.stat-tile {
  flex: 1;
  background: var(--card-bg);
  border-radius: 18px;
  padding: 14px 14px;
  text-align: center;
  border: 1px solid var(--border-dark);
  cursor: pointer;
  transition: transform 0.1s ease;
}
.stat-tile:active { transform: scale(0.97); }
.stat-tile .stat-title { font-size: 12px; color: var(--text-sub); font-weight: 600; margin-bottom: 4px; }
.stat-tile .stat-number { font-size: 22px; font-weight: 800; color: #fff; }
.stat-tile .stat-number-accent { font-size: 18px; font-weight: 800; color: var(--teal-accent); white-space: nowrap; }

/* 特殊機能ボタン行 */
.feature-row { display: flex; gap: 5px; margin-bottom: 12px; }
.feature-btn {
  flex: 1;
  background: linear-gradient(135deg, #1e293b, #0f172a);
  border: 1px solid rgba(45, 212, 191, 0.3);
  border-radius: 14px;
  padding: 11px 3px;
  text-align: center;
  cursor: pointer;
  font-size: 11px;
  font-weight: 800;
  color: var(--teal-accent);
  transition: transform 0.1s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  white-space: nowrap;
  min-width: 0;
}
.feature-btn.compact-icon {
  flex: 0 0 36px;
  font-size: 13px;
  padding: 11px 0;
}
.feature-btn:active { transform: scale(0.97); }

/* 分野タブ */
.field-tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
  margin-bottom: 10px;
}
.field-tab {
  padding: 9px 2px;
  border-radius: 12px;
  background: #1e293b;
  border: 1px solid #334155;
  color: #9ca3af;
  font-size: 11px;
  font-weight: 800;
  text-align: center;
  cursor: pointer;
  white-space: nowrap;
}
.field-tab.active {
  background: rgba(45, 212, 191, 0.15);
  border-color: var(--teal-accent);
  color: var(--teal-accent);
}

/* 単元タイル */
.unit-split-row {
  display: flex;
  gap: 10px;
  margin-bottom: 8px;
}
.unit-tile-left {
  flex: 6.5;
  background: var(--card-bg);
  border-radius: 14px;
  padding: 12px 14px;
  border: 1px solid var(--border-dark);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  overflow: hidden;
}
.unit-tile-left:active { transform: scale(0.98); }

.unit-tile-right {
  flex: 3.5;
  background: var(--card-bg);
  border-radius: 14px;
  padding: 12px 10px;
  border: 1px solid var(--border-dark);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  text-align: center;
  white-space: nowrap;
}
.unit-tile-right:active { transform: scale(0.98); }

.unit-title {
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.unit-sub { font-size: 11px; color: var(--teal-accent); font-weight: 700; white-space: nowrap; flex-shrink: 0; }
.unit-rate { font-size: 13px; font-weight: 800; color: var(--teal-accent); }
.unit-count { font-size: 11px; color: var(--text-sub); font-weight: 700; }

/* 難易度バッジ */
.diff-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 7px;
  border-radius: 6px;
  color: #fff;
  flex-shrink: 0;
}
.diff-A { background: #2563eb; }
.diff-B { background: #10b981; }
.diff-C { background: #e11d48; }

.diff-pill-card {
  display: inline-flex;
  align-items: center;
  font-size: 11px;
  font-weight: 800;
  padding: 2px 9px;
  border-radius: 6px;
  color: #fff;
}

.mistake-badge {
  display: inline-flex;
  align-items: center;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 6px;
  border-radius: 6px;
  background: rgba(239, 68, 68, 0.25);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.5);
  flex-shrink: 0;
}
.memo-list-badge {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: rgba(251, 191, 36, 0.18);
  border: 1px solid rgba(251, 191, 36, 0.6);
  color: #fde047;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 7px;
  border-radius: 999px;
  white-space: nowrap;
  flex-shrink: 0;
}
.attempt-pill {
  display: inline-flex;
  align-items: center;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.75);
  border: 1px solid #334155;
  color: #e2e8f0;
  white-space: nowrap;
}

/* ランダム出題ボタン */
.random-top-btn {
  width: 100%;
  background: linear-gradient(135deg, rgba(45, 212, 191, 0.18), rgba(15, 23, 42, 0.95));
  border: 1px solid rgba(45, 212, 191, 0.5);
  border-radius: 14px;
  padding: 10px 14px;
  margin-bottom: 8px;
  text-align: center;
  cursor: pointer;
  font-size: 13px;
  font-weight: 800;
  color: var(--teal-accent);
  transition: transform 0.1s ease;
}
.random-top-btn:active { transform: scale(0.98); }
.random-top-btn:disabled { opacity: 0.4; cursor: default; }

/* Flashcard Screen */
.flashcard {
  background: var(--card-bg);
  border-radius: 20px;
  padding: 16px 18px 20px;
  border: 1px solid var(--border-dark);
  margin: 4px 0 12px;
  min-height: 220px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  text-align: left;
  position: relative;
}
.star-btn-inline.starred { color: #f59e0b; }

.memo-open-btn {
  background: #0f172a;
  border: 1px solid #334155;
  color: #94a3b8;
  font-size: 11px;
  font-weight: 800;
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
}
.memo-open-btn.has-memo {
  background: rgba(251, 191, 36, 0.16);
  border-color: rgba(251, 191, 36, 0.55);
  color: #fde047;
}
.memo-open-btn:active { background: #334155; }

.answer-memo-box {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 10px 12px 12px;
  border-radius: 10px;
  background: rgba(251, 191, 36, 0.10);
  border: 1px solid rgba(251, 191, 36, 0.35);
  color: #fef08a;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.65;
  text-align: left;
}
.answer-memo-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  font-weight: 800;
  color: #fbbf24;
  margin-bottom: 6px;
}
.answer-memo-body {
  white-space: pre-wrap;
  word-break: break-word;
}
.memo-textarea {
  width: 100%;
  min-height: 110px;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 12px;
  padding: 12px;
  color: #f3f4f6;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.6;
  resize: vertical;
  margin-top: 6px;
}
.memo-textarea:focus { outline: none; border-color: var(--teal-accent); }

.reveal-btn {
  display: block;
  width: 100%;
  margin: 0 0 8px;
  padding: 14px;
  font-size: 14px;
  font-weight: 800;
  color: var(--teal-accent);
  background: transparent;
  border: 1px dashed var(--teal-accent);
  border-radius: 14px;
  cursor: pointer;
  text-align: center;
}
.card-category-tag { font-size: 12px; color: var(--teal-accent); font-weight: 700; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.card-source-sub { font-size: 10.5px; color: var(--text-sub); margin-top: 2px; }
.card-q-text {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.75;
  color: #fff;
  width: 100%;
  margin: 10px 0 4px;
  word-break: break-word;
}
.card-q-text img, .card-ans-text img, .card-note-box img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  background: #fff;
  padding: 6px;
  margin: 8px 0;
  display: block;
}
.card-ans-text {
  font-size: 15px;
  font-weight: 800;
  line-height: 1.7;
  color: #f3f4f6;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed #334155;
  width: 100%;
  display: none;
  text-align: left;
  word-break: break-word;
}
.card-ans-text.revealed { display: block; }
.card-ans-text u {
  text-decoration: none;
  color: #fde047;
  border-bottom: 2.5px solid var(--teal-accent);
  padding-bottom: 1px;
}
.card-note-box {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.78);
  border-left: 3px solid var(--teal-accent);
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1.65;
  color: #cbd5e1;
  word-break: break-word;
}
.card-note-box u {
  text-decoration: none;
  color: #fde047;
  border-bottom: 1.5px solid var(--teal-accent);
}
.card-ans-sub {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-sub);
  margin-top: 8px;
}

.sub-control-row { display: flex; gap: 8px; width: 100%; margin-bottom: 8px; }
.btn-sub-ctrl {
  flex: 1;
  padding: 10px 0;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 700;
  background: #1e293b;
  border: 1px solid #334155;
  color: #cbd5e1;
  cursor: pointer;
  text-align: center;
}
.btn-sub-ctrl:active { background: #334155; }

.judge-main-row { display: flex; gap: 8px; width: 100%; }
.btn-judge {
  flex: 1;
  padding: 12px 4px;
  border-radius: 14px;
  font-size: 13px;
  font-weight: 800;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}
.btn-judge-wrong { background: #ef4444; color: #fff; }
.btn-judge-unsure { background: #f59e0b; color: #1e1b4b; }
.btn-judge-good { background: #10b981; color: #064e3b; }

/* Filter Pills & Map Grid */
.filter-pills {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 8px;
}
.filter-pill {
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 800;
  background: #1e293b;
  border: 1px solid #334155;
  color: #cbd5e1;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}
.filter-pill.active {
  background: var(--teal-accent);
  color: #0f172a;
  border-color: var(--teal-accent);
}

.map-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: calc(100vh - 225px);
  overflow-y: auto;
  padding-right: 4px;
}
.map-row {
  background: var(--card-bg);
  border: 1px solid var(--border-dark);
  border-radius: 12px;
  padding: 11px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  gap: 10px;
}
.map-row:active { background: #334155; }
.map-row-left {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.map-row-line1 {
  font-size: 12px;
  font-weight: 800;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
}
.map-row-line2 {
  font-size: 12px;
  color: #e2e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.map-row-right {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 800;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 3px;
}
.rating-circle-badge {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid #475569;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  color: #fff;
}

/* Modals & Mini Games */
.modal-backdrop {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.8);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal-backdrop.active { display: flex; }
.modal-box {
  background: var(--card-bg);
  border: 1px solid #334155;
  border-radius: 20px;
  padding: 22px;
  width: 100%;
  max-width: 360px;
  max-height: 85vh;
  overflow-y: auto;
  text-align: center;
}
.modal-box h3 { font-size: 18px; font-weight: 800; margin-bottom: 10px; color: #fff; }
.modal-btn {
  width: 100%;
  padding: 13px;
  border-radius: 12px;
  border: none;
  font-weight: 700;
  font-size: 14px;
  margin-top: 10px;
  cursor: pointer;
}
.modal-btn-primary { background: var(--teal-accent); color: #0f172a; }
.modal-btn-gold { background: var(--gold-accent); color: #1e1b4b; }
.modal-btn-cancel { background: #334155; color: #e5e7eb; }

.select-modal-item {
  width: 100%;
  padding: 13px 16px;
  border-radius: 12px;
  background: #0f172a;
  border: 1px solid #334155;
  color: #f3f4f6;
  font-size: 14px;
  font-weight: 800;
  margin-bottom: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.select-modal-item:active { background: #334155; }


/* 難易度別モーダル（正答別と同じ2x2カラータイル＋抽出件数） */
.diff-btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 6px;
}
.diff-choice-btn {
  padding: 15px 8px;
  border-radius: 14px;
  border: none;
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  line-height: 1.3;
}
.diff-choice-btn:active { transform: scale(0.97); }
.diff-choice-A { background: #2563eb; }
.diff-choice-B { background: #059669; }
.diff-choice-C { background: #e11d48; }
.diff-choice-ALL { background: #475569; }
.diff-choice-count {
  font-size: 11px;
  font-weight: 700;
  opacity: 0.92;
}

/* 正答状況モーダル（2x2カラータイル＋抽出件数） */
.rating-btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 14px 0 6px;
}
.rating-choice-btn {
  padding: 15px 8px;
  border-radius: 14px;
  border: none;
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  line-height: 1.3;
}
.rating-choice-btn:active { transform: scale(0.97); }
.rating-choice-good { background: #059669; }
.rating-choice-tri { background: #d97706; }
.rating-choice-cross { background: #dc2626; }
.rating-choice-unrated { background: #334155; }
.rating-choice-count {
  font-size: 11px;
  font-weight: 700;
  opacity: 0.92;
}

.shop-card {
  background: #0f172a;
  border: 1px solid var(--border-dark);
  border-radius: 14px;
  padding: 14px;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  text-align: left;
}
.shop-info .shop-name { font-size: 14px; font-weight: 800; color: #fff; margin-bottom: 2px; }
.shop-info .shop-price { font-size: 12px; font-weight: 700; color: var(--gold-accent); }
.shop-action-btn {
  background: var(--gold-accent);
  color: #1e1b4b;
  border: none;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}
.shop-action-btn:disabled { background: #334155; color: #9ca3af; cursor: default; }

/* Mini Game Panels */
.game-panel { display: none; }
.game-panel.active { display: block; }
.choice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 15px 0;
}
.choice-box {
  background: #0f172a;
  border: 2px solid #334155;
  border-radius: 14px;
  padding: 16px;
  font-size: 16px;
  font-weight: 800;
  color: var(--teal-accent);
  cursor: pointer;
  transition: all 0.1s;
}
.choice-box:active { transform: scale(0.95); background: #1e293b; }

.race-track {
  background: #0f172a;
  border-radius: 14px;
  padding: 12px;
  margin: 12px 0;
  border: 1px solid #334155;
}
.race-lane {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #1e293b;
  border-radius: 8px;
  margin: 6px 0;
  padding: 4px 10px;
  font-size: 18px;
  position: relative;
  border: 1px solid transparent;
}
.race-progress-bar {
  flex: 1;
  background: #334155;
  height: 10px;
  border-radius: 5px;
  margin: 0 0 0 10px;
  overflow: hidden;
  position: relative;
}
.race-fill {
  height: 100%;
  width: 0%;
  background: var(--teal-accent);
  border-radius: 5px;
  transition: width 0.25s linear;
}

.history-list { display: flex; flex-direction: column; gap: 6px; max-height: 250px; overflow-y: auto; margin-bottom: 14px; text-align: left; }
.history-item { background: #0f172a; padding: 10px 12px; border-radius: 8px; font-size: 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px; }

.breakdown-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}
.breakdown-card {
  background: #0f172a;
  border: 1px solid var(--border-dark);
  border-radius: 14px;
  padding: 12px;
  text-align: center;
  cursor: pointer;
}
.breakdown-card .title { font-size: 12px; font-weight: 700; margin-bottom: 4px; }
.breakdown-card .count { font-size: 22px; font-weight: 800; }

.card-num {
  background-color: var(--teal-accent);
  color: #0f172a;
  font-weight: 800;
  font-size: 0.92em;
  padding: 2px 8px;
  border-radius: 6px;
}


/* 指定（ピン留め）バッジ・ボタン */
.pin-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 10px;
  font-weight: 800;
  padding: 1px 7px;
  border-radius: 6px;
  background: rgba(251, 191, 36, 0.16);
  color: #fbbf24;
  border: 1px solid rgba(251, 191, 36, 0.55);
  flex-shrink: 0;
}
.pin-toggle-btn {
  background: #0f172a;
  border: 1px solid #334155;
  color: #94a3b8;
  font-size: 11px;
  font-weight: 800;
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
}
.pin-toggle-btn.pinned {
  background: rgba(251, 191, 36, 0.16);
  border-color: rgba(251, 191, 36, 0.55);
  color: #fbbf24;
}
.pin-toggle-btn:active { background: #334155; }
