@font-face {
  font-family: "Fredoka";
  src: url("fonts/fredoka-sub.woff") format("woff");
  font-weight: 300 700;
  font-display: swap;
}

/* Theme tokens. "coven" is the default; the others are at the end. theme.js applies the saved one first. */
:root, [data-swatch="coven"] {
  --bar: #1c1430; --panel: #261b44; --raised: #34265c; --line: #4a3a7e; --ink: #fff8ec; --ink-2: #d9cff5; --dim: #a99cd0;
  --accent: #ffd166; --on-accent: #2c2050; --accent-2: #b7f07a; --bad: #ff7a90;
  --world-top: #1d1638; --world-bot: #0c0918; --world-edge: #08060f;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--world-edge); }
body {
  color: var(--ink);
  font-family: "Fredoka", "Trebuchet MS", system-ui, sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none; user-select: none;
  overflow: hidden; overscroll-behavior: none;
}
button { font: inherit; color: inherit; touch-action: manipulation; cursor: pointer; }
input, textarea { font: inherit; -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }
kbd { font: inherit; font-size: .85em; padding: 0 .35em; border-radius: 5px; background: var(--raised); border: 1px solid var(--line); }

.app { display: flex; flex-direction: column; height: 100dvh; }

/* ---------- top bar ---------- */
.top {
  display: flex; align-items: center; gap: 6px; background: var(--bar);
  padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  border-bottom: 1px solid rgba(255, 255, 255, .06);
}
.brand { display: flex; align-items: center; gap: 6px; font-weight: 600; font-size: 19px; letter-spacing: .02em; min-width: 0; }
.brand .logo { width: 20px; height: 20px; fill: var(--accent); }
.fps { font-size: 12px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.acts { margin-left: auto; display: flex; gap: 4px; }
.icon-btn {
  width: 42px; height: 42px; display: grid; place-items: center; border-radius: 12px; border: 0;
  background: var(--panel); font-size: 24px; line-height: 1; flex: none;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.icon-btn:disabled { opacity: .4; cursor: default; }
#grav-btn svg { transition: transform .25s; }
#grav-btn[data-g="-1"] svg { transform: rotate(180deg); }
#grav-btn[data-g="0"] svg { transform: rotate(90deg); opacity: .55; }

/* ---------- the world ---------- */
.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: var(--world-edge); }
#world { position: absolute; inset: 0; display: block; touch-action: none; outline: none; cursor: crosshair; }
.bug-corner {
  position: absolute; right: 8px; bottom: 8px; width: 42px; height: 42px; border-radius: 50%; border: 0; padding: 0;
  display: grid; place-items: center; background: rgba(20, 14, 36, .55); color: var(--dim); opacity: .75;
}
.bug-corner svg { width: 20px; height: 20px; }
.bug-corner:hover { opacity: 1; color: var(--ink); }
/* the shared Report a bug sheet takes this game's colours */
:root { --cbug-panel: var(--panel); --cbug-line: var(--line); --cbug-field: var(--bar); --cbug-shade: var(--bar); --cbug-accent-shadow: transparent; }
.toast {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); max-width: calc(100% - 32px);
  background: rgba(28, 20, 48, .92); border: 1px solid var(--line); color: var(--ink); padding: 8px 14px; border-radius: 999px;
  font-size: 14px; pointer-events: none; opacity: 0; transition: opacity .3s; text-align: center;
}
.toast.show { opacity: 1; }

/* ---------- dock ---------- */
.dock {
  background: var(--bar); border-top: 1px solid rgba(255, 255, 255, .06); display: grid; gap: 6px;
  padding: 8px max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}
.row { display: flex; gap: 6px; align-items: center; min-width: 0; }
.sw { display: inline-block; width: 30px; height: 30px; border-radius: 9px; flex: none; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .18), 0 2px 6px rgba(0, 0, 0, .35); }
.el-btn {
  display: flex; align-items: center; gap: 10px; padding: 6px 14px 6px 8px; min-height: 52px; border-radius: var(--radius); flex: none;
  border: 2px solid var(--accent); background: var(--raised); text-align: left;
}
.el-btn .sw { width: 36px; height: 36px; }
.el-txt { display: grid; line-height: 1.1; }
.el-txt b { font-size: 17px; font-weight: 600; }
.el-txt small { font-size: 12px; color: var(--dim); }
/* the "All elements" button: the way into the drawer, so it looks like one */
.dock { position: relative; }
.all-btn {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 10px; min-height: 52px; border-radius: var(--radius); flex: none;
  border: 2px solid var(--accent); background: var(--accent); color: var(--on-accent); text-align: left;
}
.all-btn svg { width: 24px; height: 24px; flex: none; }
.all-txt { display: grid; line-height: 1.1; }
.all-txt b { font-size: 15px; font-weight: 700; white-space: nowrap; }
.all-txt small { display: flex; gap: 4px; align-items: center; }
.badge { font-size: 12px; font-weight: 700; padding: 0 6px; border-radius: 999px; background: var(--on-accent); color: var(--accent); }
.kbd-hint { display: none; font-size: 11px; line-height: 1.2; background: transparent; border-color: currentColor; color: inherit; opacity: .75; }
@media (hover: hover) and (pointer: fine) { .kbd-hint { display: inline; } }
.all-btn.fresh { animation: beckon 2.6s ease-in-out infinite; }
@keyframes beckon { 0%, 100% { box-shadow: 0 0 0 0 rgba(255, 209, 102, 0); } 50% { box-shadow: 0 0 14px 3px rgba(255, 209, 102, .45); } }
@media (prefers-reduced-motion: reduce) { .all-btn.fresh { animation: none; box-shadow: 0 0 12px 2px rgba(255, 209, 102, .4); } }
.all-tip {
  position: absolute; left: max(8px, env(safe-area-inset-left)); bottom: calc(100% + 8px); z-index: 2; pointer-events: none;
  background: var(--panel); color: var(--ink); border: 1px solid var(--accent); padding: 6px 12px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .4);
}
.all-tip::after { content: ""; position: absolute; left: 26px; top: 100%; border: 7px solid transparent; border-top-color: var(--accent); }
.el-btn { border-color: var(--line); }
.chip.more { padding: 4px 12px; color: var(--ink-2); border-style: dashed; }
.chip.more .ico { width: 18px; height: 18px; }
.recents { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; min-width: 0; padding: 2px 0; }
.recents::-webkit-scrollbar { display: none; }
.chip {
  display: flex; align-items: center; gap: 6px; flex: none; padding: 4px 10px 4px 4px; min-height: 44px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--panel); font-size: 14px;
}
.chip .sw { width: 26px; height: 26px; border-radius: 8px; }
.tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; width: 100%; }
.tools button {
  display: grid; justify-items: center; gap: 1px; padding: 6px 2px; min-height: 48px; border-radius: 12px; border: 1px solid transparent;
  background: var(--panel); font-size: 12.5px;
}
.tools svg { width: 20px; height: 20px; }
.tools button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.sizes { gap: 10px; }
.shape-btn {
  display: flex; align-items: center; gap: 8px; min-height: 40px; padding: 4px 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); font-size: 14px; flex: none;
}
.shape-btn i { width: 16px; height: 16px; border: 2px solid currentColor; border-radius: 50%; }
.shape-btn[data-shape="1"] i { border-radius: 3px; }
.shape-btn[data-shape="2"] i { border-style: dotted; }
.size { display: flex; align-items: center; gap: 8px; flex: 1; font-size: 14px; color: var(--ink-2); }
.size input { flex: 1; accent-color: var(--accent); min-width: 0; height: 28px; }
.size output { width: 2.2em; text-align: right; color: var(--ink); }

/* ---------- sheets ---------- */
.scrim { position: fixed; inset: 0; background: rgba(4, 2, 10, .45); z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 86dvh; overflow-y: auto;
  background: var(--panel); border-radius: 20px 20px 0 0; border-top: 1px solid var(--line);
  padding: 12px max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  box-shadow: 0 -10px 30px rgba(0, 0, 0, .45); -webkit-user-select: none; user-select: none;
  animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head { display: flex; align-items: center; gap: 10px; position: sticky; top: -12px; background: var(--panel); padding: 4px 0 8px; z-index: 1; }
.sheet-head h2 { margin: 0; font-size: 21px; font-weight: 600; flex: 1; }
.sheet .dim, .dim { color: var(--dim); }
.tiny { font-size: 13px; }
.sheet.small { max-width: 420px; margin: 0 auto; }
.sheet.small p { margin: 4px 0 14px; font-size: 17px; }

.drawer { max-height: 78dvh; }
.drawer-body h3 { margin: 12px 0 6px; font-size: 14px; font-weight: 500; color: var(--dim); text-transform: uppercase; letter-spacing: .08em; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.tile {
  display: grid; grid-template-columns: 40px 1fr; grid-template-rows: auto auto; column-gap: 10px; align-items: center; text-align: left;
  min-height: 64px; padding: 8px 10px; border-radius: var(--radius); border: 2px solid transparent; background: var(--raised);
}
.tile .sw { grid-row: span 2; width: 40px; height: 40px; border-radius: 11px; }
.tile b { font-weight: 600; font-size: 16px; }
.tile small { font-size: 12px; color: var(--dim); line-height: 1.2; }
.tile[aria-pressed="true"] { border-color: var(--accent); }
.drawer .sheet-head { flex-wrap: wrap; }
.drawer-tabs { flex: 1 0 100%; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 2px 0; }
.drawer-tabs::-webkit-scrollbar { display: none; }
.drawer-tabs button {
  flex: none; min-height: 36px; padding: 4px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--raised); font-size: 14px;
}
.drawer-tabs button.premium { border-color: #b48cff; color: #e2d2ff; box-shadow: 0 0 10px rgba(170, 120, 255, .35); }
.drawer-body h3 { scroll-margin-top: 110px; }
.drawer-body h3 small { text-transform: none; letter-spacing: 0; margin-left: 6px; font-size: 13px; }
.premium-h { color: #d8c4ff !important; }
.premium-note { margin: 0 0 8px; font-size: 13.5px; }
.linkish { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; font-size: inherit; }
.tile { position: relative; }
.tile.premium { background: linear-gradient(135deg, var(--raised), rgba(120, 80, 200, .28)); box-shadow: 0 0 12px rgba(170, 120, 255, .28); border-color: rgba(180, 140, 255, .35); }
.tile.premium[aria-pressed="true"] { border-color: var(--accent); }
.tile .lock { position: absolute; top: 6px; right: 6px; width: 15px; height: 15px; color: #d8c4ff; opacity: .85; }
.drawer.member .tile .lock { display: none; }
.member-el { display: flex; align-items: center; gap: 10px; }
.member-el:empty { display: none; }
a.go { display: inline-grid; place-items: center; text-decoration: none; text-align: center; }
.toggle { display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--ink-2); margin: 8px 0; cursor: pointer; }
.toggle input { width: 22px; height: 22px; accent-color: var(--accent); }
.sheet-head .toggle { margin: 0; font-size: 14px; }

.menu { display: grid; gap: 8px; }
.big {
  display: grid; text-align: left; gap: 2px; padding: 12px 16px; min-height: 60px; border-radius: var(--radius);
  border: 1px solid var(--line); background: var(--raised);
}
.big b { font-size: 18px; font-weight: 600; }
.big small { color: var(--dim); font-size: 14px; }
.menu-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 4px; }
.menu-row button { flex: 1; }
.ico { width: 18px; height: 18px; vertical-align: -3px; }
.go, .ghost {
  min-height: 46px; padding: 8px 18px; border-radius: 12px; font-weight: 600; border: 2px solid var(--accent);
  background: var(--accent); color: var(--on-accent);
}
.ghost { background: transparent; color: var(--ink); border-color: var(--line); font-weight: 500; }
.small { min-height: 38px; padding: 4px 12px; }
.btns { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.btns button { flex: 1; }
.field { display: grid; gap: 4px; margin: 10px 0; font-size: 15px; color: var(--ink-2); }
.field input, .field textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bar); color: var(--ink); font-size: 16px;
}
.field textarea { resize: vertical; }
#code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; word-break: break-all; }
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; min-width: 0; }
.bug-note { min-height: 1.3em; margin: 6px 0; color: var(--accent); }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.card {
  display: grid; gap: 4px; text-align: left; padding: 8px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--raised);
}
.card canvas { width: 100%; aspect-ratio: 3 / 4; object-fit: cover; image-rendering: pixelated; border-radius: 10px; background: #160f28; }
.card b { font-weight: 600; }
.card small { color: var(--dim); font-size: 12.5px; line-height: 1.25; }
.book { display: grid; gap: 8px; }
.page { display: grid; grid-template-columns: 56px 1fr auto auto; gap: 10px; align-items: center; padding: 6px; border-radius: 12px; background: var(--raised); }
.page canvas { width: 56px; height: 56px; object-fit: cover; image-rendering: pixelated; border-radius: 8px; }
.page div { display: grid; min-width: 0; }
.page b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page small { color: var(--dim); }
.set { display: grid; gap: 6px; margin: 10px 0; font-size: 15px; color: var(--ink-2); }
.seg { display: flex; gap: 4px; flex-wrap: wrap; }
.seg button { flex: 1; min-height: 42px; padding: 4px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--raised); font-size: 14px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.howto p { line-height: 1.45; color: var(--ink-2); margin: 8px 0 12px; }
.howto b { color: var(--ink); }

/* ---------- bigger screens: sheets become centred panels, the dock sits in one line ---------- */
@media (min-width: 760px) {
  .sheet { left: 50%; right: auto; bottom: auto; top: 50%; transform: translate(-50%, -50%); width: min(560px, 92vw); max-height: 86vh;
    border-radius: 20px; border: 1px solid var(--line); animation: none; }
  .sheet.wide, .drawer { width: min(860px, 94vw); }
  .dock { grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; column-gap: 12px; }
  .tools { width: auto; grid-template-columns: repeat(5, 70px); }
  .sizes .size { width: 220px; flex: none; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .dock { grid-template-columns: minmax(0, 1fr) auto auto; padding-top: 4px; column-gap: 8px; }
  .tools { grid-template-columns: repeat(5, 56px); }
  .tools span { display: none; }
  .tools button { min-height: 40px; }
  .el-btn, .all-btn { min-height: 44px; }
  .top { padding-top: 4px; padding-bottom: 4px; }
  .icon-btn { width: 38px; height: 38px; }
}

/* ---------- themes ---------- */
:root[data-theme="midnight"], [data-swatch="midnight"] {
  --bar: #0e1016; --panel: #171a23; --raised: #222737; --line: #343b50; --ink: #eef0f5; --ink-2: #c9ccd6; --dim: #9196a6;
  --accent: #8fb8ff; --on-accent: #0e1626;
  --world-top: #121624; --world-bot: #06070b; --world-edge: #040507;
}
:root[data-theme="moss"], [data-swatch="moss"] {
  --bar: #121a14; --panel: #1a261d; --raised: #243528; --line: #36503b; --ink: #f1f6ea; --ink-2: #cfdcc6; --dim: #98ad92;
  --accent: #c6e67a; --on-accent: #1a2610;
  --world-top: #152018; --world-bot: #070c09; --world-edge: #050806;
}
:root[data-theme="bloodmoon"], [data-swatch="bloodmoon"] {
  --bar: #1c0f14; --panel: #28141b; --raised: #3a1c26; --line: #5a2c3a; --ink: #fff1ee; --ink-2: #f0cfd0; --dim: #c09aa0;
  --accent: #ff8a6a; --on-accent: #2a0e0c;
  --world-top: #221018; --world-bot: #0c0508; --world-edge: #080305;
}
