:root {
  --app-w: 720px;
  --app-h: 1280px;
  --tile-w: 92px;
  --tile-h: 122px;
  --ui-scale: 0.58;
}

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

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: #062f21;
  font-family: Arial, Helvetica, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
}

#app {
  position: relative;
  width: var(--app-w);
  height: var(--app-h);
  flex: 0 0 auto;
  transform: scale(var(--app-scale, 1));
  transform-origin: center center;
  overflow: hidden;
  background: #092d1a;
  box-shadow: 0 0 40px rgba(0, 0, 0, .5);
}

#table {
  position: absolute;
  inset: 0;
  background: url('assets/table.jpg') center center / cover no-repeat;
}

/* ---------- top bar ---------- */

.topbar {
  position: absolute;
  top: 18px;
  left: 20px;
  right: 20px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  z-index: 500;
  pointer-events: none;
}

.topbar > * { pointer-events: auto; }

.round-btn {
  width: 62px;
  height: 62px;
  flex: 0 0 62px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, .95);
  background: rgba(8, 70, 50, .35);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  text-decoration: none;
  box-sizing: border-box;
  transition: background .15s ease, transform .15s ease;
}
.round-btn:hover { background: rgba(8, 70, 50, .7); transform: scale(1.05); }

.top-icon { width: 32px; height: 32px; fill: #fff; display: block; }

.pill {
  height: 54px;
  min-width: 126px;
  padding: 0 18px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, .28);
  background: rgba(9, 68, 50, .82);
  color: #fff;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: .4px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .18);
}

.level-pill { min-width: 160px; }
.count-pill { min-width: 126px; }
.time-pill { min-width: 164px; font-variant-numeric: tabular-nums; }

.tile-chip {
  width: 26px;
  height: 32px;
  border-radius: 4px;
  background: linear-gradient(#4aa8ff, #1677d2);
  border: 2px solid #cfe8ff;
  box-shadow: 0 2px 3px rgba(0, 0, 0, .3);
}

.ui-icon {
  display: inline-block;
  background-image: url('assets/ui.png');
  background-repeat: no-repeat;
  background-size: calc(390px * var(--icon-scale, var(--ui-scale))) calc(1021px * var(--icon-scale, var(--ui-scale)));
  background-position: calc(var(--ix, 0) * var(--icon-scale, var(--ui-scale)) * -1px) calc(var(--iy, 0) * var(--icon-scale, var(--ui-scale)) * -1px);
  width: calc(var(--iw, 40) * var(--icon-scale, var(--ui-scale)) * 1px);
  height: calc(var(--ih, 40) * var(--icon-scale, var(--ui-scale)) * 1px);
}

.clock-icon { --icon-scale: .38; }

/* ---------- selection tray ---------- */

.tray {
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 158px;
  height: 98px;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 7px;
  z-index: 300;
  pointer-events: none;
}

.slot {
  position: relative;
  border-radius: 9px;
  border: 1px solid rgba(255, 255, 255, .13);
  background: rgba(4, 51, 37, .66);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, .22);
}

/* ---------- prop buttons ---------- */

.props {
  position: absolute;
  left: 20px;
  right: 20px;
  bottom: 20px;
  height: 118px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 9px;
  z-index: 400;
}

.prop-btn {
  position: relative;
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 13px;
  background: rgba(6, 59, 43, .86);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  padding: 6px 2px 4px;
  transition: transform .12s, background .12s;
}

.prop-btn:active { transform: scale(.95); background: rgba(11, 82, 60, .95); }

.prop-icon { --icon-scale: .68; height: 66px; display: flex; align-items: center; justify-content: center; }

.prop-name { font-size: 21px; font-weight: 700; line-height: 1; }

.prop-badge {
  position: absolute;
  top: -8px;
  right: -8px;
  min-width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #43c947;
  border: 2px solid #eaffea;
  color: #fff;
  font-size: 20px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, .3);
}
.prop-badge.empty::after { content: '+'; }
.prop-badge.infinite {
  background: #f2c14e;
  border-color: #fff6d8;
  color: #153d2b;
  font-size: 25px;
  font-weight: 800;
}

/* ---------- tiles ---------- */

.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  border-radius: 11px;
  background: linear-gradient(155deg, #ffffff 0%, #f7f7f7 55%, #dfe2e2 100%);
  border: 1px solid rgba(120, 130, 130, .55);
  box-shadow: 5px 8px 9px rgba(0, 0, 0, .34), inset 0 1px 1px rgba(255, 255, 255, .9);
  cursor: pointer;
  z-index: 20;
  transform-origin: 50% 60%;
  transition: left .28s cubic-bezier(.2, .8, .25, 1), top .28s cubic-bezier(.2, .8, .25, 1), transform .28s cubic-bezier(.2, .8, .25, 1), opacity .22s;
  will-change: transform, left, top;
}

.tile::before {
  content: '';
  position: absolute;
  right: -5px;
  top: 5px;
  width: 8px;
  height: calc(100% - 2px);
  border-radius: 0 8px 9px 0;
  background: linear-gradient(90deg, #c8cccd, #aeb4b5);
  z-index: -1;
}

.tile::after {
  content: '';
  position: absolute;
  left: 2px;
  right: -3px;
  bottom: -6px;
  height: 9px;
  border-radius: 0 0 9px 9px;
  background: linear-gradient(#c3c8c9, #a2a9aa);
  z-index: -2;
}

.tile .face {
  position: absolute;
  inset: 5px;
  border-radius: 6px;
  background-image: url('assets/tiles.jpg');
  background-repeat: no-repeat;
  background-size: calc(1952px * var(--face-scale)) calc(810px * var(--face-scale));
  background-position: calc(var(--fx) * var(--face-scale) * -1px - (120px * var(--face-scale) - var(--tile-w) + 10px) / 2) calc(var(--fy) * var(--face-scale) * -1px - (160px * var(--face-scale) - var(--tile-h) + 10px) / 2);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .05);
}

.tile.in-tray {
  z-index: 320;
  cursor: default;
  transform: scale(.86) rotate(0deg) !important;
  box-shadow: 3px 5px 7px rgba(0, 0, 0, .35);
}

.tile.removing { opacity: 0; transform: scale(.1) rotate(30deg) !important; }

.tile.spawning { opacity: 0; transform: scale(.25) translateY(-260px) rotate(-40deg); }

.tile.shake { animation: tileShake .38s ease-in-out; }
@keyframes tileShake {
  0%, 100% { margin-left: 0; }
  20% { margin-left: -7px; }
  40% { margin-left: 6px; }
  60% { margin-left: -4px; }
  80% { margin-left: 3px; }
}

.tile.flip { animation: tileFlip .5s ease-in-out; }
@keyframes tileFlip {
  0% { transform: rotateY(0deg); }
  50% { transform: rotateY(180deg) scale(.92); }
  100% { transform: rotateY(360deg); }
}

/* tutorial hand */

.hand-pointer {
  position: absolute;
  width: 88px;
  height: 82px;
  left: 316px;
  top: 558px;
  z-index: 350;
  pointer-events: none;
  --icon-scale: .9;
  background-image: url('assets/ui.png');
  background-repeat: no-repeat;
  background-size: calc(390px * var(--icon-scale)) calc(1021px * var(--icon-scale));
  background-position: calc(var(--ix, 0) * var(--icon-scale) * -1px) calc(var(--iy, 0) * var(--icon-scale) * -1px);
  filter: drop-shadow(0 4px 5px rgba(0, 0, 0, .25));
  animation: handPulse 1.6s ease-in-out infinite;
}

@keyframes handPulse {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(7px, -9px) scale(.94); }
}

.hidden { display: none !important; }

/* ---------- toast ---------- */

.toast {
  position: absolute;
  left: 50%;
  top: 130px;
  transform: translateX(-50%);
  z-index: 900;
  background: rgba(0, 0, 0, .72);
  color: #fff;
  padding: 12px 24px;
  border-radius: 22px;
  font-size: 22px;
  white-space: nowrap;
  pointer-events: none;
}

/* ---------- magnifier ---------- */

.magnifier {
  position: absolute;
  width: 240px;
  height: 240px;
  border-radius: 50%;
  overflow: hidden;
  outline: 5px solid rgba(255, 255, 255, .92);
  outline-offset: -5px;
  box-shadow: 0 14px 32px rgba(0, 0, 0, .55), inset 0 0 0 2px rgba(0, 0, 0, .28);
  background: #0a4a35;
  pointer-events: none;
  z-index: 850;
}
.magnifier-view {
  position: absolute;
  left: 0;
  top: 0;
  width: 720px;
  height: 1280px;
  transform-origin: 0 0;
  background: url('assets/table.jpg') center center / cover no-repeat;
  pointer-events: none;
}
.magnifier-view .tile { pointer-events: none; transition: none; }
.magnifier-cross {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 26px;
  height: 26px;
  transform: translate(-50%, -50%);
  border: 2px solid rgba(255, 255, 255, .95);
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .45), inset 0 0 0 1px rgba(0, 0, 0, .25);
}
.magnifier-cross::before,
.magnifier-cross::after {
  content: '';
  position: absolute;
  background: rgba(255, 255, 255, .95);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, .35);
}
.magnifier-cross::before { left: 50%; top: -8px; width: 2px; height: 42px; transform: translateX(-50%); }
.magnifier-cross::after { top: 50%; left: -8px; height: 2px; width: 42px; transform: translateY(-50%); }
.zoom-active #table { pointer-events: auto; }

/* ---------- overlays ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px;
}

.panel {
  width: 100%;
  max-width: 560px;
  border-radius: 24px;
  border: 2px solid rgba(255, 255, 255, .35);
  background: linear-gradient(#126c4c, #085033);
  box-shadow: 0 18px 45px rgba(0, 0, 0, .5);
  padding: 30px 28px;
  color: #fff;
  text-align: center;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 40px;
  letter-spacing: .5px;
}

.panel p { margin: 0 0 22px; font-size: 24px; opacity: .92; }

.panel .big-num { font-size: 54px; font-weight: 800; color: #ffe27a; margin: 6px 0 18px; }

.btn {
  min-width: 180px;
  height: 64px;
  margin: 8px;
  border-radius: 16px;
  border: 2px solid rgba(255, 255, 255, .55);
  background: linear-gradient(#39b869, #1f8f4e);
  color: #fff;
  font-size: 26px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 5px 0 rgba(0, 0, 0, .25);
}
.btn.secondary { background: linear-gradient(#2c7c5e, #1a6048); }
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0, 0, 0, .25); }

.panel-actions { display: flex; flex-wrap: wrap; justify-content: center; }

.ad-label {
  font-size: 16px;
  letter-spacing: 3px;
  opacity: .68;
  margin-bottom: 8px;
}
.ad-panel h2 { font-size: 32px; line-height: 1.25; }
.ad-copy {
  font-size: 30px;
  font-weight: 800;
  color: #ffe27a;
  margin: 4px 0 10px;
}
.ad-panel p { font-size: 21px; line-height: 1.5; margin-bottom: 12px; }
.ad-link {
  color: #ffe27a;
  font-weight: 800;
  text-decoration: underline;
  word-break: break-all;
}
.ad-progress {
  height: 10px;
  border-radius: 6px;
  background: rgba(0, 0, 0, .38);
  overflow: hidden;
  margin: 16px 0 8px;
}
.ad-progress-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #39b869, #ffe27a);
  transition: width 1s linear;
}
.ad-countdown { font-size: 20px; opacity: .86; margin-bottom: 12px; }
.btn:disabled { opacity: .48; cursor: not-allowed; box-shadow: none; transform: none; }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 24px;
  padding: 14px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, .14);
}

.toggle {
  width: 84px;
  height: 44px;
  border-radius: 22px;
  border: 2px solid rgba(255, 255, 255, .45);
  background: #2b7d5d;
  position: relative;
  cursor: pointer;
}
.toggle::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #fff;
  transition: left .18s;
}
.toggle.on { background: #43c66f; }
.toggle.on::after { left: 41px; }

.level-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
  margin-bottom: 16px;
}
.level-cell {
  aspect-ratio: 1;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .35);
  background: rgba(255, 255, 255, .12);
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
}
.level-cell:disabled { opacity: .3; cursor: not-allowed; }
.level-cell.current { background: #f2c14e; color: #153d2b; }

@media (max-width: 480px) {
  .topbar { top: 12px; left: 12px; right: 12px; }
  .pill { font-size: 22px; min-width: 106px; height: 48px; padding: 0 12px; }
  .level-pill { min-width: 136px; }
  .time-pill { min-width: 138px; }
  .round-btn { width: 52px; height: 52px; flex-basis: 52px; }
  .tray { left: 12px; right: 12px; bottom: 148px; height: 86px; }
  .props { left: 12px; right: 12px; bottom: 14px; height: 108px; }
  .prop-name { font-size: 18px; }
}
