:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f5f5f7;
  --bg-card: #ffffff;
  --text-primary: #1d1d1f;
  --text-secondary: #8e8e93;
  --accent-blue: #007aff;
  --accent-blue-hover: #0062cc;
  --accent-gold: #ffcc00;
  --tip-bg: #fff9e6;
  --tip-border: #ffcc00;
  --tip-text-title: #b38600;
  --border-color: #e5e5ea;
  --card-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  --base-font-size: 14px;
}

[data-theme="dark"] {
  --bg-primary: #1c1c1e;
  --bg-secondary: #2c2c2e;
  --bg-card: #2c2c2e;
  --text-primary: #f2f2f7;
  --text-secondary: #98989d;
  --accent-blue: #0a84ff;
  --accent-blue-hover: #409cff;
  --accent-gold: #ffd60a;
  --tip-bg: #2d2613;
  --tip-border: #ffd60a;
  --tip-text-title: #ffd60a;
  --border-color: #38383a;
  --card-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-primary: #1c1c1e;
    --bg-secondary: #2c2c2e;
    --bg-card: #2c2c2e;
    --text-primary: #f2f2f7;
    --text-secondary: #98989d;
    --accent-blue: #0a84ff;
    --accent-blue-hover: #409cff;
    --accent-gold: #ffd60a;
    --tip-bg: #2d2613;
    --tip-border: #ffd60a;
    --tip-text-title: #ffd60a;
    --border-color: #38383a;
    --card-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: var(--base-font-size);
}

html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  margin: 0;
  padding: 0;
  -webkit-user-select: none;
  user-select: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", sans-serif;
  font-size: 1rem;
  line-height: 1.6;
  color: var(--text-primary);
  background-color: var(--bg-primary);
  word-break: break-word;
  overflow-wrap: break-word;
  transition: background-color 0.3s, color 0.3s;
}

input, textarea {
  -webkit-user-select: text;
  user-select: text;
}

/* ---------------------------------------------------- */
/* APP SHELL VIEW SWITCHING                             */
/* ---------------------------------------------------- */
.app-view {
  display: none;
  width: 100%;
  min-height: 100vh;
  animation: fadeIn 0.2s ease-out;
}

.app-view.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(3px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Initial Loading Overlay */
#appLoadingOverlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background-color: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.2s ease;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent-blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Toast System */
.toast {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background-color: rgba(29, 29, 31, 0.92);
  color: #ffffff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  z-index: 9000;
  opacity: 0;
  transition: all 0.3s ease;
  pointer-events: none;
  backdrop-filter: blur(10px);
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* ---------------------------------------------------- */
/* 1. LOGIN VIEW STYLES                                 */
/* ---------------------------------------------------- */
.auth-container {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  background-color: var(--bg-secondary);
  border-radius: 20px;
  padding: 32px 24px;
  border: 1px solid var(--border-color);
  box-shadow: 0 10px 40px rgba(0,0,0,0.1);
  text-align: center;
}

.auth-icon { font-size: 48px; margin-bottom: 12px; }
.auth-title { font-size: 22px; font-weight: 800; margin: 0 0 6px 0; }
.auth-desc { font-size: 13px; color: var(--text-secondary); margin: 0 0 24px 0; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 14px;
  border: 1px solid var(--border-color);
  background-color: var(--bg-card);
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  letter-spacing: 2px;
  outline: none;
}
.auth-input:focus { border-color: var(--accent-blue); box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.15); }
.auth-submit-btn {
  width: 100%;
  padding: 14px;
  border-radius: 14px;
  border: none;
  background-color: var(--accent-blue);
  color: #ffffff;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s;
}
.auth-submit-btn:active { opacity: 0.85; }

/* ---------------------------------------------------- */
/* 2. LOBBY VIEW STYLES                                 */
/* ---------------------------------------------------- */
.lobby-container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 16px 80px 16px;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 28px;
}

.top-controls { display: flex; gap: 8px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 18px;
  border: 1px solid var(--border-color);
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s;
}
.icon-btn:active { transform: scale(0.95); }

.lobby-header { margin-bottom: 32px; }
.lobby-title {
  font-size: 28px;
  font-weight: 800;
  margin: 0 0 6px 0;
  background: linear-gradient(135deg, var(--accent-blue), #ff9500);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.lobby-subtitle { font-size: 14px; color: var(--text-secondary); margin: 0; }

.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.module-card {
  background-color: var(--bg-card);
  border-radius: 18px;
  padding: 24px;
  border: 1px solid var(--border-color);
  box-shadow: var(--card-shadow);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: transform 0.2s, box-shadow 0.2s;
}
.module-card:hover { transform: translateY(-2px); box-shadow: 0 10px 28px rgba(0, 0, 0, 0.1); }

.card-badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 6px;
  background-color: rgba(0, 122, 255, 0.12);
  color: var(--accent-blue);
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 12px;
  align-self: flex-start;
}

.card-title { font-size: 20px; font-weight: 800; margin: 0 0 8px 0; }
.card-desc { font-size: 13px; color: var(--text-secondary); line-height: 1.5; margin: 0 0 20px 0; }

.enter-btn {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: none;
  background-color: var(--accent-blue);
  color: #ffffff;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}
.enter-btn:active { transform: scale(0.97); }

/* ---------------------------------------------------- */
/* 3. HISTORY VIEW STYLES                               */
/* ---------------------------------------------------- */
.scroll-progress {
  position: fixed;
  top: 0; left: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent-blue), #ff9500);
  z-index: 1001;
  width: 0%;
  transition: width 0.1s linear;
}

header.history-header {
  position: sticky;
  top: 0;
  z-index: 1000;
  width: 100%;
  background-color: var(--bg-primary);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border-color);
  padding: 8px 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.03);
}

.nav-container {
  width: 100%;
  max-width: 768px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.top-controls-row {
  display: flex;
  gap: 6px;
  align-items: center;
  width: 100%;
}

.search-input-wrap {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  width: 100%;
  padding: 7px 28px 7px 12px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  background-color: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 0.93rem;
  outline: none;
}
.search-input:focus { border-color: var(--accent-blue); }

.clear-search-btn {
  position: absolute; right: 8px;
  background: none; border: none;
  color: var(--text-secondary); font-size: 14px;
  cursor: pointer; display: none;
}

.filter-chips {
  display: flex; gap: 5px;
  overflow-x: auto; padding-bottom: 2px;
  scrollbar-width: none; width: 100%;
}
.filter-chips::-webkit-scrollbar { display: none; }

.chip {
  white-space: nowrap; padding: 4px 10px;
  border-radius: 12px; background-color: var(--bg-secondary);
  color: var(--text-secondary); font-size: 0.8rem; font-weight: 600;
  border: 1px solid transparent; cursor: pointer; flex-shrink: 0;
}
.chip.active { background-color: var(--accent-blue); color: #ffffff; border-color: var(--accent-blue); }

.era-nav-bar {
  display: flex; gap: 4px; overflow-x: auto;
  scrollbar-width: none; border-top: 1px dashed var(--border-color);
  padding-top: 5px; width: 100%;
}
.era-nav-bar::-webkit-scrollbar { display: none; }

.era-link {
  white-space: nowrap; padding: 4px 8px;
  border-radius: 8px; color: var(--text-primary);
  background: none; border: none;
  font-size: 0.8rem; font-weight: 500;
  cursor: pointer; flex-shrink: 0;
  transition: background-color 0.2s, color 0.2s;
}
.era-link:active, .era-link.active {
  background-color: rgba(0, 122, 255, 0.12);
  color: var(--accent-blue); font-weight: 700;
}

/* 🔍 Redesigned Search Result Chip Badge */
.search-chip-badge {
  display: none;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  padding: 3px 10px;
  border-radius: 12px;
  background-color: rgba(0, 122, 255, 0.1);
  color: var(--accent-blue);
  font-size: 0.78rem;
  font-weight: 700;
  align-self: flex-start;
}

.search-chip-badge button {
  background: none;
  border: none;
  color: var(--accent-blue);
  font-size: 11px;
  cursor: pointer;
  padding: 0;
  margin-left: 2px;
}

main.history-main {
  width: 100%; max-width: 768px;
  margin: 0 auto; padding: 14px 12px 90px 12px;
}

.header-card {
  background-color: var(--bg-secondary);
  border-radius: 14px; padding: 16px; margin-bottom: 20px;
  border: 1px solid var(--border-color); box-shadow: var(--card-shadow);
}

h1.main-title {
  font-size: 1.35rem; font-weight: 800; margin: 0 0 4px 0;
  background: linear-gradient(135deg, var(--accent-blue), #ff9500);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
p.sub-title { font-size: 0.85rem; color: var(--text-secondary); margin: 0 0 12px 0; }

.quick-action-row { display: flex; gap: 6px; flex-wrap: wrap; width: 100%; }

.action-btn {
  padding: 7px 12px; border-radius: 10px;
  border: 1px solid var(--border-color); background-color: var(--bg-card);
  color: var(--text-primary); font-size: 0.82rem; font-weight: 600;
  cursor: pointer; display: flex; align-items: center; gap: 4px; flex-shrink: 0;
  transition: background-color 0.2s, transform 0.1s;
}
.action-btn:active { transform: scale(0.96); }

h1.section-title {
  font-size: 1.25rem; font-weight: 800; color: var(--accent-blue);
  border-bottom: 2px solid var(--accent-blue); padding-bottom: 6px;
  margin-top: 30px; margin-bottom: 14px; scroll-margin-top: 140px;
}
h2.sub-section-title { font-size: 1.05rem; font-weight: 700; color: var(--text-primary); margin-top: 18px; margin-bottom: 8px; }

ul { padding-left: 18px; margin-top: 6px; margin-bottom: 14px; width: 100%; }
li { margin-bottom: 8px; font-size: 1rem; line-height: 1.6; word-break: break-word; overflow-wrap: break-word; }
.item-wrapper { display: inline; }

.bookmark-star {
  cursor: pointer; user-select: none; margin-right: 4px;
  font-size: 0.95rem; opacity: 0.3; transition: opacity 0.2s, transform 0.2s;
}
.bookmark-star.starred { opacity: 1; color: #ffcc00; transform: scale(1.15); }
.bold-label { font-weight: 700; color: var(--text-primary); }

.tip-card {
  background-color: var(--tip-bg); border-left: 4px solid var(--tip-border);
  padding: 10px 14px; border-radius: 8px; margin: 12px 0;
  font-size: 0.92rem; line-height: 1.6; width: 100%; word-break: break-word; overflow-wrap: break-word;
}
.tip-card-title { font-weight: 700; color: var(--tip-text-title); margin-bottom: 4px; font-size: 0.95rem; }

.badge {
  background-color: rgba(0, 122, 255, 0.15); color: var(--accent-blue);
  font-weight: 700; padding: 1px 6px; border-radius: 4px; font-size: 0.78rem; display: inline-block;
}
.flow-arrow { color: #ff9500; font-weight: 700; }
mark { background-color: #ffe600; color: #000000; border-radius: 3px; padding: 0 3px; }
hr { border: none; border-top: 1px solid var(--border-color); margin: 24px 0; }

.fab-container {
  position: fixed; bottom: 20px; right: 14px;
  display: flex; flex-direction: column; gap: 8px; z-index: 999;
}
.fab-btn {
  width: 42px; height: 42px; border-radius: 21px;
  background-color: var(--accent-blue); color: #ffffff; border: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25); font-size: 16px;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.fab-btn:active { transform: scale(0.92); }

.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background-color: rgba(0, 0, 0, 0.5); backdrop-filter: blur(6px);
  display: none; align-items: center; justify-content: center; z-index: 3000; padding: 16px;
}
.modal-card {
  background-color: var(--bg-card); width: 100%; max-width: 440px;
  border-radius: 16px; padding: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.3); border: 1px solid var(--border-color);
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-title { font-size: 16px; font-weight: 800; margin: 0; }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-secondary); }
.quiz-box {
  background-color: var(--bg-secondary); border-radius: 12px; padding: 16px;
  margin-bottom: 14px; min-height: 100px; display: flex; flex-direction: column; justify-content: center;
}
.quiz-q { font-size: 14px; font-weight: 700; margin-bottom: 10px; }
.quiz-a { font-size: 13px; color: var(--accent-blue); background-color: rgba(0, 122, 255, 0.1); padding: 8px 12px; border-radius: 8px; display: none; }
.search-hide { display: none !important; }
