* {
  box-sizing: border-box;
}

:root {
  --paper: #d6d2c4;
  --panel: #f0efe9;
  --panel-dark: #5b5848;
  --line: #a8a495;
  --line-dark: #7a7666;
  --ink: #222222;
  --field-border: #8a8676;
  --field-bg: #ffffff;
  --btn-bg: #d8d5c8;
  --btn-bg-hover: #e2dfd2;
  --danger: #8a4a4a;
  --danger-hover: #a05555;
  --dim: #6b6858;

  /* driven live by the border-thickness input */
  --grid-line-width: 1px;
  /* driven live by the border-color input */
  --grid-line-color: #7a7666;
}

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font: 12px/1.5 Verdana, Geneva, Tahoma, sans-serif;
}

label {
  color: var(--dim);
  font-size: 11px;
  margin-right: 2px;
}

button {
  font: inherit;
  font-size: 11px;
  color: var(--ink);
  background: var(--btn-bg);
  border: 1px solid var(--field-border);
  border-top-color: #ece9dd;
  border-left-color: #ece9dd;
  border-radius: 0;
  padding: 4px 9px;
  cursor: pointer;
}

button:hover {
  background: var(--btn-bg-hover);
}

button:active {
  border-top-color: var(--field-border);
  border-left-color: var(--field-border);
  border-bottom-color: #ece9dd;
  border-right-color: #ece9dd;
}

button.danger {
  background: var(--danger);
  color: #fff;
  border-color: #632f2f;
}

button.danger:hover {
  background: var(--danger-hover);
}

input[type="text"],
input[type="number"] {
  font: inherit;
  font-size: 11px;
  color: var(--ink);
  background: var(--field-bg);
  border: 1px solid var(--field-border);
  padding: 3px 5px;
  outline: none;
}

input[type="number"] {
  width: 46px;
  text-align: center;
}

input#url,
input#bg-url {
  width: 200px;
}

input#hex {
  width: 64px;
}

input:focus {
  border-color: var(--ink);
}

input#hex.bad {
  border-color: #b02a2a;
  color: #b02a2a;
}

/* ============ titlebar ============ */
#titlebar {
  background: var(--panel-dark);
  color: var(--panel);
  padding: 8px 14px;
  font-weight: bold;
  letter-spacing: 0.5px;
  border-bottom: 2px solid var(--line-dark);
}

/* ============ control bar ============ */
#bar {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

#bar .grp {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
}

#bar .sep {
  width: 1px;
  align-self: stretch;
  background: var(--line);
}

/* ============ info readout ============ */
#info {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 4px 12px;
  color: var(--dim);
  font-size: 11px;
}

/* ============ stage ============ */
#stage {
  position: relative;
  overflow: auto;
  padding: 20px;
  height: calc(100vh - 47px - 39px - 25px);
  background: #cfccbe;
}

#bg-layer {
  position: absolute;
  top: 20px;
  left: 20px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  pointer-events: none;
}

#grid {
  position: relative;
  box-sizing: content-box; /* border must ADD to the set width/height, not eat into it */
  background-color: rgba(255, 255, 255, 0.55);
  background-image:
    linear-gradient(to right, var(--grid-line-color) var(--grid-line-width), transparent var(--grid-line-width)),
    linear-gradient(to bottom, var(--grid-line-color) var(--grid-line-width), transparent var(--grid-line-width));
  background-repeat: repeat;
  border-right: var(--grid-line-width) solid var(--grid-line-color);
  border-bottom: var(--grid-line-width) solid var(--grid-line-color);
  border-top: var(--grid-line-width) solid var(--grid-line-color);
  border-left: var(--grid-line-width) solid var(--grid-line-color);
}

#tokens {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 0;
  height: 0;
}

/* ============ tokens (images + player circles) ============ */
.token {
  position: absolute;
  transform: translate(-50%, -100%); /* anchor = bottom-center, the "feet" */
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.token.dragging {
  cursor: grabbing;
  filter: drop-shadow(0 4px 6px rgba(0,0,0,0.4));
  z-index: 50;
}

.token img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  border: 1px solid var(--line-dark);
  background: #fff;
}

.token .circle {
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.35);
  pointer-events: none;
}

.token .foot {
  position: absolute;
  bottom: -1px;
  left: 50%;
  transform: translateX(-50%);
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--ink);
  opacity: 0;
}

.token:hover .foot,
.token.dragging .foot {
  opacity: 0.45;
}

.token .del {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  font-size: 9px;
  line-height: 14px;
  text-align: center;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.1s;
}

.token:hover .del {
  opacity: 1;
}

/* ============ resize: selected token + gizmo handle ============ */
.token.selected-resize {
  outline: 1px dashed var(--panel-dark);
  outline-offset: 3px;
}

.token .gizmo {
  position: absolute;
  bottom: -6px;
  right: -6px;
  width: 12px;
  height: 12px;
  background: var(--panel-dark);
  border: 1px solid var(--panel);
  cursor: nwse-resize;
  touch-action: none;
}

.token:not(.selected-resize) .gizmo {
  display: none;
}

/* ============ player view: three visibility tiers ============ */
/* tier "full" (viewer's own square + orthogonal ring 1): default appearance, no change */

/* tier "ghost" (ring 2): fill hidden, black outline remains, so presence is
   knowable without full identification */
.token.pv-ghost .circle {
  background: transparent !important;
  box-shadow: inset 0 0 0 2px #000000;
}

.token.pv-ghost .del,
.token.pv-ghost .gizmo {
  display: none;
}

/* tier "hidden" (everything else): fully removed from view and interaction */
.token.pv-hidden {
  display: none;
}

.token.pv-viewer .circle {
  box-shadow: inset 0 0 0 2px #000000, inset 0 0 0 4px #ffffff;
}

#player-view.active {
  background: var(--panel-dark);
  color: var(--panel);
  border-color: var(--line-dark);
}