:root {
  --background: #f4f1ea;
  --foreground: #171717;
  --panel: #fffaf0;
  --ink-soft: #5b5b52;
  --line: #d8d0bd;
  --danger: #c1352b;
  --clear: #247a55;
}

* { box-sizing: border-box; }

body {
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(90deg, rgba(23, 23, 23, 0.04) 1px, transparent 1px),
    linear-gradient(rgba(23, 23, 23, 0.04) 1px, transparent 1px),
    var(--background);
  background-size: 28px 28px;
  color: var(--foreground);
  font-family: Arial, Helvetica, sans-serif;
}

button, input { font: inherit; }

.game-shell {
  width: min(1180px, calc(100vw - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 28px 0 34px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: #7e312b;
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

h1 {
  max-width: 760px;
  margin: 0;
  font-size: clamp(2rem, 5vw, 4.25rem);
  line-height: 0.95;
}

.status-stack { display: grid; gap: 8px; justify-items: end; }

.sync-pill, .status-pill {
  border-radius: 999px;
  background: var(--panel);
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
}

.sync-pill {
  border: 1px solid var(--line);
  color: var(--ink-soft);
  font-size: 0.78rem;
  padding: 7px 12px;
}

.sync-pill.is-offline {
  border-color: var(--danger);
  color: var(--danger);
}

.status-pill {
  min-width: 230px;
  border: 1px solid var(--line);
  color: var(--ink-soft);
  padding: 10px 14px;
}

.status-pill.is-clear {
  border-color: var(--clear);
  color: var(--clear);
}

.status-pill.is-danger {
  border-color: var(--danger);
  background: var(--danger);
  color: #fffaf0;
}

.control-panel {
  display: grid;
  grid-template-columns: repeat(5, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.control-panel label, .game-id-form, .button-row {
  min-height: 86px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.9);
  box-shadow: 0 10px 24px rgba(30, 24, 12, 0.08);
}

.control-panel label {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto 1fr;
  gap: 10px 12px;
  padding: 14px;
}

.control-panel span, .game-id-form label {
  color: var(--ink-soft);
  font-size: 0.82rem;
  font-weight: 800;
}

.control-panel strong {
  font-size: 0.86rem;
  justify-self: end;
  white-space: nowrap;
}

.control-panel input[type="range"] {
  grid-column: 1 / -1;
  width: 100%;
  accent-color: #1f7a8c;
}

.game-id-form { display: grid; gap: 10px; padding: 14px; }

.game-id-form div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.game-id-form input {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fffef9;
  padding: 8px 9px;
  font-size: 0.88rem;
  font-weight: 800;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 10px;
}

.button-row button, .game-id-form button {
  border: 1px solid #171717;
  border-radius: 7px;
  background: #171717;
  color: #fffaf0;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 800;
}

.button-row button:disabled {
  border-color: var(--line);
  background: #eee6d5;
  color: #928977;
  cursor: not-allowed;
}

.game-area {
  display: grid;
  grid-template-columns: 240px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.tray, .board-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 250, 240, 0.64);
  padding: 14px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.65);
}

.tray { min-height: 320px; }

.tray-title {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: -14px -14px 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 250, 240, 0.96);
  padding: 12px 14px;
}

.tray-title span {
  color: var(--ink-soft);
  font-size: 0.82rem;
  font-weight: 800;
}

.tray-title strong {
  display: grid;
  min-width: 30px;
  min-height: 30px;
  place-items: center;
  border-radius: 999px;
  background: #171717;
  color: #fffaf0;
  font-size: 0.82rem;
}

.tray-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(82px, 1fr));
  gap: 18px 12px;
  align-items: center;
  justify-items: center;
  padding: 6px 0 2px;
}

.field {
  position: relative;
  min-width: 420px;
  min-height: 320px;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  background:
    linear-gradient(90deg, rgba(31, 122, 140, 0.14) 1px, transparent 1px),
    linear-gradient(rgba(31, 122, 140, 0.14) 1px, transparent 1px),
    #fefcf5;
  background-size: 34px 34px;
  box-shadow: 0 20px 50px rgba(30, 24, 12, 0.16);
  touch-action: none;
}

.field-clear { box-shadow: 0 20px 50px rgba(36, 122, 85, 0.18); }

.force-map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  clip-path: inherit;
  pointer-events: none;
}

.force-map line {
  stroke: var(--danger);
  stroke-linecap: round;
  stroke-width: 6;
}

.field-outline,
.field-outline-shadow {
  fill: none;
  pointer-events: none;
}

.field-outline-shadow {
  stroke: rgba(30, 24, 12, 0.18);
  stroke-linejoin: round;
  stroke-width: 17;
  transform: translate(3px, 4px);
}

.field-outline {
  stroke: #f7f1df;
  stroke-linejoin: round;
  stroke-width: 12;
}

.field-clear .field-outline {
  stroke: #bfe2ce;
}

.magnet {
  position: absolute;
  display: block;
  overflow: hidden;
  border: 1px solid rgba(9, 10, 11, 0.95);
  background:
    radial-gradient(circle at 30% 24%, color-mix(in srgb, var(--glint) 74%, transparent), transparent 12%),
    radial-gradient(circle at 70% 68%, rgba(255, 255, 255, 0.12), transparent 16%),
    linear-gradient(135deg, rgba(255, 255, 255, 0.2), transparent 34%),
    linear-gradient(42deg, transparent 0 22%, rgba(255, 255, 255, 0.08) 23% 25%, transparent 26% 45%, rgba(255, 255, 255, 0.1) 46% 48%, transparent 49%),
    linear-gradient(155deg, color-mix(in srgb, var(--stone) 74%, #ffffff), var(--stone) 48%, #070809);
  box-shadow:
    inset -10px -12px 16px rgba(0, 0, 0, 0.42),
    inset 8px 7px 10px rgba(255, 255, 255, 0.08),
    0 8px 0 rgba(9, 10, 11, 0.9),
    0 15px 22px rgba(23, 23, 23, 0.16);
  cursor: grab;
  padding: 0;
  transform-origin: center;
  transition: box-shadow 120ms ease, outline-color 120ms ease;
  user-select: none;
}

.magnet::before, .magnet::after {
  position: absolute;
  content: "";
  pointer-events: none;
}

.magnet::before {
  inset: 13% 17% auto auto;
  width: 38%;
  height: 24%;
  border-radius: 50%;
  background: linear-gradient(110deg, rgba(255, 255, 255, 0.28), transparent 72%);
  filter: blur(0.4px);
  transform: rotate(-12deg);
}

.magnet::after {
  inset: 20% 14% 18% 18%;
  background:
    linear-gradient(28deg, transparent 0 38%, rgba(255, 255, 255, 0.11) 39% 41%, transparent 42%),
    linear-gradient(117deg, transparent 0 58%, rgba(0, 0, 0, 0.38) 59% 61%, transparent 62%);
  opacity: 0.85;
}

.tray-magnet { position: relative; left: auto; top: auto; }
.magnet:active { cursor: grabbing; box-shadow: 0 3px 0 rgba(23, 23, 23, 0.9), 0 8px 16px rgba(23, 23, 23, 0.2); }
.magnet.is-unsafe { outline: 4px solid rgba(193, 53, 43, 0.42); outline-offset: 3px; }
.magnet.is-selected { outline: 4px solid rgba(239, 176, 54, 0.72); outline-offset: 4px; }
.magnet.is-attracted { cursor: default; transition: left 16ms linear, top 16ms linear, transform 80ms ease-out, box-shadow 120ms ease, outline-color 120ms ease; }
.magnet.is-stuck {
  cursor: not-allowed;
  outline: 5px solid rgba(193, 53, 43, 0.68);
  outline-offset: 1px;
  box-shadow:
    inset -10px -12px 16px rgba(0, 0, 0, 0.5),
    inset 8px 7px 10px rgba(255, 255, 255, 0.1),
    0 3px 0 rgba(9, 10, 11, 0.94),
    0 10px 18px rgba(193, 53, 43, 0.26);
}

@media (max-width: 920px) {
  .topbar { align-items: start; flex-direction: column; }
  .status-stack { justify-items: start; }
  .control-panel { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .game-area { grid-template-columns: 1fr; }
  .tray { min-height: 0; }
  .tray-grid { grid-template-columns: repeat(4, minmax(82px, 1fr)); }
}

@media (max-width: 620px) {
  .game-shell { width: min(100vw - 20px, 1180px); padding-top: 18px; }
  h1 { font-size: 2.15rem; }
  .control-panel { grid-template-columns: 1fr; }
  .button-row { min-height: 70px; }
  .tray-grid { grid-template-columns: repeat(2, minmax(82px, 1fr)); }
}
