:root {
  --bg: #e8e1d7;
  --paper: #fffdf8;
  --ink: #1e1b18;
  --muted: #71685f;
  --accent: #4b7cff;
  --line: rgba(30, 27, 24, .16);
  --shadow: 0 18px 45px rgba(48, 37, 25, .18);
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
}

.app {
  position: fixed;
  inset: 0;
  min-width: 100vw;
  min-height: 100vh;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% 10%, rgba(255,255,255,.5), transparent 24%),
    radial-gradient(circle at 88% 78%, rgba(255,255,255,.35), transparent 28%),
    linear-gradient(135deg, #e7dfd3, #d7c9b8);
}

.hud {
  position: absolute;
  z-index: 50;
  top: env(safe-area-inset-top, 0px);
  left: 0;
  right: 0;
  height: 54px;
  display: grid;
  grid-template-columns: 180px 1fr 88px;
  align-items: center;
  padding: 8px max(18px, env(safe-area-inset-left)) 6px max(18px, env(safe-area-inset-left));
  pointer-events: none;
}

.brand {
  font-weight: 800;
  letter-spacing: -.04em;
  font-size: 21px;
  color: rgba(30, 27, 24, .72);
}

.status {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 38px;
  padding: 7px 16px;
  border-radius: 999px;
  background: rgba(255,255,255,.54);
  box-shadow: 0 8px 22px rgba(48, 37, 25, .08);
  backdrop-filter: blur(12px);
  pointer-events: auto;
}
.status strong { font-size: 15px; }
.status span { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.piece-menu {
  pointer-events: auto;
  justify-self: end;
  position: relative;
  width: 45px;
  height: 38px;
  border: 0;
  background: transparent;
  padding: 0;
  cursor: pointer;
}
.piece-menu span,
.piece-menu::before,
.piece-menu::after {
  position: absolute;
  content: "";
  background: #fff;
  box-shadow: 0 8px 20px rgba(48, 37, 25, .18);
}
.piece-menu span {
  inset: 7px 8px;
  border-radius: 6px;
}
.piece-menu::before {
  width: 13px; height: 13px; border-radius: 50%;
  left: 17px; top: 0;
}
.piece-menu::after {
  width: 12px; height: 12px; border-radius: 50%;
  right: 0; top: 15px;
}

.table {
  position: absolute;
  inset: 0;
  padding: 58px 18px 18px;
}

.board-wrap {
  position: absolute;
  z-index: 4;
  left: 50%; top: 52%;
  transform: translate(-50%, -50%);
  width: min(82vw, calc((100vh - 86px) * 1.5));
  height: min(calc(100vh - 104px), calc(82vw / 1.5));
  border-radius: 18px;
  padding: 6px;
  background: rgba(40, 33, 28, .92);
  box-shadow: 0 24px 65px rgba(48, 37, 25, .24);
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: 12px;
  background: rgba(255,255,255,.98);
}

#ghost {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: .20;
  pointer-events: none;
}

.grid {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(to right, rgba(0,0,0,.08) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0,0,0,.08) 1px, transparent 1px);
  opacity: .35;
  pointer-events: none;
}

.pieces-layer { position: absolute; inset: 0; z-index: 20; }

.piece {
  position: absolute;
  background: transparent;
  box-shadow: none;
  cursor: grab;
  touch-action: none;
  will-change: transform, left, top;
  transition: filter .12s ease, opacity .12s ease, transform .12s ease;
  transform-origin: center center;
}
.piece svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 9px 18px rgba(36, 28, 20, .28));
}
.piece-stroke {
  fill: none;
  stroke: rgba(30, 24, 18, .86);
  stroke-width: 2.6;
  stroke-linejoin: round;
}
.piece-highlight {
  fill: none;
  stroke: rgba(255,255,255,.30);
  stroke-width: 1.1;
  stroke-linejoin: round;
}
.piece.dragging {
  cursor: grabbing;
  z-index: 9999 !important;
  filter: saturate(1.08) brightness(1.03);
}
.piece.dragging svg {
  filter: drop-shadow(0 20px 38px rgba(36, 28, 20, .34));
}
.piece.rotated {
  filter: saturate(1.12) brightness(1.04);
}
.piece.locked {
  cursor: default;
}
.piece.locked svg {
  filter: drop-shadow(0 1px 2px rgba(36, 28, 20, .12));
}

.panel {
  position: absolute;
  z-index: 100;
  inset: 0;
  display: none;
  justify-content: flex-end;
  background: rgba(30, 27, 24, .18);
  backdrop-filter: blur(5px);
}
.panel.open { display: flex; }
.panel-card {
  width: min(360px, 86vw);
  height: calc(100% - 28px);
  margin: 14px;
  padding: 18px;
  border-radius: 24px;
  background: rgba(255,253,248,.94);
  box-shadow: var(--shadow);
  overflow: auto;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.panel h2 { margin: 0 0 12px; font-size: 25px; letter-spacing: -.04em; }
.panel button, .field input, .field input::file-selector-button {
  font: inherit;
}
#closePanel {
  border: 0;
  width: 38px; height: 38px;
  border-radius: 999px;
  background: #eee7de;
  font-size: 28px;
}
.field { display: grid; gap: 7px; margin: 14px 0; }
.field span { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 800; }
.field input { border: 1px solid var(--line); border-radius: 12px; padding: 11px; background: #fff; }
.sizes { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; margin: 16px 0; }
.sizes button, .actions button, #playAgain {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 10px;
  background: #fff;
  color: var(--ink);
  font-weight: 800;
}
.sizes button.active { border-color: rgba(75,124,255,.6); background: rgba(75,124,255,.1); color: #204ecf; }
.actions { display: grid; gap: 9px; }
.actions button:first-child { background: #1e1b18; color: white; }
.note { color: var(--muted); line-height: 1.45; font-size: 13px; }

.complete {
  position: absolute;
  z-index: 150;
  inset: 0;
  display: none;
  place-items: center;
  background: rgba(30,27,24,.22);
  backdrop-filter: blur(8px);
}
.complete.show { display: grid; }
.complete > div {
  text-align: center;
  min-width: 280px;
  padding: 26px;
  border-radius: 28px;
  background: rgba(255,253,248,.97);
  box-shadow: var(--shadow);
}
.complete h2 { margin: 0 0 8px; font-size: 30px; }

@media (orientation: portrait) {
  .hud { grid-template-columns: 90px 1fr 64px; }
  .brand { font-size: 16px; }
  .status { gap: 9px; padding-inline: 10px; }
  .status strong { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .board-wrap {
    width: min(94vw, calc((100vh - 110px) * .72));
    height: min(calc(100vh - 124px), calc(94vw / .72));
  }
}


@media (max-width: 1180px) and (orientation: landscape) {
  .board-wrap {
    width: min(80vw, calc((100vh - 84px) * 1.5));
    height: min(calc(100vh - 100px), calc(80vw / 1.5));
  }
  .brand {
    font-size: 18px;
  }
  .status {
    min-height: 34px;
    padding: 5px 13px;
  }
}
