:root {
  --bg: #0b1220;
  --bg-elevated: #121b30;
  --bg-card: #16213c;
  --border: #263355;
  --text: #eaf0ff;
  --text-dim: #9fb0d3;
  --text-faint: #6b7aa0;
  --accent: #3d7eff;
  --accent-2: #12b8a6;
  --danger: #ef4a5f;
  --warn: #e0a72d;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.app { min-height: 100%; display: flex; flex-direction: column; }

/* Print blocking */
.print-guard { display: none; }
@media print {
  body > *:not(.print-guard) { display: none !important; }
  .print-guard {
    display: block !important;
    padding: 4rem 2rem;
    font-size: 1.4rem;
    text-align: center;
  }
}

/* Disable text selection/copy on content areas, but keep inputs usable */
.no-select { -webkit-user-select: none; user-select: none; }
input, textarea { -webkit-user-select: text; user-select: text; }

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.9rem 1rem;
  background: rgba(11, 18, 32, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 {
  font-size: 1.05rem;
  margin: 0;
  font-weight: 600;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.icon-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  cursor: pointer;
}
.icon-btn:active { transform: scale(0.96); }

.main {
  flex: 1;
  padding: 1rem;
  padding-bottom: calc(2rem + var(--safe-bottom));
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
}

/* Auth screen */
.auth-wrap {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem 1.5rem;
  text-align: center;
}
.auth-logo {
  width: 76px; height: 76px;
  border-radius: 20px;
  margin-bottom: 1.25rem;
  box-shadow: 0 10px 30px rgba(61, 126, 255, 0.35);
}
.auth-wrap h1 { font-size: 1.4rem; margin: 0 0 0.25rem; }
.auth-wrap p.sub { color: var(--text-dim); margin: 0 0 2rem; font-size: 0.92rem; }
.auth-form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 0.85rem; }
.auth-form input {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.9rem 1rem;
  border-radius: 12px;
  font-size: 1.05rem;
  letter-spacing: 0.08em;
  text-align: center;
  text-transform: uppercase;
}
.auth-form input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border: none;
  padding: 0.9rem 1rem;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.55; }
.btn.secondary {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn.block { width: 100%; }
.auth-error {
  color: var(--danger);
  font-size: 0.88rem;
  min-height: 1.2em;
}
.auth-note {
  margin-top: 1.5rem;
  color: var(--text-faint);
  font-size: 0.78rem;
  max-width: 320px;
}

/* Home / paper cards */
.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.9rem;
}
@media (min-width: 560px) {
  .grid { grid-template-columns: repeat(2, 1fr); gap: 1.1rem; }
}
.paper-card {
  background: linear-gradient(160deg, var(--bg-card), var(--bg-elevated));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.4rem 1.1rem;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-height: 140px;
  color: var(--text);
}
.paper-card:active { transform: scale(0.98); }
.paper-card .num {
  width: 40px; height: 40px;
  border-radius: 10px;
  background: rgba(61, 126, 255, 0.18);
  color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700;
}
.paper-card .name { font-weight: 700; font-size: 1.05rem; }
.paper-card .desc { color: var(--text-dim); font-size: 0.8rem; line-height: 1.3; }

.home-header { text-align: center; margin-bottom: 1.5rem; }
.home-header .brand-mark {
  width: 56px; height: 56px; border-radius: 14px; margin-bottom: 0.75rem;
}
.home-header h1 { margin: 0 0 0.2rem; font-size: 1.3rem; }
.home-header p { margin: 0; color: var(--text-dim); font-size: 0.85rem; }

/* Question list */
.section-title {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-faint);
  margin: 1.3rem 0 0.5rem;
  padding: 0 0.1rem;
}
.section-title:first-child { margin-top: 0; }
.q-item {
  width: 100%;
  text-align: left;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.85rem 0.9rem;
  margin-bottom: 0.55rem;
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  cursor: pointer;
  color: var(--text);
}
.q-item .idx {
  flex-shrink: 0;
  width: 30px; height: 30px;
  border-radius: 8px;
  background: rgba(18, 184, 166, 0.16);
  color: var(--accent-2);
  font-size: 0.78rem;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}
.q-item .body { min-width: 0; flex: 1; }
.q-item .title { font-weight: 600; font-size: 0.95rem; line-height: 1.3; }
.q-item .meta { color: var(--text-faint); font-size: 0.76rem; margin-top: 0.25rem; }
.q-item .diff {
  flex-shrink: 0;
  font-size: 0.68rem;
  padding: 0.2rem 0.5rem;
  border-radius: 20px;
  text-transform: capitalize;
  align-self: center;
}
.diff-easy { background: rgba(18, 184, 166, 0.18); color: var(--accent-2); }
.diff-medium { background: rgba(224, 167, 45, 0.18); color: var(--warn); }
.diff-hard { background: rgba(239, 74, 95, 0.18); color: var(--danger); }

/* Detail screen */
.q-detail-head { margin-bottom: 1rem; }
.q-detail-head .crumb { color: var(--text-faint); font-size: 0.78rem; margin-bottom: 0.4rem; }
.q-detail-head h2 { margin: 0 0 0.35rem; font-size: 1.25rem; line-height: 1.35; }
.q-detail-head .source { color: var(--text-dim); font-size: 0.8rem; }
.tags { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.6rem; }
.tag {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 0.7rem;
  padding: 0.22rem 0.55rem;
  border-radius: 20px;
}

.qtext-box {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  padding: 0.9rem 1rem;
  margin: 1rem 0 1.2rem;
  font-weight: 600;
  line-height: 1.5;
}

.answer-body { line-height: 1.6; font-size: 0.95rem; }
.answer-body p { margin: 0.7rem 0; }
.answer-body h4.section-label {
  margin: 1.3rem 0 0.5rem;
  color: var(--accent-2);
  font-size: 0.95rem;
  font-weight: 700;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.3rem;
}
.answer-body ul { margin: 0.5rem 0; padding-left: 1.3rem; }
.answer-body li { margin: 0.35rem 0; }
.sub-line {
  display: block;
  margin-top: 0.2rem;
  padding-left: 1.1em;
  color: var(--text-dim);
  font-size: 0.93em;
}
.sub-line::before { content: "↳ "; }

.callout {
  border-radius: 10px;
  padding: 0.8rem 0.95rem;
  margin: 0.9rem 0;
  border: 1px solid transparent;
  font-size: 0.92rem;
}
.callout .label { font-weight: 700; display: block; margin-bottom: 0.3rem; font-size: 0.85rem; }
.callout-yellow { background: rgba(224, 167, 45, 0.14); border-color: rgba(224, 167, 45, 0.35); }
.callout-red { background: rgba(239, 74, 95, 0.14); border-color: rgba(239, 74, 95, 0.35); }
.callout-green { background: rgba(18, 184, 166, 0.14); border-color: rgba(18, 184, 166, 0.35); }
.callout-blue { background: rgba(61, 126, 255, 0.14); border-color: rgba(61, 126, 255, 0.35); }
.callout-gray { background: rgba(159, 176, 211, 0.1); border-color: var(--border); }

.table-wrap { overflow-x: auto; margin: 0.9rem 0; border: 1px solid var(--border); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; min-width: 420px; }
table th, table td { padding: 0.55rem 0.7rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
table th { background: var(--bg-elevated); color: var(--text-dim); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em; }
table tr:last-child td { border-bottom: none; }

/* Prev/Next nav */
.nav-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 0.6rem;
  padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
  background: rgba(11, 18, 32, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}
.nav-bar button {
  flex: 1;
  padding: 0.85rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}
.nav-bar button:disabled { opacity: 0.35; }
.nav-bar button.primary { background: linear-gradient(135deg, var(--accent), var(--accent-2)); border: none; color: #fff; }

/* States */
.state-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 4rem 1.5rem;
  gap: 0.9rem;
  color: var(--text-dim);
}
.spinner {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.state-wrap .msg { font-size: 0.92rem; max-width: 320px; }
.state-wrap .err-icon { font-size: 2rem; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.2rem + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #1c2740;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.65rem 1rem;
  border-radius: 10px;
  font-size: 0.85rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 999;
  max-width: 88vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.install-banner {
  margin: 0 0 1rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.8rem 0.9rem;
  font-size: 0.82rem;
  color: var(--text-dim);
  display: flex;
  gap: 0.6rem;
  align-items: center;
  justify-content: space-between;
}
.install-banner button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}
