/* Elemental: theme tokens, layout and components. Light by default, dark via prefers-color-scheme. */
:root {
  --bg: #f6f3ff;
  --bg-2: #ece7ff;
  --panel: #ffffff;
  --panel-2: #f7f5fd;
  --ink: #221d38;
  --ink-2: #5d5878;
  --ink-3: #8c88a6;
  --line: #e2dcf5;
  --accent: #6c5ce7;
  --accent-2: #ff7a59;
  --good: #06b67a;
  --bad: #e5484d;
  --dot: rgba(108, 92, 231, 0.13);
  --shadow: 0 1px 2px rgba(34, 29, 56, 0.06), 0 4px 14px rgba(34, 29, 56, 0.08);
  --shadow-lift: 0 6px 16px rgba(34, 29, 56, 0.16), 0 18px 40px rgba(34, 29, 56, 0.14);
  --radius: 14px;
  --font: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --display: 'Fredoka', 'Nunito', system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14121f;
    --bg-2: #1b1830;
    --panel: #201d33;
    --panel-2: #29253f;
    --ink: #f1eeff;
    --ink-2: #b8b2d6;
    --ink-3: #847ea6;
    --line: #353052;
    --accent: #9d8cff;
    --accent-2: #ff9474;
    --dot: rgba(157, 140, 255, 0.12);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 14px rgba(0, 0, 0, 0.25);
    --shadow-lift: 0 8px 20px rgba(0, 0, 0, 0.45), 0 20px 44px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* --- top bar ----------------------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: none;
  z-index: 5;
}
.logo {
  display: flex; align-items: center; gap: 10px;
  font: 700 24px/1 var(--display);
  color: var(--ink);
  text-decoration: none;
  letter-spacing: 0.2px;
}
.logo-orbs { display: grid; grid-template-columns: 10px 10px; gap: 3px; }
.logo-orbs i { width: 10px; height: 10px; border-radius: 50%; }
.logo-orbs i:nth-child(1) { background: #9ad7ff; } /* air */
.logo-orbs i:nth-child(2) { background: #339af0; } /* water */
.logo-orbs i:nth-child(3) { background: #a0785a; } /* earth */
.logo-orbs i:nth-child(4) { background: #ff7a59; } /* fire */
.progress { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; max-width: 340px; }
.progress-count { font-weight: 700; color: var(--ink-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.progress-count b { color: var(--ink); font-size: 18px; }
.progress-bar { flex: 1; height: 10px; border-radius: 99px; background: var(--bg-2); overflow: hidden; }
.progress-bar i { display: block; height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, #06d6a0, #4dabf7, #6c5ce7, #ff7a59); transition: width 0.6s cubic-bezier(.2,.8,.2,1); }
.actions { display: flex; gap: 8px; margin-left: auto; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 999px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-2); border-color: var(--accent); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; border-color: var(--line); }
.btn.small { padding: 5px 11px; font-size: 13px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.danger:hover { border-color: var(--bad); }
.btn-hint { background: #fff4d6; border-color: #ffd977; }
@media (prefers-color-scheme: dark) { .btn-hint { background: #3a3220; border-color: #7a6326; } }

/* --- main layout ------------------------------------------------------------------------- */
.layout { flex: 1; display: flex; min-height: 0; }
.board {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1.5px, transparent 1.6px);
  background-size: 22px 22px;
  touch-action: none;
}
.board.drop-ok { box-shadow: inset 0 0 0 3px var(--accent); }
.board-empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; gap: 4px;
  color: var(--ink-3);
  pointer-events: none;
}
.board-empty[hidden] { display: none; }
.board-empty .big { font: 600 22px/1.25 var(--display); color: var(--ink-2); margin: 0; }
.board-empty p { margin: 0; max-width: 420px; }
.board-tools { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 8px; z-index: 3; }

.inventory {
  width: 340px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--line);
  min-height: 0;
}
.inventory.drop-remove { box-shadow: inset 0 0 0 3px var(--bad); }
.inv-head { display: flex; gap: 8px; padding: 12px 12px 6px; }
.search { flex: 1; }
.search input, .sort select, .field {
  width: 100%;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  outline: none;
}
.search input:focus, .sort select:focus, .field:focus { border-color: var(--accent); }
.sort select { width: auto; padding-right: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; padding: 4px 12px 6px; }
.chip {
  border: 1px solid var(--line); background: transparent; border-radius: 99px;
  padding: 2px 9px; font-size: 12px; font-weight: 700; cursor: pointer; color: var(--ink-2);
}
.chip[aria-pressed='true'] { background: var(--c, var(--accent)); border-color: var(--c, var(--accent)); color: #fff; }
.chip .n { opacity: 0.7; font-weight: 600; margin-left: 3px; }
.inv-info { margin: 0; padding: 0 14px 6px; font-size: 12px; color: var(--ink-3); min-height: 18px; }
.inv-grid {
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 16px;
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 6px;
}
.inv-empty { color: var(--ink-3); padding: 12px 2px; }

/* --- element tiles (inventory, board, encyclopedia) -------------------------------------- */
.tile, .item {
  --c: var(--accent);
  position: relative;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 11px 5px 7px;
  border-radius: 12px;
  border: 1px solid var(--line);
  border-left: 4px solid var(--c);
  background: var(--panel);
  box-shadow: var(--shadow);
  font-weight: 700;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  white-space: nowrap;
  line-height: 1.2;
}
.tile:hover { border-color: var(--c); border-left-color: var(--c); transform: translateY(-1px); }
.tile .icon, .item .icon { font-size: 20px; line-height: 1; width: 24px; text-align: center; }
.tile.final::after, .item.final::after {
  content: '★'; font-size: 9px; color: var(--c);
  position: absolute; top: 1px; right: 3px;
}
.tile.fresh { animation: fresh 1.2s ease-in-out 3; }
.tile.fresh::before {
  content: 'new'; position: absolute; top: -7px; right: -5px;
  background: var(--accent-2); color: #fff; font-size: 9px; padding: 0 5px; border-radius: 99px; text-transform: uppercase; letter-spacing: 0.5px;
}
.tile.hinted, .item.hinted { box-shadow: 0 0 0 3px #ffd166, var(--shadow); }
@keyframes fresh { 50% { box-shadow: 0 0 0 4px color-mix(in srgb, var(--c) 35%, transparent), var(--shadow); } }

.item {
  position: absolute;
  transform: translate(-50%, -50%);
  z-index: 1;
  touch-action: none;
  transition: box-shadow 0.15s;
}
.item:hover { z-index: 2; }
.item.selected { box-shadow: 0 0 0 3px var(--c), var(--shadow-lift); }
.item.target { box-shadow: 0 0 0 4px var(--c), var(--shadow-lift); transform: translate(-50%, -50%) scale(1.1); transition: transform 0.12s; }
.item.dragging { opacity: 0.25; }
.item.pop { animation: pop 0.55s cubic-bezier(.2,1.6,.4,1); }
.item.shake { animation: shake 0.4s; }
@keyframes pop { 0% { transform: translate(-50%, -50%) scale(0.2); } 100% { transform: translate(-50%, -50%) scale(1); } }
@keyframes shake {
  0%, 100% { transform: translate(-50%, -50%); }
  20% { transform: translate(calc(-50% - 7px), -50%); }
  40% { transform: translate(calc(-50% + 6px), -50%); }
  60% { transform: translate(calc(-50% - 4px), -50%); }
  80% { transform: translate(calc(-50% + 2px), -50%); }
}

.ghost {
  position: fixed; left: 0; top: 0; z-index: 50;
  pointer-events: none;
  cursor: grabbing;
  box-shadow: var(--shadow-lift);
  transform: rotate(-3deg) scale(1.06);
}
.ghost.returning { transition: transform 0.32s cubic-bezier(.3,.7,.3,1), opacity 0.32s; }
.ghost.removing { transition: transform 0.25s, opacity 0.25s; opacity: 0; }
body.is-dragging, body.is-dragging * { cursor: grabbing !important; }

/* combination burst */
.burst { position: absolute; width: 0; height: 0; pointer-events: none; z-index: 4; }
.burst i {
  position: absolute; left: -5px; top: -5px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--c);
  animation: burst 0.8s cubic-bezier(.1,.8,.3,1) forwards;
}
.burst i:nth-child(3n) { background: #ffd166; }
.burst i:nth-child(4n) { width: 7px; height: 7px; border-radius: 2px; }
@keyframes burst {
  from { transform: rotate(var(--a)) translateX(0) scale(1); opacity: 1; }
  to { transform: rotate(var(--a)) translateX(var(--d)) scale(0.2); opacity: 0; }
}
.ring {
  position: absolute; width: 20px; height: 20px; margin: -10px 0 0 -10px; border-radius: 50%;
  border: 3px solid var(--c); pointer-events: none; z-index: 4;
  animation: ring 0.6s ease-out forwards;
}
@keyframes ring { to { transform: scale(7); opacity: 0; } }

/* --- new discovery card ------------------------------------------------------------------ */
.discovery {
  position: fixed; inset: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center;
  background: rgba(20, 16, 40, 0.32);
  padding: 16px;
  animation: fadein 0.2s;
}
.discovery[hidden] { display: none; }
.discovery-card {
  --c: var(--accent);
  position: relative;
  width: min(380px, 100%);
  background: var(--panel);
  border-radius: 22px;
  padding: 26px 24px 20px;
  text-align: center;
  box-shadow: var(--shadow-lift);
  border-top: 6px solid var(--c);
  animation: cardin 0.5s cubic-bezier(.2,1.5,.4,1);
  overflow: hidden;
}
.discovery-card .kicker { font: 700 13px/1 var(--display); letter-spacing: 2px; text-transform: uppercase; color: var(--c); }
.discovery-card .big-icon {
  font-size: 72px; line-height: 1; margin: 14px auto 8px;
  width: 116px; height: 116px; display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  background: radial-gradient(circle, color-mix(in srgb, var(--c) 28%, transparent) 0%, transparent 70%);
  animation: wobble 1.6s ease-in-out infinite;
}
.discovery-card h2 { margin: 0; font: 700 30px/1.1 var(--display); }
.discovery-card .cat { display: inline-block; margin: 8px 0 10px; font-size: 12px; font-weight: 800; color: #fff; background: var(--c); border-radius: 99px; padding: 2px 10px; }
.discovery-card p { margin: 0 0 8px; color: var(--ink-2); }
.discovery-card .recipe-line { font-weight: 700; color: var(--ink); }
.discovery-card .milestone { color: var(--accent-2); font-weight: 800; }
.discovery-card .btn { margin-top: 8px; }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i { position: absolute; top: -12px; width: 8px; height: 12px; border-radius: 2px; animation: fall var(--t) linear var(--delay) forwards; }
@keyframes fall { to { transform: translateY(520px) rotate(var(--r)); opacity: 0.2; } }
@keyframes cardin { from { transform: scale(0.6) translateY(20px); opacity: 0; } }
@keyframes wobble { 50% { transform: scale(1.06) rotate(-4deg); } }
@keyframes fadein { from { opacity: 0; } }

/* --- hint popover ------------------------------------------------------------------------ */
.hint-pop {
  position: fixed; z-index: 40;
  top: 72px; left: 16px;
  width: min(340px, calc(100vw - 32px));
  background: var(--panel);
  border: 1px solid var(--line);
  border-top: 5px solid #ffd166;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lift);
  padding: 14px 16px 12px;
  animation: cardin 0.25s;
}
.hint-pop h3 { margin: 0 0 6px; font: 600 18px/1.2 var(--display); }
.hint-pop ol { margin: 0 0 10px; padding-left: 20px; }
.hint-pop li { margin-bottom: 4px; }
.hint-pop .meta { font-size: 12px; color: var(--ink-3); margin: 0 0 10px; }
.hint-pop .row { display: flex; gap: 6px; flex-wrap: wrap; }

/* --- modal ------------------------------------------------------------------------------- */
.modal {
  border: 0; padding: 0;
  width: min(940px, calc(100vw - 24px));
  max-height: calc(100vh - 32px);
  border-radius: 20px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow-lift);
}
.modal::backdrop { background: rgba(20, 16, 40, 0.45); }
.modal[open] { animation: cardin 0.25s; display: flex; }
.modal.narrow { width: min(560px, calc(100vw - 24px)); }
.modal-inner { display: flex; flex-direction: column; min-height: 0; width: 100%; max-height: calc(100vh - 32px); }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 20px 12px; border-bottom: 1px solid var(--line); flex: none; }
.modal-head h2 { margin: 0; font: 600 22px/1.2 var(--display); flex: 1; }
.modal-close { border: 0; background: transparent; font-size: 22px; cursor: pointer; color: var(--ink-3); width: 36px; height: 36px; border-radius: 50%; }
.modal-close:hover { background: var(--bg-2); color: var(--ink); }
.modal-body { padding: 16px 20px 20px; overflow-y: auto; min-height: 0; }
.modal-body h3 { font: 600 17px/1.2 var(--display); margin: 18px 0 8px; }
.modal-body h3:first-child { margin-top: 0; }
.muted { color: var(--ink-3); }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
textarea.field { min-height: 90px; font: 12px/1.4 ui-monospace, Menlo, Consolas, monospace; resize: vertical; }
.msg { font-weight: 700; margin: 8px 0 0; min-height: 1.4em; }
.msg.ok { color: var(--good); }
.msg.err { color: var(--bad); }

/* encyclopedia */
.book { display: grid; grid-template-columns: 1fr 300px; gap: 16px; min-height: 0; }
.book-controls { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.book-controls .field { width: auto; flex: 1; min-width: 140px; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.book-section { grid-column: 1 / -1; font: 600 14px/1 var(--display); color: var(--c); margin: 10px 0 0; display: flex; justify-content: space-between; }
.book-section:first-child { margin-top: 0; }
.entry {
  --c: var(--accent);
  border: 1px solid var(--line); border-bottom: 3px solid var(--c);
  background: var(--panel-2); border-radius: 12px;
  padding: 8px 4px 6px; text-align: center; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 12px; font-weight: 700; line-height: 1.15;
}
.entry .icon { font-size: 26px; line-height: 1; }
.entry.unknown { color: var(--ink-3); border-style: dashed; border-bottom-style: solid; }
.entry.unknown .icon { filter: grayscale(1); opacity: 0.45; }
.entry[aria-current='true'] { outline: 3px solid var(--c); outline-offset: 1px; }
.detail {
  --c: var(--accent);
  position: sticky; top: 0; align-self: start;
  background: var(--panel-2); border-radius: 16px; padding: 18px; border-top: 5px solid var(--c);
}
.detail .d-icon { font-size: 54px; line-height: 1; }
.detail h3 { margin: 8px 0 2px !important; font-size: 22px !important; }
.detail .cat { display: inline-block; font-size: 11px; font-weight: 800; color: #fff; background: var(--c); border-radius: 99px; padding: 1px 9px; }
.detail .desc { color: var(--ink-2); margin: 10px 0; }
.detail h4 { margin: 14px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--ink-3); }
.detail ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.detail li { font-weight: 600; font-size: 14px; }
.detail li.unknown { color: var(--ink-3); }
.detail .tag { font-size: 11px; color: var(--ink-3); font-weight: 700; }

/* stats */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--panel-2); border-radius: 14px; padding: 12px; }
.stat b { display: block; font: 700 26px/1.1 var(--display); }
.stat span { font-size: 12px; color: var(--ink-3); font-weight: 700; }
.cat-bars { display: flex; flex-direction: column; gap: 8px; }
.cat-bar { display: grid; grid-template-columns: 110px 1fr 70px; gap: 10px; align-items: center; font-weight: 700; font-size: 14px; }
.cat-bar .track { height: 10px; border-radius: 99px; background: var(--bg-2); overflow: hidden; }
.cat-bar .track i { display: block; height: 100%; background: var(--c); border-radius: inherit; }
.cat-bar .n { text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; }

/* toasts */
.toasts { position: fixed; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 70; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast {
  background: var(--ink); color: var(--panel);
  padding: 8px 16px; border-radius: 99px; font-weight: 700; font-size: 14px;
  box-shadow: var(--shadow-lift);
  animation: toastin 0.25s ease-out;
  max-width: calc(100vw - 32px); text-align: center;
}
.toast.bad { background: var(--bad); color: #fff; }
.toast.good { background: var(--good); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); transition: 0.3s; }
@keyframes toastin { from { opacity: 0; transform: translateY(10px); } }

.noscript { padding: 40px; text-align: center; }

/* --- narrow screens: board on top, inventory below ---------------------------------------- */
@media (max-width: 860px) {
  .actions .lbl { display: none; }
  .btn { padding: 8px 11px; }
  .book { grid-template-columns: 1fr; }
  .detail { position: static; order: -1; }
}
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 16px; }
  .logo { font-size: 20px; }
  .progress { order: 3; flex-basis: 100%; max-width: none; }
  .layout { flex-direction: column; }
  .board { flex: 1 1 50%; }
  .inventory { width: auto; flex: 1 1 50%; border-left: 0; border-top: 1px solid var(--line); }
  .inv-head { padding: 8px 16px 4px; }
  .chips { padding: 2px 16px 4px; flex-wrap: nowrap; overflow-x: auto; }
  .chip { flex: none; }
  .inv-info { padding: 0 16px 4px; }
  .inv-grid { padding: 4px 16px 16px; }
  .cat-bar { grid-template-columns: 86px 1fr 56px; }
  .board-empty .big { font-size: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
