*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--fg);
}

:root,
body[data-theme='light'] {
  --bg: #f3f3f1;
  --fg: #111;
  --hud-bg: rgba(255, 255, 255, 0.92);
  --hud-border: #ddd;
  --tile-bg: #fff;
  --tile-border: #ccc;
  --tile-fg: #111;
  --armed: #7c3aed;
  --dev: #ea580c;
  --done: #2563eb;
  --done-fill: #dbeafe;
  --btn-bg: #f5f5f5;
  --btn-border: #bbb;
  --mm-bg: #e8e8e8;
  --mm-border: #bbb;
  --tip-bg: rgba(255, 255, 255, 0.96);
  --tip-border: #ccc;
  --shadow: rgba(0, 0, 0, 0.18);
}

body[data-theme='dark'] {
  --bg: #121212;
  --fg: #eee;
  --hud-bg: rgba(20, 20, 20, 0.92);
  --hud-border: #333;
  --tile-bg: #2a2a2a;
  --tile-border: #555;
  --tile-fg: #eee;
  --armed: #a78bfa;
  --dev: #fb923c;
  --done: #3b82f6;
  --done-fill: #1e3a5f;
  --btn-bg: #2a2a2a;
  --btn-border: #555;
  --mm-bg: #111;
  --mm-border: #444;
  --tip-bg: rgba(20, 20, 20, 0.96);
  --tip-border: #555;
  --shadow: rgba(0, 0, 0, 0.4);
}

#hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 40;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 14px;
  background: var(--hud-bg);
  border-bottom: 1px solid var(--hud-border);
  backdrop-filter: blur(6px);
  user-select: none;
}

.armed-readout {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.armed-readout.is-idle {
  font-weight: 400;
  opacity: 0.55;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.stats {
  font-size: 13px;
  white-space: nowrap;
}

.stats--short,
.btn-label--short {
  display: none;
}

.btn {
  font-family: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--btn-border);
  border-radius: 4px;
  background: var(--btn-bg);
  color: var(--fg);
  cursor: pointer;
}

.btn--quiet {
  font-size: 12px;
  padding: 4px 8px;
  background: transparent;
  opacity: 0.75;
}

.btn--icon {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  font-weight: 600;
  line-height: 1;
}

.btn:hover {
  opacity: 1;
}

#viewport {
  position: fixed;
  inset: 48px 0 0 0;
  overflow: hidden;
  cursor: default;
  touch-action: none;
}

#board {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.tile {
  position: absolute;
  width: 140px;
  margin: 0;
  padding: 12px;
  border: 1.5px solid var(--tile-border);
  border-radius: 2px;
  background: var(--tile-bg);
  color: var(--tile-fg);
  font: inherit;
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  appearance: none;
  /* Short tiles top-align in taller row bands via absolute y = row top. */
  display: flex;
  align-items: center;
  justify-content: center;
}

.tile:focus-visible {
  outline: 2px solid var(--armed);
  outline-offset: 2px;
}

.tile__label {
  display: block;
  width: 100%;
  overflow: hidden;
  /* Wrap at spaces only — never split an unbroken token. */
  word-break: keep-all;
  overflow-wrap: normal;
  hyphens: none;
}

.tile--armed {
  border-color: var(--armed);
  border-width: 2.5px;
  outline: 1px solid var(--armed);
}

.tile--dev {
  border-color: var(--dev);
  border-width: 2.5px;
  outline: 1px solid var(--dev);
}

.tile--completed {
  background: var(--done-fill);
  border-color: var(--done);
  border-width: 2px;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.35),
    0 2px 0 rgba(0, 0, 0, 0.12),
    0 4px 10px rgba(37, 99, 235, 0.22);
  cursor: default;
  animation: done-in 280ms ease-out;
}

@keyframes done-in {
  from {
    transform: scale(0.96);
    box-shadow: none;
  }
  to {
    transform: scale(1);
  }
}

.tile--just-merged .tile__label {
  animation: merge-pop 180ms ease-out;
}

@keyframes merge-pop {
  from {
    opacity: 0.35;
    transform: scale(0.97);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

#minimap-wrap {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 30;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  pointer-events: auto;
}

#minimap {
  display: block;
  border: 1px solid var(--mm-border);
  border-radius: 4px;
  cursor: pointer;
  background: var(--mm-bg);
}

#minimap[hidden] {
  display: none;
}

#tooltip {
  position: fixed;
  z-index: 80;
  max-width: 320px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--tip-bg);
  color: var(--fg);
  border: 1px solid var(--tip-border);
  box-shadow: 0 8px 24px var(--shadow);
  font-size: 13px;
  line-height: 1.35;
  pointer-events: none;
  white-space: normal;
  word-break: break-word;
}

#tooltip[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.modal[hidden] {
  display: none;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
}

.modal__panel {
  position: relative;
  z-index: 1;
  width: min(640px, 100%);
  max-height: min(90vh, 640px);
  display: flex;
  flex-direction: column;
  background: var(--hud-bg);
  color: var(--fg);
  border: 1px solid var(--hud-border);
  border-radius: 6px;
  box-shadow: 0 16px 48px var(--shadow);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px 0;
  flex-shrink: 0;
}

.modal__header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.modal__body {
  padding: 12px 16px 8px;
  overflow: auto;
  font-size: 14px;
  line-height: 1.45;
}

.modal__body p {
  margin: 0 0 12px;
}

.modal__body h3 {
  margin: 14px 0 6px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.7;
}

.modal__body ul {
  margin: 0;
  padding-left: 1.15em;
}

.modal__body li {
  margin: 0 0 6px;
}

.modal__body li:last-child {
  margin-bottom: 0;
}

.modal__body kbd {
  font-family: inherit;
  font-size: 0.92em;
  padding: 1px 5px;
  border: 1px solid var(--btn-border);
  border-radius: 3px;
  background: var(--btn-bg);
}

.modal__footer {
  display: flex;
  justify-content: flex-end;
  padding: 8px 16px 14px;
  flex-shrink: 0;
}

body.is-panning #viewport {
  cursor: grabbing;
}

body.modal-open {
  overflow: hidden;
}

/* Narrow HUD: keep stats + New + help; drop readout / theme text load. */
@media (max-width: 720px) {
  #hud {
    gap: 10px;
    padding: 0 10px;
  }

  .armed-readout {
    display: none;
  }

  .hud-actions {
    flex: 1;
    justify-content: flex-end;
    gap: 8px;
  }

  .stats--full {
    display: none;
  }

  .stats--short {
    display: inline;
    margin-right: auto;
  }

  .btn-label--full {
    display: none;
  }

  .btn-label--short {
    display: inline;
  }

  .btn--theme {
    display: none;
  }
}

@media (max-width: 380px) {
  .stats--short {
    font-size: 12px;
  }

  .btn {
    padding: 6px 8px;
  }
}
