/* ── Reset & tokens ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0d0d1a;
  --surface:  #1a1a2e;
  --card:     #16213e;
  --border:   #2a2a4a;
  --accent:   #7c3aed;
  --accent2:  #9d6eff;
  --gold:     #f59e0b;
  --hp:       #ef4444;
  --hp-low:   #dc2626;
  --success:  #10b981;
  --text:     #e2e8f0;
  --muted:    #94a3b8;
  --easy:     #10b981;
  --medium:   #f59e0b;
  --hard:     #ef4444;
  --radius:   12px;
  --nav-h:    60px;
  --safe-b:   env(safe-area-inset-bottom, 0px);
}

html { height: 100%; height: -webkit-fill-available; }
body {
  height: 100svh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ── Screen system ── */
.screen { display: none; position: fixed; inset: 0; flex-direction: column; }
.screen.active { display: flex; }

/* ── Auth ── */
.auth-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 24px 20px;
  max-width: 420px;
  width: 100%;
  margin: 0 auto;
  overflow-y: auto;
}

.auth-logo {
  text-align: center;
  margin-bottom: 32px;
}
.logo-sword { font-size: 56px; line-height: 1; margin-bottom: 8px; }
.auth-logo h1 { font-size: 32px; font-weight: 800; letter-spacing: -0.5px; color: var(--accent2); }
.auth-logo p  { color: var(--muted); margin-top: 4px; }

.auth-tabs {
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 3px;
  width: 100%;
  margin-bottom: 20px;
}
.auth-tab {
  flex: 1; padding: 8px; border: none; border-radius: 9px;
  background: none; color: var(--muted); font-size: 14px; font-weight: 600; cursor: pointer;
  transition: all .2s;
}
.auth-tab.active { background: var(--accent); color: #fff; }

.auth-form { width: 100%; }
.auth-form.hidden { display: none; }

/* ── Form fields ── */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field input, .field textarea {
  width: 100%; padding: 10px 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text); font-size: 15px;
  outline: none; transition: border-color .2s;
  -webkit-appearance: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent2); }
.field textarea { resize: vertical; font-family: inherit; }
.optional { font-weight: 400; }

.form-error { color: var(--hp); font-size: 13px; margin-bottom: 8px; }
.form-error.hidden { display: none; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 18px; border: none; border-radius: 8px;
  font-size: 14px; font-weight: 700; cursor: pointer; transition: all .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn.primary  { background: var(--accent);  color: #fff; }
.btn.primary:hover { background: var(--accent2); }
.btn.ghost    { background: var(--surface); border: 1px solid var(--border); color: var(--muted); }
.btn.ghost:hover  { border-color: var(--accent2); color: var(--text); }
.btn.danger   { background: var(--hp); color: #fff; }
.btn.full     { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ── Google button ── */
.auth-divider {
  display: flex; align-items: center; gap: 10px;
  width: 100%; margin: 14px 0;
}
.auth-divider::before, .auth-divider::after {
  content: ''; flex: 1; height: 1px; background: var(--border);
}
.auth-divider span { color: var(--muted); font-size: 12px; white-space: nowrap; }

.google-btn {
  background: #fff; color: #3c4043; border: 1px solid #dadce0;
  font-weight: 600; font-size: 14px; text-decoration: none;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.google-btn:hover { background: #f8f8f8; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.google-btn:active { background: #f1f3f4; transform: scale(.97); }

/* ── App layout — mobile-safe ── */
#app-screen {
  display: none;
  flex-direction: column;
  position: fixed;
  inset: 0;
  /* Account for notch at top on iOS */
  padding-top: env(safe-area-inset-top, 0px);
}
#app-screen.active { display: flex; }

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.header-title { font-size: 17px; font-weight: 700; }
.header-stats { display: flex; gap: 6px; align-items: center; }
.stat-pill {
  padding: 3px 8px; border-radius: 20px; font-size: 12px; font-weight: 700;
  background: var(--bg); border: 1px solid var(--border);
}
.stat-pill.hp   { border-color: var(--hp);    color: var(--hp); }
.stat-pill.gold { border-color: var(--gold);   color: var(--gold); }
.stat-pill.level { border-color: var(--accent2); color: var(--accent2); }

.app-main {
  flex: 1;
  overflow: hidden;
  position: relative;
  min-height: 0;
}

.page {
  display: none;
  position: absolute;
  inset: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px;
  padding-bottom: 14px;
}
.page.active { display: block; }

/* ── Bottom nav — sticks above OS navigation bar ── */
.bottom-nav {
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  /* Push content above OS home indicator / navigation bar */
  padding-bottom: max(env(safe-area-inset-bottom, 0px), 8px);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  padding: 8px 4px 6px; border: none; background: none; color: var(--muted); cursor: pointer;
  transition: color .2s;
}
.nav-btn.active { color: var(--accent2); }
.nav-icon  { font-size: 22px; line-height: 1; }
.nav-label { font-size: 10px; font-weight: 600; margin-top: 2px; }

/* ── XP bar ── */
.xp-bar-wrap { margin-bottom: 14px; }
.xp-bar {
  height: 8px; background: var(--surface); border-radius: 4px;
  border: 1px solid var(--border); overflow: hidden;
}
.xp-fill {
  height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 4px; transition: width .4s ease;
  width: 0%;
}
.xp-label { font-size: 11px; color: var(--muted); margin-top: 3px; display: block; text-align: right; }

/* ── Quest toolbar ── */
.quest-toolbar { margin-bottom: 12px; }

/* ── Quest list ── */
.quest-list { display: flex; flex-direction: column; gap: 8px; }
.quest-empty { text-align: center; color: var(--muted); padding: 40px 0; }

.quest-card {
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
  transition: opacity .2s;
}
.quest-card.done { opacity: .55; }

.quest-check {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border);
  background: none; cursor: pointer; flex-shrink: 0; margin-top: 1px;
  transition: all .2s; display: flex; align-items: center; justify-content: center;
}
.quest-check.checked { background: var(--success); border-color: var(--success); color: #fff; }
.quest-check.checked::after { content: '✓'; font-size: 12px; font-weight: 700; }

.quest-body { flex: 1; min-width: 0; }
.quest-title { font-weight: 600; font-size: 15px; line-height: 1.3; }
.quest-desc  { color: var(--muted); font-size: 13px; margin-top: 2px; }
.quest-meta  { display: flex; gap: 6px; align-items: center; margin-top: 6px; flex-wrap: wrap; }

.diff-badge {
  display: inline-block; padding: 1px 7px; border-radius: 20px;
  font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .3px;
}
.diff-badge.easy   { background: rgba(16,185,129,.15); color: var(--easy);   border: 1px solid var(--easy); }
.diff-badge.medium { background: rgba(245,158,11,.15);  color: var(--medium); border: 1px solid var(--medium); }
.diff-badge.hard   { background: rgba(239,68,68,.15);   color: var(--hard);   border: 1px solid var(--hard); }

/* Quest type badges */
.type-badge {
  display: inline-block; padding: 1px 6px; border-radius: 20px;
  font-size: 11px; font-weight: 600; border: 1px solid var(--border);
  color: var(--muted); background: var(--surface);
}
.type-badge.daily  { color: var(--accent2); border-color: var(--accent2); background: rgba(124,58,237,.1); }
.type-badge.weekly { color: #06b6d4;        border-color: #06b6d4;        background: rgba(6,182,212,.1); }
.type-badge.habit  { color: var(--success); border-color: var(--success); background: rgba(16,185,129,.1); }
.type-badge.once   { color: var(--gold);    border-color: var(--gold);    background: rgba(245,158,11,.1); }

.quest-actions { display: flex; gap: 6px; margin-left: auto; flex-shrink: 0; }
.icon-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 15px; padding: 4px; border-radius: 4px; line-height: 1;
}
.icon-btn:hover { color: var(--text); }
.icon-btn.del:hover { color: var(--hp); }

/* ── Quest type pills (in modal) ── */
.diff-opt input:checked + .diff-pill.qtype-daily  { background: rgba(124,58,237,.2);  color: var(--accent2); border-color: var(--accent2); }
.diff-opt input:checked + .diff-pill.qtype-weekly { background: rgba(6,182,212,.2);   color: #06b6d4;        border-color: #06b6d4; }
.diff-opt input:checked + .diff-pill.qtype-habit  { background: rgba(16,185,129,.2);  color: var(--success); border-color: var(--success); }
.diff-opt input:checked + .diff-pill.qtype-once   { background: rgba(245,158,11,.2);  color: var(--gold);    border-color: var(--gold); }
.diff-pill.qtype-daily  { background: var(--surface); color: var(--muted); border-color: var(--border); }
.diff-pill.qtype-weekly { background: var(--surface); color: var(--muted); border-color: var(--border); }
.diff-pill.qtype-habit  { background: var(--surface); color: var(--muted); border-color: var(--border); }
.diff-pill.qtype-once   { background: var(--surface); color: var(--muted); border-color: var(--border); }

/* ── Character view ── */
.char-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
}
.char-name   { font-size: 22px; font-weight: 800; color: var(--accent2); margin-bottom: 2px; }
.char-sub    { color: var(--muted); font-size: 13px; }
.char-xp-bar { margin: 10px 0 4px; }

/* ── Hero avatar card ── */
.hero-avatar-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
  display: flex; align-items: center; gap: 16px; margin-bottom: 12px;
}
.hero-avatar-canvas-wrap {
  background: var(--surface); border: 2px solid var(--border);
  border-radius: 12px; padding: 6px; flex-shrink: 0;
}
.hero-avatar-info { flex: 1; min-width: 0; }
.hero-avatar-info .char-name { font-size: 20px; margin-bottom: 4px; }
.hero-avatar-info .char-sub  { margin-bottom: 10px; }

.stats-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px;
}
.stat-box {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
}
.stat-box .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.stat-box .value { font-size: 24px; font-weight: 800; line-height: 1.1; margin-top: 2px; }
.stat-box .bonus { font-size: 11px; color: var(--success); }

.equip-row {
  display: flex; gap: 8px; margin-bottom: 12px;
}
.equip-slot {
  flex: 1; background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px; text-align: center;
}
.equip-slot .slot-icon  { font-size: 22px; }
.equip-slot .slot-label { font-size: 11px; color: var(--muted); margin-top: 2px; }
.equip-slot .slot-name  { font-size: 12px; font-weight: 600; margin-top: 2px; }

.stage-info {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; font-size: 13px;
}
.stage-info .stage-label { font-weight: 700; color: var(--accent2); }

/* ── HP bar (reusable) ── */
.hp-bar-wrap { margin: 6px 0 2px; }
.hp-bar {
  height: 10px; background: var(--surface); border-radius: 5px;
  border: 1px solid var(--border); overflow: hidden;
}
.hp-fill {
  height: 100%; background: var(--success); border-radius: 5px; transition: width .3s;
}
.hp-fill.low  { background: var(--medium); }
.hp-fill.crit { background: var(--hp); }
.hp-label { font-size: 11px; color: var(--muted); display: block; text-align: right; margin-top: 2px; }

/* ── Pixel canvas (crisp rendering) ── */
.pixel-canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
}

/* ── Combat view ── */
.combat-arena {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 12px;
}
.arena-fighters {
  display: flex; justify-content: space-around; align-items: flex-end;
  margin-bottom: 12px; gap: 8px;
}
.fighter {
  display: flex; flex-direction: column; align-items: center; gap: 4px; flex: 1;
}
.fighter-label {
  font-size: 11px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .4px; text-align: center;
}
.fighter canvas { display: block; }
.vs-label { font-size: 18px; font-weight: 900; color: var(--border); align-self: center; }

.combatant { margin-bottom: 12px; }
.combatant-name { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 6px; }
.boss-badge {
  display: inline-block; background: rgba(239,68,68,.2); color: var(--hp);
  border: 1px solid var(--hp); border-radius: 4px;
  font-size: 10px; font-weight: 700; padding: 1px 5px;
}

.battle-log {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px; max-height: 130px; overflow-y: auto;
  margin-bottom: 12px; font-size: 13px;
}
.log-entry { padding: 3px 0; border-bottom: 1px solid var(--border); }
.log-entry:last-child { border-bottom: none; }
.log-entry.player  { color: var(--accent2); }
.log-entry.monster { color: var(--hp); }
.log-entry.system  { color: var(--muted); font-style: italic; }
.log-entry.success { color: var(--success); }

.combat-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.combat-actions .btn { flex: 1; min-width: 120px; }

.no-battle-box {
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 0 8px;
}
.no-battle-box p { color: var(--muted); margin-bottom: 14px; }
.penalty-notice {
  background: rgba(239,68,68,.1); border: 1px solid var(--hp);
  border-radius: 8px; padding: 10px 12px; margin-bottom: 12px;
  font-size: 13px; color: var(--hp);
}

/* ── Shop view ── */
.shop-gold {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 14px; margin-bottom: 12px;
  font-size: 15px; font-weight: 700; color: var(--gold);
  display: flex; align-items: center; gap: 6px;
}
.item-section { margin-bottom: 8px; }
.item-section h3 { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin-bottom: 8px; }
.item-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }

.item-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
  display: flex; align-items: center; gap: 10px;
}
.item-card.equipped { border-color: var(--success); }
.item-icon {
  width: 48px; height: 48px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
.item-body { flex: 1; min-width: 0; }
.item-name  { font-weight: 700; font-size: 14px; }
.item-desc  { color: var(--muted); font-size: 12px; margin-top: 1px; }
.item-stats { font-size: 12px; margin-top: 3px; color: var(--accent2); }
.item-price { font-size: 13px; font-weight: 700; color: var(--gold); white-space: nowrap; }
.item-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.equipped-tag { font-size: 11px; color: var(--success); font-weight: 700; }
.locked-tag   { font-size: 11px; color: var(--muted); }

/* ── Modal ── */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; }
.modal.hidden { display: none; }

.modal-backdrop {
  position: absolute; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(2px);
}
.modal-box {
  position: relative; z-index: 1;
  background: var(--card); border: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  padding: 20px 20px;
  padding-bottom: max(env(safe-area-inset-bottom, 0px), 20px);
  width: 100%; max-width: 520px; margin: 0 auto;
  animation: slide-up .25s ease;
  max-height: 90svh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
@keyframes slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.modal-title { font-size: 18px; font-weight: 700; margin-bottom: 14px; }

.diff-group { display: flex; gap: 6px; flex-wrap: wrap; }
.diff-opt { cursor: pointer; }
.diff-opt input { display: none; }
.diff-pill {
  display: inline-block; padding: 5px 10px; border-radius: 20px;
  font-size: 12px; font-weight: 700; border: 1px solid transparent;
  transition: all .15s; cursor: pointer;
}
.diff-opt input:checked + .diff-pill.easy   { background: rgba(16,185,129,.2);  color: var(--easy);   border-color: var(--easy); }
.diff-opt input:checked + .diff-pill.medium { background: rgba(245,158,11,.2);  color: var(--medium); border-color: var(--medium); }
.diff-opt input:checked + .diff-pill.hard   { background: rgba(239,68,68,.2);   color: var(--hard);   border-color: var(--hard); }
.diff-pill.easy   { background: var(--surface); color: var(--easy);   border-color: var(--border); }
.diff-pill.medium { background: var(--surface); color: var(--medium); border-color: var(--border); }
.diff-pill.hard   { background: var(--surface); color: var(--hard);   border-color: var(--border); }

.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

/* ── Hero Editor ── */
.hero-editor { display: flex; flex-direction: column; gap: 14px; }

.hero-preview-wrap {
  display: flex; justify-content: center; align-items: center;
  padding: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
}
.hero-preview-canvas { border-radius: 4px; }

.swatch-row { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
  width: 34px; height: 34px; border-radius: 50%;
  cursor: pointer; border: 3px solid var(--border);
  transition: border-color .15s, transform .15s;
  flex-shrink: 0;
}
.swatch:hover   { transform: scale(1.1); }
.swatch.active  { border-color: var(--accent2); transform: scale(1.1); }

/* ── Toasts ── */
#toasts {
  position: fixed;
  bottom: calc(var(--nav-h) + max(env(safe-area-inset-bottom, 0px), 8px) + 8px);
  left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 6px; align-items: center;
  z-index: 200; pointer-events: none; width: 320px;
}
.toast {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 14px; font-size: 13px; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,.4);
  animation: fade-in .2s ease;
  pointer-events: auto; text-align: center;
}
.toast.success { border-color: var(--success); color: var(--success); }
.toast.error   { border-color: var(--hp);      color: var(--hp); }
.toast.info    { border-color: var(--accent2);  color: var(--accent2); }
@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Misc ── */
.loading { text-align: center; color: var(--muted); padding: 32px 0; }
.section-title {
  font-size: 13px; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .5px; margin-bottom: 10px;
}

/* ── Desktop centering ── */
@media (min-width: 520px) {
  #auth-screen { align-items: center; }
  #app-screen  { max-width: 480px; margin: 0 auto; }
}
