:root {
  --ink: #120d0a;
  --panel: #211611;
  --panel-soft: #302018;
  --gold: #d7b66a;
  --gold-bright: #ffe49a;
  --parchment: #efe0b4;
  --muted: #bda77c;
  --danger: #a53d2f;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
  font-family: Georgia, "Times New Roman", serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 50% 12%, rgba(219, 179, 95, 0.18), transparent 28rem),
    linear-gradient(135deg, #100b09, #241611 45%, #0d0908);
  color: var(--parchment);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 2.45rem;
  border: 1px solid rgba(215, 182, 106, 0.55);
  border-radius: 6px;
  background: linear-gradient(#3b281d, #21150f);
  color: var(--parchment);
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

button:hover {
  border-color: var(--gold-bright);
  color: #fff6d7;
}

button.primary {
  background: linear-gradient(#c99c45, #806126);
  color: #160d08;
  font-weight: 700;
}

button.danger,
button.danger-ghost {
  border-color: rgba(202, 83, 64, 0.8);
}

button.danger {
  background: linear-gradient(#8a2f25, #4b1814);
}

button.danger-ghost {
  color: #ffc4ba;
}

label {
  display: grid;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.88rem;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid rgba(215, 182, 106, 0.36);
  border-radius: 6px;
  background: #130d0a;
  color: var(--parchment);
  padding: 0.65rem 0.7rem;
}

textarea {
  resize: vertical;
}

.app-shell {
  min-height: 100vh;
  padding: 1rem;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin: 0 auto 1rem;
  max-width: 1680px;
  border-bottom: 1px solid transparent;
  padding-bottom: 0.5rem;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.eyebrow {
  margin: 0 0 0.15rem;
  color: var(--gold);
  font-size: 0.78rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  color: #ffe8ac;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(1.65rem, 3vw, 2.8rem);
}

.combat-state {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  margin-left: auto;
  border: 1px solid rgba(215, 182, 106, 0.38);
  border-radius: 999px;
  padding: 0.35rem 0.45rem 0.35rem 0.8rem;
  background: rgba(18, 12, 9, 0.78);
  box-shadow: inset 0 0 16px rgba(215, 182, 106, 0.08);
}

.combat-state span {
  color: var(--gold);
  font-size: 0.72rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.combat-state select {
  width: auto;
  min-width: 12rem;
  border-radius: 999px;
  padding-block: 0.45rem;
  font-weight: 700;
}

.app-shell[data-combat-state="active"] .combat-state {
  box-shadow: 0 0 24px rgba(255, 218, 118, 0.24), inset 0 0 16px rgba(215, 182, 106, 0.1);
}

.app-shell[data-combat-state="active"] .topbar {
  border-bottom-color: rgba(255, 220, 120, 0.25);
  box-shadow: 0 10px 28px -26px rgba(255, 220, 120, 0.7);
}

.app-shell[data-combat-state="resolution"] .combat-state {
  border-color: rgba(187, 221, 214, 0.65);
}

.app-shell[data-combat-state="resolution"] .topbar {
  border-bottom-color: rgba(187, 221, 214, 0.22);
}

.app-shell[data-combat-state="new-phase"] .combat-state {
  border-color: rgba(255, 228, 154, 0.85);
}

.app-shell[data-combat-state="new-phase"] .topbar {
  border-bottom-color: rgba(255, 228, 154, 0.28);
}

h2 {
  font-size: 1.05rem;
}

.panel-toggle {
  display: none;
  padding-inline: 1rem;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 1rem;
  max-width: 1680px;
  margin: 0 auto;
}

.board-column {
  min-width: 0;
}

.phase-strip,
.staging-area,
.control-panel,
.token-dialog {
  border: 1px solid rgba(215, 182, 106, 0.35);
  background: rgba(25, 16, 12, 0.92);
  box-shadow: var(--shadow);
}

.phase-strip {
  display: flex;
  align-items: end;
  gap: 0.55rem;
  flex-wrap: wrap;
  padding: 0.75rem;
  border-radius: 8px;
  margin-bottom: 0.8rem;
}

.phase-strip label {
  width: 5.5rem;
}

.current-sr {
  min-width: 7.25rem;
  border: 1px solid rgba(255, 228, 154, 0.42);
  border-radius: 7px;
  padding: 0.42rem 0.7rem;
  background: radial-gradient(circle at 50% 0, rgba(255, 228, 154, 0.2), rgba(0, 0, 0, 0.12));
}

.current-sr span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
}

.current-sr strong {
  display: block;
  font-size: 1.8rem;
  line-height: 1;
}

.board-frame {
  border-radius: 8px;
  border: 1px solid rgba(215, 182, 106, 0.38);
  background: #060403;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.board-shell {
  position: relative;
  width: 100%;
  line-height: 0;
  user-select: none;
}

.board-image {
  display: block;
  width: 100%;
  height: auto;
}

.sr-layer,
.token-layer {
  position: absolute;
  inset: 0;
}

.token-layer {
  pointer-events: none;
}

.sr-marker {
  position: absolute;
  width: clamp(1.65rem, 3.2vw, 3rem);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  pointer-events: auto;
}

@keyframes srPulse {
  0% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.08); }
  100% { transform: translate(-50%, -50%) scale(1); }
}

.sr-marker.active {
  border: 2px solid rgba(255, 228, 154, 0.95);
  box-shadow: 0 0 24px 8px rgba(255, 215, 110, 0.65), inset 0 0 14px rgba(255, 228, 154, 0.3);
  animation: srPulse 1.5s infinite ease-in-out;
}

.token {
  --token-color: #d7b66a;
  position: absolute;
  pointer-events: auto;
  left: calc(var(--x) * 1%);
  top: calc(var(--y) * 1%);
  width: clamp(4rem, 5vw, 5.25rem);
  min-height: clamp(4rem, 5vw, 5.25rem);
  transform: translate(calc(-50% + var(--offset-x, 0px)), calc(-50% + var(--offset-y, 0px)));
  display: grid;
  place-items: center;
  border: 3px solid var(--token-color);
  background: radial-gradient(circle at 32% 24%, rgba(255, 255, 255, 0.28), transparent 28%), #1b110d;
  color: #fff7d6;
  backdrop-filter: blur(1px);
  box-shadow: 0 7px 16px rgba(0, 0, 0, 0.55), inset 0 0 10px rgba(0, 0, 0, 0.35);
  cursor: grab;
  line-height: 1.05;
  touch-action: none;
  z-index: 5;
}

.token:active {
  cursor: grabbing;
}

@keyframes tokenResolvePulse {
  0% { filter: brightness(1); }
  50% { filter: brightness(1.28); }
  100% { filter: brightness(1); }
}

.token:hover,
.token:focus-visible {
  transform:
    translate(calc(-50% + var(--offset-x, 0px)),
              calc(-50% + var(--offset-y, 0px)))
    scale(1.18);
  z-index: 30;
}

.token.active {
  outline: 3px solid rgba(255, 235, 170, 0.95);
  outline-offset: 3px;
  box-shadow: 0 0 0 3px rgba(255, 234, 163, 0.88), 0 0 25px rgba(255, 221, 112, 0.95), 0 7px 16px rgba(0, 0, 0, 0.6);
  animation: tokenResolvePulse 1.25s infinite ease-in-out;
  z-index: 10;
}

.token.dragging {
  opacity: 0.88;
  z-index: 20;
}

.token-inner {
  display: grid;
  place-items: center;
  gap: 0.12rem;
  width: 100%;
  height: 100%;
  padding: 0.25rem;
  text-align: center;
}

.token-glyph {
  display: grid;
  place-items: center;
  width: 1.85rem;
  height: 1.85rem;
  font-size: 1.35rem;
  font-weight: 800;
}

.token-label {
  max-width: 4.65rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  line-height: 1;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-size: 0.82rem;
  font-weight: 700;
  text-shadow: 0 1px 2px #000;
}

.token-owner {
  max-width: 4.65rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: normal;
  line-height: 1;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  color: #e6d0a0;
  font-size: 0.62rem;
}

.token-actions {
  position: absolute;
  top: -0.55rem;
  right: -0.55rem;
  display: flex;
  gap: 0.18rem;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.token:hover .token-actions,
.token:focus-within .token-actions {
  opacity: 1;
}

.token-action {
  display: grid;
  place-items: center;
  width: 1.6rem;
  min-height: 1.6rem;
  padding: 0;
  border-radius: 50%;
  background: #100a07;
  font-family: Arial, sans-serif;
  font-size: 0.9rem;
  line-height: 1;
}

.token.character {
  border-radius: 50%;
}

.token.weapon {
  border-radius: 35% 12% 35% 12%;
}

.token.weapon .token-glyph::before {
  content: "";
  width: 1.65rem;
  height: 0.2rem;
  background: currentColor;
  box-shadow: 0 0.33rem 0 currentColor;
  transform: rotate(-36deg);
}

.token.spell {
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.14), transparent 34%), #17100f;
}

.token.spell .token-glyph {
  border: 2px solid currentColor;
  border-radius: 50%;
  box-shadow: 0 0 10px currentColor;
}

.token.psionic {
  border-radius: 50% 8px;
}

.token.psionic .token-glyph::before {
  content: "◉";
  font-size: 1.35rem;
  line-height: 1;
  text-shadow: 0 0 8px currentColor;
}

.token.divine {
  border-radius: 50%;
  outline: 2px solid rgba(255, 255, 255, 0.16);
  outline-offset: 4px;
}

.token.divine .token-glyph::before {
  content: "";
  width: 1.65rem;
  aspect-ratio: 1;
  background: currentColor;
  clip-path: polygon(50% 0, 61% 35%, 100% 50%, 61% 65%, 50% 100%, 39% 65%, 0 50%, 39% 35%);
}

.token.skill {
  border-radius: 7px;
  transform: translate(calc(-50% + var(--offset-x, 0px)), calc(-50% + var(--offset-y, 0px))) rotate(45deg);
}

.token.skill:hover,
.token.skill:focus-visible {
  transform: translate(calc(-50% + var(--offset-x, 0px)), calc(-50% + var(--offset-y, 0px))) rotate(45deg) scale(1.18);
}

.token.skill .token-inner {
  transform: rotate(-45deg);
}

.token.active.divine,
.token.active {
  outline: 3px solid rgba(255, 235, 170, 0.95);
  outline-offset: 3px;
}

.token.misc {
  border-radius: 8px;
}

.token.misc .token-glyph::before {
  content: "...";
  font-family: Arial, sans-serif;
}

.staging-area {
  margin-top: 0.8rem;
  border-style: dashed;
  border-radius: 8px;
  padding: 0.8rem;
  opacity: 0.88;
}

.staging-area strong {
  color: #ffe8ac;
  margin-right: 0.5rem;
}

.staging-area span {
  color: var(--muted);
}

.control-panel {
  display: grid;
  align-content: start;
  gap: 0.9rem;
  border-radius: 8px;
  padding: 0.9rem;
  max-height: calc(100vh - 6rem);
  overflow: auto;
}

@media (min-width: 641px) {
  .control-panel {
    position: sticky;
    top: 0.5rem;
  }
}

.panel-section {
  display: grid;
  gap: 0.75rem;
}

#tokenForm,
#editForm {
  display: grid;
  gap: 0.75rem;
}

.button-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.legend {
  display: grid;
  gap: 0.45rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--muted);
  font-size: 0.9rem;
}

.legend-swatch {
  width: 1.5rem;
  aspect-ratio: 1;
  border: 2px solid var(--gold);
  background: #1b110d;
}

.legend-swatch.character {
  border-radius: 50%;
}

.legend-swatch.weapon {
  border-radius: 35% 12% 35% 12%;
}

.legend-swatch.spell {
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(215, 182, 106, 0.75);
}

.legend-swatch.psionic {
  border-radius: 50% 8px;
}

.legend-swatch.divine {
  border-radius: 50%;
  outline: 1px solid rgba(255, 255, 255, 0.22);
  outline-offset: 3px;
}

.legend-swatch.skill {
  border-radius: 4px;
  transform: rotate(45deg);
}

.legend-swatch.misc {
  border-radius: 6px;
}

.token-dialog {
  width: min(92vw, 430px);
  border-radius: 8px;
  color: var(--parchment);
}

.token-dialog::backdrop {
  background: rgba(0, 0, 0, 0.72);
}

.dialog-head,
.dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
}

.dialog-head button {
  width: 2rem;
  min-height: 2rem;
  padding: 0;
  font-family: Arial, sans-serif;
}

.dialog-actions {
  justify-content: end;
  flex-wrap: wrap;
}

.is-hidden {
  display: none;
}

@media (max-width: 980px) {
  .app-shell {
    padding: 0.65rem;
  }

  .panel-toggle {
    display: inline-grid;
    place-items: center;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .control-panel {
    max-height: none;
  }

  .control-panel.collapsed {
    display: none;
  }

  .phase-strip button {
    flex: 1 1 9rem;
  }
}

@media (max-width: 640px) {
  .topbar {
    align-items: end;
    flex-wrap: wrap;
  }

  .combat-state {
    order: 3;
    width: 100%;
    margin-left: 0;
    border-radius: 8px;
  }

  .combat-state select {
    min-width: 0;
    flex: 1;
  }

  .phase-strip {
    align-items: stretch;
  }

  .phase-strip label,
  .current-sr {
    width: auto;
    flex: 1 1 7rem;
  }

  .token {
    width: clamp(3.5rem, 13vw, 4.25rem);
    min-height: clamp(3.5rem, 13vw, 4.25rem);
  }

  .token-label,
  .token-owner {
    max-width: 3.7rem;
  }
}
