:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --card: #0f3460;
  --accent: #e94560;
  --accent2: #f5a623;
  --text: #eaeaea;
  --text-dim: #8892a4;
  --done: #1e4d38;
  --done-text: #74c69d;
  --current-bg: #2a1f0e;
  --current-border: #f5a623;
  --border: #1e3a5f;
  --radius: 10px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ── Screens ───────────────────────────────── */
.screen { display: none; flex-direction: column; height: 100vh; overflow: hidden; }
.screen.active { display: flex; }

/* ── Header ────────────────────────────────── */
.header {
  background: var(--surface);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.header h1 { font-size: 17px; font-weight: 700; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header .sub { font-size: 11px; color: var(--text-dim); margin-top: 1px; }
.back-btn { background: none; border: none; color: var(--accent); font-size: 24px; cursor: pointer; padding: 0 4px; line-height: 1; flex-shrink: 0; }
.lang-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 5px;
  cursor: pointer;
  flex-shrink: 0;
}

/* ── Progress ──────────────────────────────── */
.progress-bar {
  flex-shrink: 0;
  background: var(--surface);
  padding: 8px 16px 10px;
  border-bottom: 1px solid var(--border);
}
.progress-labels { display: flex; justify-content: space-between; font-size: 11px; color: var(--text-dim); margin-bottom: 5px; }
.progress-track { background: var(--bg); border-radius: 4px; height: 6px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent2)); border-radius: 4px; transition: width .3s; }

/* ── Area List ─────────────────────────────── */
#areaList {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.area-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 12px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border: 1px solid var(--border);
  -webkit-tap-highlight-color: transparent;
  transition: background .12s;
}
.area-card:active { background: #1a4a80; }
.area-name-ko { font-weight: 700; font-size: 15px; }
.area-name-en { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.area-progress { font-size: 12px; color: var(--text-dim); background: var(--bg); padding: 3px 8px; border-radius: 20px; white-space: nowrap; margin-left: 8px; flex-shrink: 0; }
.area-done { color: var(--done-text); }

/* ── 2-column layout ───────────────────────── */
.area-body {
  flex: 1;
  display: flex;
  overflow: hidden; /* each column scrolls independently */
  min-height: 0;
}

/* Left: quest list — own scroll */
.quest-panel {
  width: 55%;
  border-right: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.panel-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .05em;
  padding: 8px 12px 6px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
  display: flex;
  align-items: center;
}
#questList { padding: 8px; overflow-y: auto; flex: 1; }

/* Right panel — fixed alongside left, own internal scroll */
.right-panel {
  width: 45%;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.items-panel {
  flex: 1;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.rewards-panel {
  height: 36%;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.panel-scroll { flex: 1; overflow-y: auto; padding: 8px; }

/* view toggle button in panel title */
.view-toggle {
  margin-left: auto;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 9px;
  padding: 1px 7px;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
}

/* compact icon grid */
.items-compact-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.item-chip {
  position: relative;
  flex-shrink: 0;
  cursor: default;
  width: 48px;
  height: 48px;
}
.item-chip img,
.item-chip-ph {
  width: 44px;
  height: 44px;
  object-fit: contain;
  border-radius: 8px;
  border: 1px solid var(--border);
  display: block;
}
.item-chip-ph {
  background: var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
  color: var(--text-dim);
}
.item-chip-qty {
  position: absolute; bottom: 0; right: 0;
  background: var(--accent2); color: #000;
  font-size: 9px; font-weight: 700;
  padding: 0 3px; border-radius: 4px; line-height: 14px;
  pointer-events: none;
}
.item-chip-lv {
  position: absolute; top: 0; left: 0;
  background: rgba(74,144,226,0.9); color: #fff;
  font-size: 8px; font-weight: 700;
  padding: 0 2px; border-radius: 3px; line-height: 13px;
  pointer-events: none;
}

/* Mobile: stack vertically, no page scroll */
@media (max-width: 640px) {
  .area-body { flex-direction: column; overflow: hidden; }
  .quest-panel {
    width: 100%; border-right: none;
    border-bottom: 1px solid var(--border);
    flex: 1; min-height: 0;
  }
  .right-panel {
    width: 100%; flex: 1; min-height: 0;
    flex-direction: row; overflow: hidden;
  }
  .items-panel { min-height: 0; max-height: none; border-bottom: none; border-right: 1px solid var(--border); }
  .rewards-panel { width: 38%; height: auto; min-height: 0; flex-shrink: 0; }
}


/* ── Swimlane row-based branch layout ──────── */
.swimlane-body { display: flex; flex-direction: column; }
.level-row { display: flex; gap: 6px; }
/* Row-to-row connector gap — holds per-row-transition SVG bezier curves */
.row-connector {
  height: 14px;
  position: relative;
  width: 100%;
  flex-shrink: 0;
}
.row-connector svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
/* Active lane cell: equal share of remaining width */
.level-cell {
  flex: 1 1 0;
  min-width: 0;
  container-type: inline-size;
}
/* Exhausted lane cell: thin blue strip, no content */
.level-strip {
  flex: 0 0 4px;
  background: rgba(74, 144, 226, 0.75);
  border-radius: 2px;
  align-self: stretch;
}

/* ── Compact quest layout for narrow lane cells ─── */
@container (max-width: 90px) {
  .quest-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
    padding: 5px 6px;
  }
  .quest-desc, .quest-xp, .quest-unlock, .done-tag { display: none; }
  .quest-reqs { max-width: 100%; justify-content: flex-start; }
  .quest-idx { font-size: 11px; font-weight: 600; }
}

/* Branch connector gaps — hold the split/merge SVG curves */
.branch-split-gap,
.branch-merge-gap {
  position: relative;
  width: 100%;
  flex-shrink: 0;
}
.branch-split-gap { height: 36px; }
.branch-merge-gap { height: 52px; }
.branch-split-gap svg,
.branch-merge-gap svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}

/* ── Quest items ───────────────────────────── */
.quest-item {
  padding: 8px 10px;
  margin-bottom: 5px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background .1s;
  display: flex; align-items: center; gap: 8px;
}
.quest-item.done { background: var(--done); border-color: #2d6a4f; }
.quest-item.quest-virtual { cursor: default; opacity: 0.55; border-style: dashed; pointer-events: none; }
.quest-virtual-sync {
  height: 4px;
  margin: 2px 0 4px;
  background: rgba(74, 144, 226, 0.35);
  border-radius: 2px;
}
.quest-item.current { background: var(--current-bg); border-color: var(--current-border); box-shadow: 0 0 0 1px var(--current-border); }
.quest-item.locked { opacity: 0.45; }
.quest-header { display: flex; align-items: baseline; gap: 6px; overflow: hidden; }
.quest-idx { font-size: 10px; color: var(--text-dim); flex-shrink: 0; }
.quest-item.done .quest-idx { color: var(--done-text); }
.quest-desc { font-size: 13px; font-weight: 500; line-height: 1.35; flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; min-width: 0; }
.quest-item.done .quest-desc { color: var(--done-text); }
.done-tag { font-size: 9px; font-weight: 700; background: var(--done-text); color: #000; padding: 1px 4px; border-radius: 8px; flex-shrink: 0; }
.quest-xp { font-size: 10px; color: var(--accent2); margin-top: 3px; }
.quest-unlock { font-size: 10px; color: var(--text-dim); margin-top: 2px; }
.quest-unlock span { color: var(--accent2); font-weight: 600; }
.quest-left { flex: 1; min-width: 0; }
.quest-reqs {
  display: flex; flex-wrap: wrap; gap: 2px;
  align-items: center; justify-content: flex-end;
  flex-shrink: 0; max-width: 42%;
}

/* ── Compact req chips ─────────────────────── */
.req-chip {
  position: relative; display: inline-block;
  width: 30px; height: 30px;
  vertical-align: middle; margin: 1px; flex-shrink: 0;
}
.req-chip img {
  width: 26px; height: 26px; object-fit: contain;
  border-radius: 5px; border: 1px solid var(--border); display: block;
}
.req-chip-ph {
  width: 26px; height: 26px; background: var(--border);
  border-radius: 5px; display: flex; align-items: center;
  justify-content: center; font-size: 7px; color: var(--text-dim);
}
.req-chip-qty {
  position: absolute; bottom: 0; right: -1px;
  background: var(--accent2); color: #000; font-size: 7px; font-weight: 700;
  padding: 0 2px; border-radius: 3px; line-height: 11px;
  pointer-events: none; font-style: normal;
}
.req-chip-lv {
  position: absolute; top: 0; left: -1px;
  background: rgba(74,144,226,0.9); color: #fff; font-size: 7px; font-weight: 700;
  padding: 0 2px; border-radius: 3px; line-height: 11px;
  pointer-events: none; font-style: normal;
}
.quest-item.done .req-chip { opacity: .5; }

/* ── Items needed ──────────────────────────── */
.item-row {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 6px;
  border-radius: var(--radius);
  background: var(--card);
  border: 1px solid var(--border);
  margin-bottom: 4px;
}
.item-row img { width: 28px; height: 28px; object-fit: contain; border-radius: 5px; flex-shrink: 0; }
.item-icon-ph { width: 28px; height: 28px; background: var(--border); border-radius: 5px; display: flex; align-items: center; justify-content: center; font-size: 9px; color: var(--text-dim); flex-shrink: 0; }
.item-info { flex: 1; min-width: 0; }
.item-name { font-size: 11px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-chain { font-size: 9px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-qty { font-size: 13px; font-weight: 700; color: var(--accent2); flex-shrink: 0; }

/* ── XP chip (reuses item-chip base) ──────── */
.xp-chip-inner {
  width: 44px; height: 44px; background: var(--card);
  border: 1px solid var(--border); border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
}

/* ── Empty / footer ────────────────────────── */
.empty { text-align: center; padding: 20px 12px; color: var(--text-dim); font-size: 12px; }
.ver-badge { font-size: 10px; color: var(--text-dim); opacity: .6; flex-shrink: 0; align-self: flex-start; margin-top: 2px; }
.footer { text-align: center; padding: 14px 16px; font-size: 10px; color: var(--text-dim); border-top: 1px solid var(--border); flex-shrink: 0; }
.footer a { color: var(--accent); text-decoration: none; }

/* ── Loading ───────────────────────────────── */
#loading {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; z-index: 999;
}
.spinner { width: 36px; height: 36px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#loading p { color: var(--text-dim); font-size: 13px; }

/* ── Flat view ─────────────────────────────── */
.view-toggle-btn {
  font-size: 11px; padding: 3px 8px; border-radius: 12px;
  background: var(--card); border: 1px solid var(--border);
  color: var(--text); cursor: pointer; flex-shrink: 0;
  transition: background .15s;
}
.view-toggle-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.flat-body { display: flex; flex-direction: column; }

/* Row = [좌측 유령 슬롯들] [활성 영역(flex:1)] [우측 유령 슬롯들] */
.flat-row {
  display: flex;
  align-items: stretch;
  gap: 4px;
  padding: 0 4px;
}
.flat-ghost-slot {
  width: 12px;
  flex-shrink: 0;
  position: relative;
}
.flat-ghost-slot.occupied::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: 50%; width: 2px;
  transform: translateX(-50%);
  background: rgba(74,144,226,0.55);
  pointer-events: none;
}
.flat-active-area {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  min-width: 0;
}
.flat-active-area.wide { grid-template-columns: 1fr; }
.flat-cell { min-width: 0; position: relative; }
.flat-cell-wide { grid-column: 1 / -1; }
.flat-passthrough::before {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  left: 50%; width: 2px;
  transform: translateX(-50%);
  background: rgba(74,144,226,0.45);
  pointer-events: none;
}
.flat-empty { background: transparent; }

/* Connector: 유령 슬롯 + 활성영역 동일 폭 분할, SVG는 그 위에 overlay */
.flat-connector {
  height: 32px; position: relative;
  display: flex; align-items: stretch;
  gap: 4px; padding: 0 4px;
}
.flat-connector .flat-ghost-slot.occupied::before {
  background: rgba(74,144,226,0.55);
}
.flat-connector svg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  overflow: visible; pointer-events: none;
}
