:root {
  --bg: #f4ecdf;
  --bg2: #efe3cf;
  --card: #ffffff;
  --ink: #3d3229;
  --ink-soft: #8a7a68;
  --accent: #fb8c00;
  --accent2: #1e88e5;
  --good: #43a047;
  --bad: #e53935;
  --radius: 18px;
  --shadow: 0 4px 14px rgba(80, 60, 30, 0.15);
  font-size: 16px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  font-family: "Segoe UI", "Nunito", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  touch-action: manipulation;
}

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

button {
  font: inherit; color: inherit; border: none; cursor: pointer;
  background: var(--card); border-radius: var(--radius);
}
button:active { transform: scale(0.97); }

.boot-splash { margin: auto; text-align: center; color: var(--ink-soft); }
.boot-logo {
  width: 90px; height: 90px; margin: 0 auto 12px; border-radius: 50%;
  border: 16px solid var(--accent); animation: spin 1.6s linear infinite;
  border-top-color: var(--accent2);
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- generic screen layout ---------- */
.screen { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.screen-body { flex: 1; overflow-y: auto; padding: 14px; }

.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  background: var(--bg2); box-shadow: 0 2px 6px rgba(80,60,30,.08); z-index: 2;
}
.topbar h1 { font-size: 1.15rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-icon {
  width: 42px; height: 42px; border-radius: 14px; font-size: 1.25rem;
  display: inline-flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); flex: none;
}
.btn-icon:disabled { opacity: .4; cursor: default; }

.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 6px 12px;
  background: var(--card); border-radius: 999px; font-weight: 700;
  box-shadow: var(--shadow); white-space: nowrap;
}

/* ---------- home ---------- */
.home { justify-content: center; align-items: center; gap: 18px; padding: 20px; position: relative; }
.home-title { font-size: clamp(2rem, 7vw, 3.2rem); margin: 0; text-align: center; }
.home-title span { display: inline-block; animation: bob 2.4s ease-in-out infinite; }
.home-title span:nth-child(2n) { animation-delay: .3s; color: var(--accent); }
.home-title span:nth-child(3n) { animation-delay: .6s; color: var(--accent2); }
.home-title span:nth-child(5n) { animation-delay: .9s; color: var(--good); }
@keyframes bob { 50% { transform: translateY(-6px); } }
.home-tagline { color: var(--ink-soft); margin: 0; }
.home-logo { width: 130px; height: 130px; }
.btn-big {
  font-size: 1.3rem; font-weight: 800; padding: 14px 48px;
  background: var(--accent); color: #fff; box-shadow: var(--shadow);
}
.btn-menu { font-size: 1.05rem; font-weight: 700; padding: 11px 30px; min-width: 220px; box-shadow: var(--shadow); }
.home-status { position: absolute; top: 12px; left: 12px; right: 12px; display: flex; justify-content: space-between; }

/* ---------- category / level grids ---------- */
.grid-cats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.cat-card {
  padding: 16px 10px; text-align: center; border-radius: var(--radius);
  background: var(--card); box-shadow: var(--shadow); position: relative;
}
.cat-card .cat-icon { font-size: 2.2rem; }
.cat-card .cat-name { font-weight: 800; margin: 6px 0 2px; }
.cat-card .cat-progress { color: var(--ink-soft); font-size: .85rem; }
.cat-card .cat-trophy { position: absolute; top: 8px; right: 10px; font-size: 1.2rem; }

.grid-levels { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 10px; }
.lvl-btn {
  aspect-ratio: 1; border-radius: 16px; background: var(--card); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-weight: 800; font-size: 1.05rem; position: relative; padding: 4px;
}
.lvl-btn .lvl-stars { font-size: .7rem; letter-spacing: 1px; color: var(--accent); }
.lvl-btn .lvl-count { font-size: .62rem; color: var(--ink-soft); font-weight: 600; }
.lvl-btn.locked { opacity: .45; }
.lvl-btn.locked::after { content: "🔒"; position: absolute; font-size: 1.3rem; }
.lvl-btn.done { outline: 3px solid var(--good); }

/* ---------- game screen ---------- */
.game { background: var(--bg); }
.game-hud { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--bg2); }
.hud-name { flex: 1; font-weight: 800; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.timebar { height: 12px; background: #ddd0bb; border-radius: 8px; overflow: hidden; margin: 0 10px 4px; }
.timebar-fill { height: 100%; background: var(--good); border-radius: 8px; transition: width .25s linear, background .3s; }
.timebar-fill.warn { background: var(--accent); }
.timebar-fill.danger { background: var(--bad); }

.board-wrap { flex: 1; display: flex; min-height: 0; padding: 6px 10px; }
svg.board { width: 100%; height: 100%; background: #fffdf7; border-radius: var(--radius); box-shadow: var(--shadow); touch-action: none; }
.board .ghost { pointer-events: none; }
.board .piece { cursor: grab; }
.board .piece.dragging { cursor: grabbing; }
.board .piece.selected .sel-halo { display: block; }

.game-toolbar { display: flex; justify-content: center; gap: 10px; padding: 6px 10px 2px; }

.palette {
  display: flex; flex-direction: column; gap: 6px; padding: 6px 10px 10px;
  background: var(--bg2); border-radius: 20px 20px 0 0;
}
.shape-row { display: flex; gap: 8px; overflow-x: auto; padding: 4px 2px; scrollbar-width: thin; }
.shape-btn {
  flex: none; width: 68px; height: 68px; border-radius: 14px; background: var(--card);
  box-shadow: var(--shadow); display: flex; align-items: center; justify-content: center;
  position: relative; touch-action: none;
}
.shape-btn svg { width: 62px; height: 62px; pointer-events: none; }
.shape-btn .count {
  position: absolute; top: -4px; right: -4px; background: var(--accent2); color: #fff;
  font-size: .65rem; font-weight: 800; border-radius: 999px; padding: 2px 6px;
}
.shape-btn.exhausted { opacity: .35; }

/* ---------- modals ---------- */
#modal-root:not(:empty) {
  position: fixed; inset: 0; background: rgba(40, 30, 15, 0.5);
  display: flex; align-items: center; justify-content: center; z-index: 50; padding: 16px;
}
.modal {
  background: var(--card); border-radius: 22px; padding: 22px; max-width: 420px; width: 100%;
  box-shadow: 0 10px 40px rgba(0,0,0,.3); text-align: center; max-height: 90vh; overflow-y: auto;
}
.modal h2 { margin: 0 0 10px; }
.modal p { color: var(--ink-soft); }
.modal .modal-buttons { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.modal .modal-buttons button {
  padding: 12px; font-weight: 700; background: var(--bg2); border-radius: 14px;
}
.modal .modal-buttons button.primary { background: var(--accent); color: #fff; }
.modal .modal-buttons button:disabled { opacity: .4; }
.modal .stars-anim { font-size: 3rem; letter-spacing: 6px; }
.modal .stars-anim .star { display: inline-block; opacity: 0; transform: scale(0); animation: pop-star .5s forwards; }
.modal .stars-anim .star:nth-child(2) { animation-delay: .25s; }
.modal .stars-anim .star:nth-child(3) { animation-delay: .5s; }
@keyframes pop-star { 60% { transform: scale(1.4); opacity: 1; } 100% { transform: scale(1); opacity: 1; } }

/* ---------- settings / shop ---------- */
.setting-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: var(--card); border-radius: var(--radius); padding: 14px; margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.setting-row .label { font-weight: 700; }
.setting-row .note { display: block; font-size: .78rem; color: var(--ink-soft); font-weight: 400; margin-top: 2px; }
.toggle { min-width: 64px; padding: 8px 14px; font-weight: 800; border-radius: 999px; background: var(--bg2); }
.toggle.on { background: var(--good); color: #fff; }
select { font: inherit; padding: 8px 12px; border-radius: 12px; border: 2px solid var(--bg2); background: var(--card); }
.btn-danger { background: var(--bad); color: #fff; padding: 12px; font-weight: 700; width: 100%; }

.shop-item {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border-radius: var(--radius); padding: 14px; margin-bottom: 10px; box-shadow: var(--shadow);
}
.shop-item .shop-icon { font-size: 1.8rem; }
.shop-item .shop-info { flex: 1; }
.shop-item .shop-name { font-weight: 800; }
.shop-item .shop-desc { font-size: .8rem; color: var(--ink-soft); }
.shop-item .shop-buy { background: var(--accent); color: #fff; font-weight: 800; padding: 10px 16px; border-radius: 14px; white-space: nowrap; }
.shop-item .shop-buy:disabled { background: var(--bg2); color: var(--ink-soft); }
.shop-note { font-size: .75rem; color: var(--ink-soft); text-align: center; margin-top: 14px; }

/* ---------- stats / achievements ---------- */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; margin-bottom: 6px; }
.stat-tile {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px; text-align: center;
}
.stat-value { font-weight: 800; font-size: 1.15rem; }
.stat-label { font-size: .72rem; color: var(--ink-soft); margin-top: 2px; }
.stats-h2 { font-size: 1rem; margin: 18px 4px 8px; }
.badge-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.badge-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 12px 8px; text-align: center;
}
.badge-card.locked { opacity: .5; filter: grayscale(1); }
.badge-icon { font-size: 1.9rem; }
.badge-name { font-weight: 800; font-size: .82rem; margin-top: 4px; }
.badge-desc { font-size: .68rem; color: var(--ink-soft); margin-top: 2px; }
.cat-progress-row {
  display: flex; align-items: center; gap: 10px; background: var(--card);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 14px; margin-bottom: 8px;
}
.cat-progress-icon { font-size: 1.4rem; }
.cat-progress-info { flex: 1; }
.cat-progress-name { font-weight: 700; font-size: .85rem; margin-bottom: 4px; }
.cat-progress-bar { height: 8px; background: var(--bg2); border-radius: 6px; overflow: hidden; }
.cat-progress-fill { height: 100%; background: var(--good); border-radius: 6px; }
.cat-progress-num { font-weight: 700; font-size: .8rem; color: var(--ink-soft); }

/* ---------- leaderboard ---------- */
.lb-note { text-align: center; color: var(--ink-soft); font-size: .8rem; margin-top: 0; }
.lb-row {
  display: flex; align-items: center; gap: 10px; background: var(--card);
  border-radius: 12px; box-shadow: var(--shadow); padding: 9px 14px; margin-bottom: 7px;
}
.lb-row.you { outline: 3px solid var(--accent2); font-weight: 800; }
.lb-rank { width: 34px; text-align: center; font-weight: 800; flex: none; }
.lb-name { flex: 1; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-score { font-weight: 700; color: var(--ink-soft); white-space: nowrap; }
.lb-list { max-height: 45vh; overflow-y: auto; }
.name-input {
  font: inherit; font-weight: 700; padding: 8px 12px; border-radius: 12px;
  border: 2px solid var(--bg2); background: var(--card); width: 150px; text-align: center;
}

/* ---------- ads ---------- */
.ad-banner {
  height: 52px; background: repeating-linear-gradient(45deg, #e8dcc4, #e8dcc4 12px, #f2e9d7 12px, #f2e9d7 24px);
  display: flex; align-items: center; justify-content: center; font-size: .8rem;
  color: var(--ink-soft); border-top: 1px dashed #c9b997; flex: none;
}
.ad-overlay {
  position: fixed; inset: 0; background: #22303c; color: #fff; z-index: 100;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; text-align: center; padding: 20px;
}
.ad-overlay .ad-box {
  width: min(80vw, 340px); aspect-ratio: 4/3; border-radius: 14px;
  background: repeating-linear-gradient(-45deg, #33475a, #33475a 16px, #3d5368 16px, #3d5368 32px);
  display: flex; align-items: center; justify-content: center; font-size: 2.4rem;
}
.ad-overlay button {
  padding: 12px 30px; border-radius: 999px; font-weight: 800; background: var(--accent); color: #fff;
}
.ad-overlay button:disabled { background: #55606b; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 20px; border-radius: 999px;
  font-weight: 700; z-index: 60; animation: toast-in-out 2.6s forwards; pointer-events: none;
  max-width: 90vw; text-align: center;
}
@keyframes toast-in-out {
  0% { opacity: 0; transform: translate(-50%, 16px); }
  12%, 85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; }
}

@media (min-width: 800px) {
  .game-hud { padding: 10px 16px; }
  .shape-btn { width: 78px; height: 78px; }
  .shape-btn svg { width: 72px; height: 72px; }
}
