*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --bg3: #0f3460;
  --accent: #e94560;
  --accent2: #533483;
  --text: #eee;
  --text2: #aaa;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --cat-tech: #4fc3f7;
  --cat-mgmt: #81c784;
  --cat-strat: #ffb74d;
  --card: #16213e;
  --border: #333;
}

html, body {
  height: 100%; width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  user-select: none;
  -webkit-user-select: none;
}

/* --- Screens --- */
.screen { display: none; height: 100%; flex-direction: column; }
.screen.active { display: flex; }

/* --- Header --- */
.header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px;
  padding-top: calc(var(--safe-top) + 12px);
  background: var(--bg2);
  flex-shrink: 0;
}
.header h1 { font-size: 18px; flex: 1; }
.header .version { font-size: 12px; color: var(--text2); font-weight: normal; margin-left: 1ch; }
.header .back-btn {
  background: none; border: none; color: var(--text); font-size: 22px;
  cursor: pointer; padding: 4px 8px;
}
.header .back-title {
  cursor: pointer;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn {
  background: none; border: none; color: var(--text2); font-size: 20px;
  cursor: pointer; padding: 4px;
}

/* --- Home: Exam Selection & Coverage --- */
.exam-bar { padding: 8px 16px 0; flex-shrink: 0; }
.exam-chips {
  display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.exam-chips::-webkit-scrollbar { display: none; }
.exam-chip {
  flex-shrink: 0; display: flex; align-items: center; gap: 5px;
  padding: 5px 10px; border-radius: 14px;
  background: var(--bg2); border: 1px solid #ffffff20;
  font-size: 12px; cursor: pointer; white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.exam-chip input { display: none; }
.exam-chip.active { background: var(--accent); border-color: transparent; }
.exam-chip .chip-code { font-weight: 700; }
.exam-chip .chip-count { opacity: 0.6; font-size: 11px; }
.cov-summary { font-size: 12px; color: var(--text2); padding: 2px 0 4px; cursor: pointer; }
.cov-detail { padding: 2px 0 6px; }
.cov-row { display: flex; align-items: center; font-size: 11px; padding: 1px 0; }
.cov-field {
  flex: 1; color: var(--text2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.cov-cell { width: 34px; text-align: center; flex-shrink: 0; }
.cov-head .cov-cell { color: var(--text2); }
.cov-na { color: #555; }
.cov-none { color: #e05555; }
.cov-few { color: #e0b345; }
.cov-good { color: #55c060; }

/* --- Home: Field Grid --- */
.field-grid {
  flex: 1; overflow-y: auto; padding: 0 16px 16px;
  -webkit-overflow-scrolling: touch;
}
.category-section { margin-top: 16px; }
.category-title {
  font-size: 13px; font-weight: 700; padding: 6px 0;
  border-bottom: 1px solid #333; margin-bottom: 8px;
}
.category-title.tech { color: var(--cat-tech); border-color: var(--cat-tech); }
.category-title.mgmt { color: var(--cat-mgmt); border-color: var(--cat-mgmt); }
.category-title.strat { color: var(--cat-strat); border-color: var(--cat-strat); }

.field-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}
.field-card {
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 12px;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
  border-left: 3px solid transparent;
}
.field-card:active { transform: scale(0.97); background: var(--bg3); }
.field-card.tech { border-left-color: var(--cat-tech); }
.field-card.mgmt { border-left-color: var(--cat-mgmt); }
.field-card.strat { border-left-color: var(--cat-strat); }

.field-name { font-size: 13px; font-weight: 600; line-height: 1.3; }
.field-count { font-size: 11px; color: var(--text2); margin-top: 4px; }
.level-progress {
  display: flex; gap: 3px; margin-top: 6px;
}
.level-dot {
  flex: 1; height: 4px; border-radius: 2px;
  background: #333;
}
.level-dot.has-content { background: var(--accent); }
.level-dot.downloaded { background: #4caf50; }

/* --- Field Detail: Level Tabs --- */
.level-tabs {
  display: flex; gap: 0; flex-shrink: 0;
  background: var(--bg2);
  border-bottom: 1px solid #333;
}
.level-tab {
  flex: 1; padding: 10px 8px;
  text-align: center; font-size: 13px;
  color: var(--text2); cursor: pointer;
  border-bottom: 2px solid transparent;
  background: none; border-top: none; border-left: none; border-right: none;
  transition: all 0.15s;
}
.level-tab.active {
  color: var(--accent); border-bottom-color: var(--accent);
}
.level-tab .tab-count {
  display: block; font-size: 10px; margin-top: 2px;
}
.level-tab.locked { opacity: 0.4; }

/* --- Lecture List with Swipe Delete --- */
.lecture-list {
  flex: 1; overflow-y: auto; padding: 8px 16px;
  -webkit-overflow-scrolling: touch;
}
.lecture-empty {
  padding: 40px 20px; text-align: center; color: var(--text2);
  font-size: 14px; line-height: 1.6;
}
.swipe-wrapper {
  position: relative; overflow: hidden;
}
.lecture-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 0; border-top: 1px solid #ffffff10;
  cursor: pointer;
  background: var(--bg);
  position: relative; z-index: 1;
  transform: translateX(0);
  will-change: transform;
}
.lecture-item:active:not(.swiped) { opacity: 0.7; }
.lecture-item .play-icon { font-size: 18px; color: var(--accent); flex-shrink: 0; width: 24px; }
.lecture-item .lecture-info { flex: 1; min-width: 0; }
.lecture-item .lecture-term { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lecture-item .lecture-meta { font-size: 11px; color: var(--text2); }
.lecture-item .lecture-dur { font-size: 12px; color: var(--text2); flex-shrink: 0; }
.swipe-delete {
  position: absolute; right: 0; top: 0; bottom: 0;
  width: 80px; background: #b71c1c;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 13px; font-weight: bold;
  cursor: pointer; z-index: 0;
}
.list-actions {
  padding: 12px 16px; padding-bottom: calc(12px + var(--safe-bottom));
  background: var(--bg2);
  display: flex; gap: 10px; flex-shrink: 0;
}
.list-actions button {
  flex: 1; padding: 10px; border: none; border-radius: var(--radius);
  font-size: 14px; cursor: pointer; color: var(--text);
}
.btn-play { background: var(--accent); }

/* --- Player Screen --- */
.player-area { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.subtitle-box {
  flex: 1; overflow-y: auto; padding: 20px 16px;
  font-size: 16px; line-height: 1.8;
  -webkit-overflow-scrolling: touch;
}
.subtitle-box .chunk { color: var(--text2); transition: color 0.3s; }
.subtitle-box .chunk.active { color: var(--text); }

.player-controls {
  background: var(--bg2);
  padding: 12px 16px; padding-bottom: calc(12px + var(--safe-bottom));
  flex-shrink: 0;
}
.progress-bar {
  display: flex; align-items: center; gap: 8px; margin-bottom: 10px;
}
.progress-bar input[type=range] {
  flex: 1; -webkit-appearance: none; height: 4px;
  background: #444; border-radius: 2px; outline: none;
}
.progress-bar input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px;
  background: var(--accent); border-radius: 50%;
}
.progress-bar .time { font-size: 11px; color: var(--text2); min-width: 36px; }

.transport {
  display: flex; justify-content: center; align-items: center; gap: 16px; margin-bottom: 10px;
}
.transport button {
  background: none; border: none; color: var(--text); font-size: 24px;
  cursor: pointer; padding: 8px;
}
.transport .play-pause { font-size: 36px; color: var(--accent); }

.transport .repeat-btn {
  position: relative; font-size: 22px; color: var(--text2); transition: color 0.2s;
}
.transport .repeat-btn.active { color: var(--accent); }
.transport .repeat-btn.active::after {
  content: attr(data-count);
  position: absolute; top: 4px; right: 2px;
  font-size: 11px; font-weight: bold; color: var(--accent);
  line-height: 1; pointer-events: none;
}

.speed-bar {
  display: flex; justify-content: center; gap: 6px;
}
.speed-bar button {
  background: var(--bg); color: var(--text2); border: 1px solid #444;
  padding: 4px 12px; border-radius: 16px; font-size: 12px; cursor: pointer;
}
.speed-bar button.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* --- Progress Matrix --- */
.progress-matrix {
  flex: 1; overflow-y: auto; padding: 16px;
  -webkit-overflow-scrolling: touch;
}
.matrix-category { margin-bottom: 16px; }
.matrix-category-title {
  font-size: 13px; font-weight: 700; padding: 4px 0 8px;
  border-bottom: 1px solid #333;
}
.matrix-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 0; border-bottom: 1px solid #ffffff08;
}
.matrix-field-name {
  flex: 1; font-size: 12px; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.matrix-levels {
  display: flex; gap: 4px; flex-shrink: 0;
}
.matrix-cell {
  width: 40px; height: 24px; border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 700;
}
.matrix-cell.empty { background: #222; color: #555; }
.matrix-cell.available { background: #1b4332; color: #a5d6a7; }
.matrix-cell.downloaded { background: #0d47a1; color: #90caf9; }
.matrix-cell.na { background: transparent; border: 1px solid #333; color: #555; }

/* --- Exam Schedule --- */
.schedule-list {
  flex: 1; overflow-y: auto; padding: 16px;
  -webkit-overflow-scrolling: touch;
}
.schedule-updated {
  font-size: 11px; color: var(--text2); padding: 0 0 12px;
}
.schedule-group { margin-bottom: 16px; }
.schedule-group-title {
  font-size: 12px; font-weight: 700; color: var(--accent);
  padding: 4px 0 6px; border-bottom: 1px solid #333;
  margin-bottom: 6px;
}
.schedule-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid #ffffff08;
}
.schedule-exam-code {
  font-size: 16px; font-weight: 700; width: 36px; flex-shrink: 0;
  color: var(--accent);
}
.schedule-exam-name {
  flex: 1; font-size: 13px; min-width: 0;
}
.schedule-info {
  text-align: right; flex-shrink: 0;
}
.schedule-label {
  font-size: 12px; line-height: 1.4;
}
.schedule-days {
  font-size: 11px; margin-top: 2px;
}
.schedule-cbt { color: #81c784; }
.schedule-date { color: #90caf9; }
.schedule-soon { color: #ffb74d; }
.schedule-soon .schedule-days { color: #ff9800; font-weight: 700; }
.schedule-active { color: #4fc3f7; }
.schedule-active .schedule-days { color: #29b6f6; font-weight: 700; }
.schedule-tbd { color: #999; }
.schedule-past { color: #666; }

/* --- Download Manager --- */
.dl-manager { flex: 1; overflow-y: auto; padding: 16px; }
.dl-section-title {
  font-size: 14px; font-weight: 700; color: var(--accent);
  padding: 12px 0 6px; border-bottom: 1px solid #333;
  margin-bottom: 8px;
}
.dl-field-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; border-top: 1px solid #ffffff10;
}
.dl-field-row label { flex: 1; font-size: 13px; }
.dl-field-row .dl-info { font-size: 11px; color: var(--text2); }
.dl-field-row input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); }
.dl-level-select {
  font-size: 12px; background: var(--bg); color: var(--text);
  border: 1px solid #444; border-radius: 6px; padding: 2px 6px;
}
.dl-progress {
  margin-top: 16px; padding: 12px; background: var(--bg2); border-radius: var(--radius);
}
.dl-progress .dl-bar {
  height: 6px; background: #333; border-radius: 3px; overflow: hidden; margin-top: 8px;
}
.dl-progress .dl-bar-fill { height: 100%; background: var(--accent); transition: width 0.3s; }
.dl-footer {
  padding: 12px 16px; padding-bottom: calc(12px + var(--safe-bottom));
  background: var(--bg2); display: flex; gap: 10px; flex-shrink: 0;
}
.dl-footer button {
  flex: 1; padding: 12px; border: none; border-radius: var(--radius);
  font-size: 14px; cursor: pointer; color: var(--text);
}
.btn-download { background: var(--accent); }
.btn-delete { background: #b71c1c; }

/* --- Edit Mode --- */
.edit-btn, .delete-btn {
  background: none; border: none; color: var(--text2); font-size: 18px;
  cursor: pointer; padding: 4px 8px; flex-shrink: 0;
}
.delete-btn { font-size: 16px; }
.edit-controls {
  background: var(--bg2);
  padding: 12px 16px; padding-bottom: calc(12px + var(--safe-bottom));
  flex-shrink: 0;
}
.edit-info {
  text-align: center; font-size: 13px; color: var(--accent); margin-bottom: 10px;
}
.edit-buttons { display: flex; gap: 10px; }
.edit-buttons button {
  flex: 1; padding: 12px; border: none; border-radius: var(--radius);
  font-size: 14px; cursor: pointer; color: var(--text);
}
.btn-cancel { background: #555; }
.btn-submit { background: var(--accent); }
.edit-textarea {
  width: 100%; height: 100%;
  background: var(--bg); color: var(--text);
  border: 2px solid var(--accent); border-radius: var(--radius);
  padding: 16px; font-size: 16px; line-height: 1.8;
  font-family: inherit; resize: none; outline: none;
  user-select: text; -webkit-user-select: text;
}

/* --- Infographic --- */
.infographic-area {
  flex-shrink: 0; padding: 8px 16px;
  text-align: center; background: var(--bg);
  border-top: 1px solid #ffffff10;
}
.infographic-img {
  max-width: 100%; max-height: 200px;
  border-radius: 8px; cursor: pointer;
  object-fit: contain;
  background: #fff;
  transition: opacity 0.3s;
}

/* Landscape: text and infographic side by side */
@media (orientation: landscape) {
  .player-area { flex-direction: row; }
  .infographic-area {
    width: 45%; flex-shrink: 0;
    border-top: none; border-left: 1px solid #ffffff10;
    display: flex; align-items: center; justify-content: center;
    overflow: hidden;
  }
  .infographic-img { max-height: 100%; }
}
.infographic-modal {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.92); z-index: 200;
  justify-content: center; align-items: center;
  padding: 20px; cursor: pointer;
}
.infographic-modal.show { display: flex; }
.infographic-modal-img {
  max-width: 100%; max-height: 100%;
  object-fit: contain; background: #fff;
  border-radius: 8px;
}

/* --- Mentor Quiz Area --- */
.mentor-quiz-area {
  flex: 1; overflow-y: auto; padding: 16px;
  -webkit-overflow-scrolling: touch;
}

/* --- Toast --- */
.toast {
  position: fixed; bottom: calc(80px + var(--safe-bottom)); left: 50%; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 20px; border-radius: 20px;
  font-size: 13px; opacity: 0; transition: opacity 0.3s;
  pointer-events: none; z-index: 100;
}
.toast.show { opacity: 1; }
