:root {
  --cave: #150b2a;
  --cave-2: #241448;
  --amethyst: #a855f7;
  --cyan: #4bc8ff;
  --grad-accent: linear-gradient(135deg, #7b5cff 0%, #c13cff 50%, #4bc8ff 100%);
  --grad-accent-soft: linear-gradient(135deg, #9a7bff, #6bd4ff);
  --ink: #efeaff;
  --muted: #a89ec7;
  --glass: rgba(46, 30, 84, 0.55);
  --glass-strong: rgba(56, 38, 100, 0.85);
  --glass-border: rgba(168, 130, 255, 0.28);
  --green: #3fd17b;
  --red: #ff5c5c;
  --gold: #ffd23b;
  --shadow-soft: 0 18px 40px -20px rgba(0, 0, 0, 0.7);
}

* { box-sizing: border-box; }

/* Tausta myös html:lle: muuten bodyn alle jäävä kangas on valkoinen
   (valkoinen alapalkki iOS:n kotivalikkosovelluksessa).
   Väri = bodyn gradientin viimeinen pysäkki. */
html {
  background: #0d0817;
  height: 100%;
  overscroll-behavior-y: none;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  position: relative;
  background:
    radial-gradient(1200px 600px at 15% -5%, rgba(123, 92, 255, 0.22) 0%, rgba(123, 92, 255, 0) 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(75, 200, 255, 0.14) 0%, rgba(75, 200, 255, 0) 55%),
    linear-gradient(180deg, #1c1233 0%, #140c28 45%, #0d0817 100%);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Hohtavat kideläikät taustalla */
.bg-blobs { position: fixed; inset: 0; z-index: -1; overflow: hidden; pointer-events: none; }
.bg-blobs::before, .bg-blobs::after {
  content: ""; position: absolute; border-radius: 50%; filter: blur(60px); opacity: 0.3;
}
.bg-blobs::before {
  width: 46vmax; height: 46vmax; left: -12vmax; top: 30vh;
  background: radial-gradient(circle, #7b5cff, transparent 65%);
  animation: float1 18s ease-in-out infinite;
}
.bg-blobs::after {
  width: 40vmax; height: 40vmax; right: -14vmax; top: 8vh;
  background: radial-gradient(circle, #2b9dd4, transparent 65%);
  animation: float2 22s ease-in-out infinite;
}
@keyframes float1 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(4vmax,-3vmax) scale(1.08); } }
@keyframes float2 { 0%,100% { transform: translate(0,0) scale(1); } 50% { transform: translate(-4vmax,3vmax) scale(1.1); } }

.hidden-control { display: none !important; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  color: white;
  flex-wrap: wrap;
  position: relative;
  overflow: hidden;
  background: linear-gradient(120deg, #1a0f38 0%, var(--cave-2) 60%, #3a1a6e 100%);
  margin: calc(env(safe-area-inset-top) * -1) calc(env(safe-area-inset-left) * -1) 0 calc(env(safe-area-inset-right) * -1);
  padding-top: calc(14px + env(safe-area-inset-top));
  padding-left: calc(20px + env(safe-area-inset-left));
  padding-right: calc(20px + env(safe-area-inset-right));
  box-shadow: 0 8px 30px -12px rgba(0, 0, 0, 0.9);
}
.topbar::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 40%, rgba(255,255,255,0.08) 50%, transparent 60%);
}
.brand { display: flex; align-items: center; gap: 12px; z-index: 1; }
.logo {
  width: 54px; height: 54px; border-radius: 18px; display: flex; align-items: center; justify-content: center;
  font-size: 1.7rem; background: rgba(168, 130, 255, 0.16);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.25), 0 0 0 2px rgba(168,130,255,0.2), 0 6px 16px -6px rgba(0,0,0,0.6);
  backdrop-filter: blur(6px);
}
.brand-text h1 { margin: 0; font-size: 1.35rem; letter-spacing: 0.3px; font-family: Georgia, "Iowan Old Style", serif; }
.tagline { font-size: 0.72rem; color: #b9a6ff; text-transform: uppercase; letter-spacing: 1px; }

.stats-strip { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; z-index: 1; }
.stat { display: flex; flex-direction: column; min-width: 78px; }
.stat-label { font-size: 0.68rem; text-transform: uppercase; color: #c3b4e8; opacity: 0.85; letter-spacing: 0.05em; }
.stat-value { font-size: 1.2rem; font-weight: 800; color: #fff; font-variant-numeric: tabular-nums; text-shadow: 0 1px 8px rgba(123, 92, 255, 0.6); }

.sound-toggle {
  background: rgba(168,130,255,0.14); border: 1px solid rgba(168,130,255,0.3); color: white;
  padding: 9px 12px; border-radius: 12px; cursor: pointer; font-size: 0.95rem; backdrop-filter: blur(6px);
  transition: transform 0.15s, background 0.15s;
}
.sound-toggle:hover { background: rgba(168,130,255,0.26); }
.sound-toggle:active { transform: scale(0.92); }
.sound-toggle.muted { opacity: 0.6; }

.tabs { display: flex; gap: 8px; padding: 16px 16px 0; flex-wrap: wrap; max-width: 900px; margin: 0 auto; }
.tab-btn {
  border: 1px solid transparent; background: rgba(56, 38, 100, 0.45); color: var(--ink); padding: 10px 18px;
  border-radius: 14px; font-size: 0.9rem; cursor: pointer; font-weight: 700; opacity: 0.8;
  backdrop-filter: blur(6px); transition: transform 0.15s, opacity 0.15s, box-shadow 0.2s;
}
.tab-btn.active {
  background: var(--glass-strong); opacity: 1; border-color: var(--glass-border);
  box-shadow: 0 8px 20px -10px rgba(123, 92, 255, 0.6); color: #d4c2ff;
}
.tab-btn:hover { opacity: 1; }
.tab-btn:active { transform: scale(0.96); }

main { padding: 18px; max-width: 900px; margin: 0 auto; }
.tab-panel {
  display: none; background: var(--glass); border: 1px solid var(--glass-border); border-radius: 22px;
  padding: 20px; box-shadow: var(--shadow-soft); backdrop-filter: blur(14px);
}
.tab-panel.active { display: block; animation: panel-in 0.35s ease; }
@keyframes panel-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

h2 { margin-top: 0; font-family: Georgia, "Iowan Old Style", serif; color: #d4c2ff; }
.intro { color: var(--muted); font-size: 0.9rem; margin-top: -6px; }

/* ---------- Pelialue ---------- */
.play-layout { display: flex; flex-direction: column; align-items: center; gap: min(16px, 1.8vh); }

.play-hud { display: flex; gap: 10px; width: min(100%, 380px); }
.hud-box {
  flex: 1; background: rgba(30, 18, 60, 0.55); border: 1px solid var(--glass-border); border-radius: 16px;
  padding: 8px 10px; text-align: center; box-shadow: inset 0 1px 0 rgba(255,255,255,0.08);
  min-height: 0;
}
.hud-box.score-box { flex: 1.3; }
.hud-box .label { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); font-weight: 700; }
.hud-box .value {
  font-family: ui-monospace, "SF Mono", Consolas, monospace; font-variant-numeric: tabular-nums;
  font-size: 1.3rem; font-weight: 800; color: #fff; display: flex; align-items: center;
  justify-content: center; gap: 4px; min-height: 30px;
}
.mini-fruit { width: 34px; height: 34px; }
.mini-fruit.peek { width: 26px; height: 26px; opacity: 0.75; }

/* Hehku-mittari */
.hehku-wrap { display: flex; align-items: center; gap: 8px; width: min(100%, 380px); }
.hehku-label {
  font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.1em;
  color: var(--gold); font-weight: 800; flex: 0 0 auto;
}
.hehku-bar {
  flex: 1; height: min(14px, 2vh); border-radius: 8px; overflow: hidden;
  background: rgba(255, 255, 255, 0.07); border: 1px solid var(--glass-border);
}
.hehku-fill {
  height: 100%; width: 0%; border-radius: 8px;
  background: linear-gradient(90deg, #b88a1e, #ffd23b);
  transition: width 0.15s linear;
}
.hehku-bar.active { border-color: var(--gold); box-shadow: 0 0 10px rgba(255, 210, 59, 0.5); }
.hehku-bar.active .hehku-fill {
  background: linear-gradient(90deg, #fff3c9, #ffd23b, #ff9d2e);
  animation: hehku-pulse 0.5s ease-in-out infinite alternate;
}
@keyframes hehku-pulse { from { filter: brightness(1); } to { filter: brightness(1.35); } }

.board-wrap {
  position: relative;
  width: min(100%, 380px);
  border-radius: 28px;
  padding: 14px 14px 16px;
  background: linear-gradient(160deg, rgba(90, 60, 150, 0.35), rgba(40, 24, 80, 0.25));
  border: 1.5px solid rgba(168, 130, 255, 0.35);
  box-shadow:
    0 30px 60px -25px rgba(0, 0, 0, 0.85),
    inset 0 2px 3px rgba(255,255,255,0.12),
    inset 0 -12px 24px -12px rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}
.jar-lid {
  position: absolute; top: 6px; left: 50%; transform: translateX(-50%);
  width: 62%; height: 12px; border-radius: 0 0 16px 16px;
  background: linear-gradient(180deg, rgba(168,130,255,0.5), rgba(168,130,255,0.1));
  box-shadow: 0 2px 6px -2px rgba(0,0,0,0.5);
}
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 300 / 460;
  border-radius: 18px;
  overflow: hidden;
  touch-action: none;
  display: block;
  box-shadow: inset 0 2px 10px rgba(0,0,0,0.5), inset 0 0 0 1px rgba(168,130,255,0.25);
}
.board canvas { width: 100%; height: 100%; display: block; }

.pause-btn {
  position: absolute; top: 20px; right: 20px; z-index: 5;
  width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--glass-border);
  background: rgba(30, 18, 60, 0.6); color: var(--ink); font-size: 0.8rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center; backdrop-filter: blur(4px);
}
.pause-btn:active { transform: scale(0.92); }
.pause-ov {
  position: absolute; inset: 14px 14px 16px; border-radius: 18px; z-index: 4;
  display: none; align-items: center; justify-content: center; flex-direction: column; gap: 6px;
  background: rgba(13, 8, 23, 0.72); backdrop-filter: blur(3px); cursor: pointer;
  color: var(--ink); font-weight: 800; font-size: 1.1rem; text-align: center;
}
.pause-ov.show { display: flex; }
.pause-ov small { color: var(--muted); font-weight: 600; }

.popups { position: absolute; inset: 0; pointer-events: none; }
.score-pop {
  position: absolute; font-family: ui-monospace, "SF Mono", monospace; font-weight: 800; font-size: 1rem;
  color: #fff; text-shadow: 0 2px 6px rgba(0,0,0,0.8), 0 0 2px rgba(0,0,0,0.5);
  transform: translate(-50%, -50%); animation: float-up 750ms ease-out forwards;
}
.score-pop.combo {
  color: #fff; font-size: 0.95rem; letter-spacing: 0.06em;
  background: linear-gradient(135deg, #ffd23b, #ff9d2e, #c13cff); -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent; text-shadow: none; filter: drop-shadow(0 2px 6px rgba(255,180,60,0.6));
  animation: combo-pop 800ms cubic-bezier(0.2,1.4,0.4,1) forwards;
}
@keyframes float-up {
  0% { opacity: 0; transform: translate(-50%, -40%) scale(0.9); }
  20% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -140%) scale(1.05); }
}
@keyframes combo-pop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.4); }
  25% { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  70% { opacity: 1; transform: translate(-50%, -80%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -120%) scale(1); }
}

/* ---------- Kidekaari ---------- */
.chain-wrap { width: min(100%, 380px); }
.chain-title {
  font-size: 0.62rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted);
  font-weight: 800; text-align: center; margin-bottom: 6px;
}
.chain {
  display: flex; align-items: center; justify-content: center; flex-wrap: nowrap; gap: 0;
  background: rgba(30, 18, 60, 0.45); border: 1px solid var(--glass-border); border-radius: 16px;
  padding: 8px 6px; box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
  overflow-x: auto; scrollbar-width: none;
}
.chain::-webkit-scrollbar { display: none; }
.chain-cell {
  width: 24px; height: 24px; display: flex; align-items: center; justify-content: center;
  border-radius: 50%; flex-shrink: 0;
}
.chain-cell.goal { box-shadow: 0 0 0 2px var(--gold), 0 0 8px rgba(255,191,71,0.6); }
.chain-cell.secret.locked { box-shadow: 0 0 0 1px rgba(255,191,71,0.4); }
.chain-cell.locked {
  background: rgba(120, 90, 180, 0.15); color: var(--muted); font-weight: 800; font-size: 0.72rem;
}
.chain-canvas { display: block; }
.chain-arrow { color: var(--muted); font-size: 0.7rem; opacity: 0.45; flex-shrink: 0; width: 8px; text-align: center; }

.controls { display: flex; gap: 8px; width: min(100%, 380px); flex-wrap: wrap; }
.controls button {
  font-family: inherit; font-size: 0.82rem; font-weight: 700; padding: min(12px, 1.5vh) 12px; border-radius: 14px;
  border: 1px solid var(--glass-border); background: rgba(56, 38, 100, 0.55); color: var(--ink); cursor: pointer;
  display: flex; align-items: center; gap: 6px; justify-content: center; flex: 1;
  transition: transform 0.12s, box-shadow 0.2s, background 0.2s;
}
.controls button:hover { background: rgba(76, 54, 130, 0.7); }
.controls button:active { transform: scale(0.95); }
.controls button:disabled { opacity: 0.4; cursor: not-allowed; }
.controls button.primary {
  background: var(--grad-accent); color: white; border-color: transparent;
  box-shadow: 0 10px 22px -10px rgba(123, 92, 255, 0.8);
}
.controls button.daily {
  background: linear-gradient(135deg, #8a5a10, #b8861e); color: #fff3c9; border-color: rgba(255,210,59,0.4);
}
.controls button.armed { background: var(--red); color: white; border-color: var(--red); }
.charge-count { font-size: 0.7rem; background: rgba(0,0,0,0.3); border-radius: 6px; padding: 1px 6px; }

.mode-line {
  margin: 0; text-align: center; font-size: 0.75rem; color: var(--gold); font-weight: 700;
  width: min(100%, 380px);
}
.hint { margin: 0; text-align: center; font-size: 0.75rem; color: var(--muted); width: min(100%, 380px); line-height: 1.5; }

/* ---------- Toastit (saavutukset ym.) ---------- */
.toasts {
  position: fixed; top: calc(72px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
  z-index: 60; display: flex; flex-direction: column; gap: 8px; pointer-events: none; width: max-content; max-width: 92vw;
}
.toast {
  background: var(--glass-strong); border: 1px solid var(--gold); color: var(--ink);
  padding: 10px 16px; border-radius: 14px; font-weight: 800; font-size: 0.85rem; text-align: center;
  box-shadow: 0 12px 30px -10px rgba(0,0,0,0.8); backdrop-filter: blur(8px);
  animation: toast-in 3.4s ease both;
}
@keyframes toast-in {
  0% { opacity: 0; transform: translateY(-12px) scale(0.9); }
  8% { opacity: 1; transform: translateY(0) scale(1); }
  88% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-8px); }
}

/* ---------- Kauppa / Teemat / Tilastot ---------- */
.shop-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; }
.shop-card {
  border: 1px solid var(--glass-border); border-radius: 18px; padding: 16px;
  background: rgba(30, 18, 60, 0.55); box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
  transition: transform 0.15s, box-shadow 0.2s;
}
.shop-card:hover { transform: translateY(-2px); box-shadow: 0 14px 28px -18px rgba(123, 92, 255, 0.6); }
.shop-card.affordable { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold), inset 0 1px 0 rgba(255,255,255,0.06); }
.shop-card h3 { margin: 0 0 6px; font-size: 1.05rem; display: flex; align-items: center; gap: 6px; color: #d4c2ff; }
.shop-card .tier-current { font-size: 0.8rem; color: var(--muted); margin-bottom: 10px; }
.shop-card button {
  width: 100%; padding: 11px; border: none; border-radius: 12px; background: var(--grad-accent); color: white;
  font-weight: 800; cursor: pointer; font-size: 0.85rem; transition: transform 0.12s, filter 0.15s;
}
.shop-card button:hover { filter: brightness(1.05); }
.shop-card button:active { transform: scale(0.96); }
.shop-card button:disabled { background: #3a2f52; color: #7a6f96; cursor: not-allowed; filter: none; }
.shop-card .maxed { color: var(--green); font-weight: 800; font-size: 0.85rem; }
.shop-card.equipped { border-color: var(--green); box-shadow: 0 0 0 2px rgba(63,209,123,0.3); }
.shop-card button.equip-btn { background: linear-gradient(135deg, #35c26f, #22a35c); }
.shop-card .equipped-tag { color: var(--green); font-weight: 800; font-size: 0.85rem; }
.shop-card .locked-tag { color: var(--muted); font-weight: 700; font-size: 0.85rem; }

.cosmetic-preview {
  height: 56px; border-radius: 12px; margin-bottom: 10px; display: flex; overflow: hidden;
  border: 1px solid var(--glass-border); box-shadow: inset 0 2px 6px rgba(0,0,0,0.4);
}
.cosmetic-preview span { flex: 1; }

.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stats-grid .stat-card {
  border: 1px solid var(--glass-border); border-radius: 16px; padding: 16px; text-align: center;
  background: rgba(30, 18, 60, 0.55); box-shadow: inset 0 1px 0 rgba(255,255,255,0.06);
}
.stats-grid .stat-card .num { font-size: 1.4rem; font-weight: 900; display: block; font-variant-numeric: tabular-nums; color: #fff; }
.stats-grid .stat-card .label { font-size: 0.74rem; color: var(--muted); }

h3.section-title { color: #d4c2ff; margin: 20px 0 10px; font-family: Georgia, serif; }

.history-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.history-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 14px; border-radius: 12px;
  background: rgba(30, 18, 60, 0.55); border: 1px solid var(--glass-border); font-size: 0.85rem;
}
.history-row .pos { font-weight: 900; color: var(--gold); width: 18px; }
.history-row .pts { font-weight: 800; font-variant-numeric: tabular-nums; min-width: 64px; }
.history-row .gem { flex: 1; color: var(--muted); }
.history-row .date { color: var(--muted); font-size: 0.75rem; }

.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; margin-bottom: 18px; }
.ach-card {
  display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-radius: 14px;
  background: rgba(30, 18, 60, 0.55); border: 1px solid var(--glass-border); font-size: 0.82rem;
}
.ach-card small { color: var(--muted); }
.ach-card .ach-icon { font-size: 1.3rem; }
.ach-card .ach-reward { margin-left: auto; font-weight: 800; color: var(--gold); white-space: nowrap; }
.ach-card.locked { opacity: 0.5; filter: grayscale(0.8); }

.danger-btn {
  background: linear-gradient(135deg, #ef5a4f, #cf3a30); border: none; color: white; padding: 11px 20px;
  border-radius: 12px; font-weight: 800; cursor: pointer; box-shadow: 0 10px 20px -12px rgba(207,58,48,0.7);
}
.danger-btn:active { transform: scale(0.97); }

/* ---------- Ylivuoto (voitto/häviö) ---------- */
.result-overlay {
  position: fixed; inset: 0; background: rgba(10, 5, 20, 0.6); backdrop-filter: blur(4px);
  display: none; align-items: center; justify-content: center; padding: 20px; z-index: 20;
}
.result-overlay.show { display: flex; animation: fade-in 0.3s ease; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.result-card {
  background: var(--glass-strong); border: 1px solid var(--glass-border); border-radius: 24px;
  padding: 30px 26px; max-width: 340px; width: 100%; text-align: center;
  box-shadow: 0 30px 60px -25px rgba(0,0,0,0.9); backdrop-filter: blur(18px);
  animation: card-pop 0.4s cubic-bezier(0.2,1.3,0.4,1);
}
@keyframes card-pop { from { opacity: 0; transform: scale(0.85) translateY(10px); } to { opacity: 1; transform: scale(1) translateY(0); } }
.result-card h2 {
  margin: 0 0 8px; font-size: 1.5rem;
  background: linear-gradient(135deg, #ffd23b, #c13cff, #4bc8ff); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.result-card p { margin: 0 0 8px; color: var(--muted); font-size: 0.92rem; line-height: 1.5; }
.result-card .earned { color: var(--gold); font-weight: 900; font-size: 1.15rem; margin-bottom: 20px; }
.result-card .row { display: flex; gap: 10px; }
.result-card .row button {
  flex: 1; padding: 12px; border-radius: 12px; border: 1px solid var(--glass-border); background: rgba(56, 38, 100, 0.6);
  color: var(--ink); font-weight: 800; cursor: pointer; font-size: 0.85rem; transition: transform 0.12s;
}
.result-card .row button:active { transform: scale(0.96); }
.result-card .row button.primary { background: var(--grad-accent); color: white; border-color: transparent; box-shadow: 0 10px 22px -10px rgba(123, 92, 255, 0.8); }

@media (max-width: 480px) {
  main { padding: 14px; }
  .topbar { padding: 12px 14px; }
  .brand-text h1 { font-size: 1.2rem; }
}

/* Liikeherkkyys: ei kelluvia animaatioita eikä sykkeitä */
@media (prefers-reduced-motion: reduce) {
  .bg-blobs::before, .bg-blobs::after { animation: none; }
  .tab-panel.active { animation: none; }
  .hehku-bar.active .hehku-fill { animation: none; }
  .result-overlay.show, .result-card { animation: none; }
}
