:root {
  --bg: #09090b;
  --surface: #111114;
  --surface-2: #18181c;
  --surface-3: #232329;
  --line: rgba(255, 255, 255, 0.08);
  --line-strong: rgba(255, 255, 255, 0.14);
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --subtle: #71717a;
  --accent: #4ade80;
  --accent-2: #2dd4bf;
  /* Position colors match the labels drawn on the field. */
  --off: #f2c230;
  --def: #5bc0f8;
  --radius: 10px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-field: "Roboto Condensed", "Arial Narrow", sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background:
    radial-gradient(900px 520px at 0% 0%, rgba(74, 222, 128, 0.07), transparent 65%),
    radial-gradient(700px 480px at 100% 100%, rgba(45, 212, 191, 0.05), transparent 65%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Layout (stacked: phones & tablets) ---------- */
.app {
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  flex: 1;
  display: flex;
  flex-direction: column;
}
/* Phones/tablets: the panel's pieces join the page flow so the sprite switch can sit under the
   field (title, buttons, field, sprite switch), and the field runs edge to edge. */
.panel { display: contents; }
.toolbar { padding-inline: 16px; }
.stage { order: 1; padding: 0 0 18px; }
.style-switch { order: 2; }

.top {
  text-align: center;
  padding: 28px 16px 18px;
}
.top h1 {
  margin: 0;
  font-size: clamp(28px, 5vw, 40px);
  font-weight: 800;
  letter-spacing: -0.035em;
  line-height: 1.05;
}
.top h1 .accent {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.sub {
  margin: 8px auto 0;
  max-width: 560px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

/* ---------- Buttons ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 14px;
}
button {
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 14px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}
button:hover { background: var(--surface-3); border-color: var(--line-strong); }
button:active { transform: scale(0.98); }
button.primary {
  background: #fafafa;
  color: #09090b;
  border-color: transparent;
}
button.primary:hover { background: #e4e4e7; }
button:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.toolbar button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.toolbar svg, .close svg {
  width: 16px;
  height: 16px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.toolbar button:not(.primary) svg { color: var(--muted); }

/* ---------- Sprite style: segmented control ---------- */
.style-switch {
  min-width: 0;
  margin: 0 auto 8px;
  padding: 0;
  border: 0;
  width: calc(100% - 32px);
  max-width: 320px;
}
.style-switch legend {
  padding: 0;
  margin: 0 auto 8px;
  color: var(--subtle);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 2px;
  padding: 3px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.segmented label { cursor: pointer; min-width: 0; }
.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.segmented span {
  display: block;
  text-align: center;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  padding: 7px 4px;
  border-radius: 7px;
  transition: background 0.15s, color 0.15s;
}
.segmented label:hover span { color: var(--text); }
.segmented input:checked + span {
  background: var(--surface-3);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), inset 0 0 0 1px var(--line-strong);
}
.segmented input:focus-visible + span {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ---------- Field ---------- */
.field-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 2;
  overflow: hidden;
  border-block: 1px solid var(--line);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
#field {
  display: block;
  width: 100%;
  height: 100%;
}
#hotspots { position: absolute; inset: 0; }

.hotspot {
  position: absolute;
  padding: 0;
  margin: 0;
  background: transparent;
  border: 2px solid transparent;
  border-radius: 12px;
  cursor: grab;
  touch-action: none;
}
.hotspot:hover { background: rgba(255, 255, 255, 0.08); border-color: rgba(255, 255, 255, 0.4); }
.hotspot.drop-target { background: rgba(74, 222, 128, 0.16); border-color: var(--accent); }
.hotspot.dragging { opacity: 0.4; }
.hotspot:active { transform: none; }

.ghost {
  position: fixed;
  pointer-events: none;
  z-index: 50;
  width: 96px;
  height: 96px;
  transform: translate(-50%, -60%);
  filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.5));
}
.ghost.pixelated { image-rendering: pixelated; }

/* ---------- Picker ---------- */
dialog#picker {
  width: min(860px, calc(100vw - 32px));
  max-height: min(760px, calc(100vh - 48px));
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 30px 100px rgba(0, 0, 0, 0.7);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
dialog#picker[open] { display: flex; }
dialog::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(4px); }

.picker-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 10px;
  margin: 0;
}
.picker-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.picker-title h2 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.pos-chip {
  font-family: var(--font-field);
  font-weight: 700;
  font-size: 14px;
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--bg);
  border: 1px solid var(--line-strong);
}
.pos-chip.off { color: var(--off); }
.pos-chip.def { color: var(--def); }
.close {
  display: inline-flex;
  padding: 8px;
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.close:hover { color: var(--text); }

.picker-tools {
  display: flex;
  gap: 8px;
  padding: 0 16px;
}
.picker-tools input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 10px 12px;
  transition: border-color 0.15s;
}
.picker-tools input::placeholder { color: var(--subtle); }
.picker-tools input:focus { border-color: var(--accent); outline: none; }
.picker-hint {
  margin: 8px 16px 4px;
  color: var(--subtle);
  font-size: 12px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 8px;
  padding: 8px 16px 16px;
  overflow-y: auto;
}
.mon {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px 10px;
  border-radius: 12px;
  background: var(--surface-2);
}
.mon:hover { transform: translateY(-1px); }
.mon[hidden], .empty-msg[hidden] { display: none; }
.mon img {
  width: 72px;
  height: 72px;
  image-rendering: pixelated;
}
.mon .num { color: var(--subtle); font-size: 11px; font-weight: 500; font-variant-numeric: tabular-nums; }
.mon .nm { font-size: 13px; font-weight: 600; line-height: 1.15; text-align: center; }
.mon.current { border-color: var(--accent); background: rgba(74, 222, 128, 0.08); }
.mon .tag {
  position: absolute;
  top: 6px;
  right: 6px;
  font-family: var(--font-field);
  font-weight: 700;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--bg);
  border: 1px solid var(--line);
}
.mon .tag.off { color: var(--off); }
.mon .tag.def { color: var(--def); }
.empty-msg { color: var(--muted); grid-column: 1 / -1; text-align: center; padding: 24px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--surface-2);
  border: 1px solid var(--line-strong);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  font-size: 14px;
  font-weight: 500;
  padding: 10px 16px;
  border-radius: var(--radius);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 60;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Footer ---------- */
footer {
  text-align: center;
  padding: 24px 16px 32px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
  border-top: 1px solid var(--line);
  margin-top: 16px;
}
footer p { margin: 0 0 4px; }
footer a { color: var(--accent); font-weight: 600; text-decoration: none; }
footer a:hover { text-decoration: underline; text-underline-offset: 3px; }
footer .fine { margin-top: 10px; font-size: 12px; color: var(--subtle); }

@media (max-width: 600px) {
  button { font-size: 13px; padding: 8px 12px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
  .mon img { width: 56px; height: 56px; }
  dialog#picker { max-height: calc(100vh - 24px); width: calc(100vw - 16px); }
}

/* ---------- Wide screens: controls in a slim left column so the field can use the full window height ---------- */
@media (min-width: 1000px) and (min-height: 560px) {
  .app {
    display: grid;
    /* Field column is capped at the width whose 3:2 height fits the window (minus 16px top/bottom). */
    grid-template-columns: 236px minmax(0, calc((100vh - 32px) * 1.5));
    grid-template-columns: 236px minmax(0, calc((100svh - 32px) * 1.5));
    justify-content: center;
    align-items: center;
    gap: 28px;
    max-width: none;
    min-height: 100vh;
    min-height: 100svh;
    padding: 16px 24px;
  }
  .panel {
    display: flex;
    flex-direction: column;
    gap: 18px;
    padding: 0;
  }
  .top, .toolbar { padding-inline: 0; }
  .top { text-align: left; padding: 0; }
  .top h1 { font-size: 34px; line-height: 1; }
  .top h1 span { display: block; }
  .sub { margin: 12px 0 0; font-size: 14px; }
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    margin: 0;
  }
  .toolbar button { justify-content: flex-start; }
  .style-switch { margin: 0; width: auto; max-width: none; }
  .style-switch legend { margin: 0 0 8px; }
  .stage {
    min-width: 0;
    padding: 0;
  }
  .field-wrap {
    border-radius: 14px;
    border: 1px solid var(--line);
  }
}
