:root {
  --bg: #151617;
  --panel: #303235;
  --grid-line: rgba(255, 255, 255, 0.1);
  --blue: #0088ff;
  --cell-size: 15px;
  --sensor-size: 22.5px;
  --sensor-icon-size: 14px;
  --indicator-size: 22.5px;
  --indicator-icon-size: 11.25px;
  --swatch-size: 20px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
  touch-action: pan-y;
}

body {
  background: var(--bg);
  font-family: system-ui, sans-serif;
  color: #e9eef2;
}

.container {
  width: 100%;
  min-height: 100dvh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12px;
}

.stack {
  display: flex;
  flex-direction: column;
  width: min(92vw, 240px);
}

.presence-chart {
  width: 100%;
  aspect-ratio: 8 / 9;
  background: var(--panel);
  border-radius: 10px;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  overflow: hidden;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

.draw-tools {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}

.draw-tools .btn {
  flex: 1 1 0;
  min-width: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.draw-tools .mdi {
  font-size: 16px;
  line-height: 1;
}

.edit-palette-wrap {
  margin-top: 8px;
}

.controls .btn {
  flex: 1 1 0;
  min-width: 140px;
}

.zone-tools {
  margin-top: 12px;
  background: rgba(0, 0, 0, 0.18);
  border-radius: 10px;
  padding: 10px;
}

.tool-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-bottom: 8px;
  color: #cbd5df;
}

.tool-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #e9eef2;
  background: linear-gradient(180deg, #3b3d41 0%, #2b2d31 100%);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.08),
    0 6px 14px rgba(0, 0, 0, 0.35),
    inset 0 0 0 1px rgba(255, 255, 255, 0.05);
  cursor: pointer;
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease,
    background 0.2s ease;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.1),
    0 10px 18px rgba(0, 0, 0, 0.4),
    inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.btn:active {
  transform: translateY(0);
}

.btn.secondary {
  color: #cbd5df;
  background: linear-gradient(180deg, #34363a 0%, #26282c 100%);
}

.btn.energetic {
  color: #fff4e8;
  background: linear-gradient(180deg, #ff8b2b 0%, #d55c00 100%);
}

.btn.compact {
  padding: 8px 11px;
  font-size: 12px;
}

.btn.active {
  box-shadow:
    0 1px 0 rgba(255, 255, 255, 0.1),
    0 10px 18px rgba(0, 0, 0, 0.4),
    inset 0 0 0 1px rgba(0, 177, 255, 0.8);
  color: #ffffff;
}

.zone-list {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.zone-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  padding: 8px;
}

.zone-row-info {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.zone-row-marker {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--zone-item-color, #fff);
}

.zone-row-marker.mdi {
  width: auto;
  height: auto;
  color: rgba(255, 255, 255, 0.7);
  font-size: 16px;
  line-height: 1;
}

.zone-row-title {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.zone-count-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 18px;
  padding: 0 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  color: #e6edf4;
  font-size: 11px;
  line-height: 1;
}

.zone-count-tag .mdi {
  font-size: 12px;
  line-height: 1;
}

.zone-row-actions {
  display: inline-flex;
  gap: 6px;
}

.zone-row-actions .btn {
  min-height: 30px;
  padding: 6px 8px;
}

.icon-btn {
  min-width: 34px;
  width: 34px;
  height: 34px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn .mdi {
  font-size: 16px;
  line-height: 1;
}

.zone-empty {
  border: 1px dashed rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  padding: 10px;
  color: #aeb8c4;
  font-size: 12px;
}

.palette {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding-top: 8px;
  padding-bottom: 2px;
}

.swatch {
  width: var(--swatch-size);
  min-width: var(--swatch-size);
  height: var(--swatch-size);
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: var(--swatch-color);
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}

.swatch.active {
  outline: 2px solid #ffffff;
  outline-offset: 1px;
}

.zone-editor-view {
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  padding: 10px;
}

.zone-editor-meta {
  font-size: 12px;
  font-weight: 600;
  color: #dbe4ed;
  margin-bottom: 8px;
}

.field-label {
  display: block;
  font-size: 11px;
  color: #c0c9d3;
  margin-bottom: 6px;
}

.text-input {
  width: 100%;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.22);
  color: #ffffff;
  padding: 0 10px;
  font-size: 13px;
  margin-bottom: 10px;
}

.text-input:focus {
  outline: none;
  border-color: rgba(0, 177, 255, 0.85);
  box-shadow: 0 0 0 1px rgba(0, 177, 255, 0.35);
}

.tool-hint {
  margin-top: 8px;
  font-size: 11px;
  color: #b8c5d2;
}

.grid-wrap {
  width: 100%;
  aspect-ratio: 1 / 1;
  position: relative;
  overflow: visible;
}

.grid {
  width: 100%;
  height: 100%;
  position: relative;
  isolation: isolate;
  overflow: hidden;
  user-select: none;
  touch-action: pan-y;
  cursor: crosshair;
}

body.zone-editing .grid {
  touch-action: none;
}

.grid::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(
      to right,
      transparent calc(100% - 1px),
      var(--grid-line) calc(100% - 1px),
      var(--grid-line) 100%
    ),
    linear-gradient(
      to bottom,
      transparent calc(100% - 1px),
      var(--grid-line) calc(100% - 1px),
      var(--grid-line) 100%
    );
  background-size: var(--cell-size) var(--cell-size);
  background-position: 0 0;
}

.zones-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

.no-detect-layer {
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
}

.zone-cell {
  position: absolute;
  border: 1px solid transparent;
  box-sizing: border-box;
}

.no-detect-cell {
  background: rgba(93, 93, 93, 0.7);
  border: 1px dashed #c6c6c6;
  backdrop-filter: blur(2px);
  border-radius: 4px;
}

.no-detect-icon {
  position: absolute;
  transform: translate(-50%, -50%);
  color: rgba(255, 255, 255, 0.5);
  font-size: var(--sensor-icon-size);
  line-height: 1;
}

.deadzone {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.25);
  clip-path: polygon(50% 0%, 100% 28%, 100% 0%, 0% 0%, 0% 28%);
  z-index: 1;
  pointer-events: none;
}

.draw-preview {
  position: absolute;
  border: 1px dashed rgba(255, 255, 255, 0.9);
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  pointer-events: none;
  z-index: 9;
  display: none;
}

.draw-preview.erase {
  border-color: rgba(255, 114, 0, 0.95);
  background: rgba(255, 114, 0, 0.14);
}

.draw-preview.no-detect {
  border-color: #c6c6c6;
  border-style: dashed;
  background: rgba(93, 93, 93, 0.35);
}

.sensor-node {
  position: absolute;
  width: var(--sensor-size);
  height: var(--sensor-size);
  top: calc(var(--sensor-size) * -0.5);
  left: 50%;
  transform: translateX(-50%);
  background: radial-gradient(
      80% 80% at 50% 20%,
      rgba(0, 0, 0, 0) 0%,
      rgba(0, 0, 0, 0.2) 100%
    ),
    var(--blue);
  box-shadow:
    0px 4px 4px rgba(0, 0, 0, 0.25),
    inset 0px -1px 0px rgba(255, 255, 255, 0.25);
  border-radius: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  z-index: 5;
}

.sensor-node .mdi {
  font-size: var(--sensor-icon-size);
  line-height: 1;
}

.walker {
  position: absolute;
  width: var(--indicator-size);
  height: var(--indicator-size);
  border-radius: 50%;
  border: 1px solid var(--panel);
  background: var(--walker-color);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  transform: translate(-50%, -50%);
  z-index: 4;
  pointer-events: none;
  opacity: 1;
  transition-property: left, top, opacity;
  transition-duration:
    var(--walker-step-duration, 0.9s),
    var(--walker-step-duration, 0.9s),
    1.6s;
  transition-timing-function: ease-in-out, ease-in-out, ease-out;
}

.walker .mdi {
  font-size: var(--indicator-icon-size);
  line-height: 1;
}

.walker.walker-facing-left .mdi {
  transform: scaleX(-1);
}

.walker-faded {
  opacity: 0;
}

.walker-energetic {
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.35),
    0 0 14px rgba(255, 114, 0, 0.32);
}

.walker-trail {
  z-index: 3;
  opacity: 0.45;
  width: var(--indicator-size);
  height: var(--indicator-size);
  border: 0;
  filter: saturate(0.95);
  transition: none;
  animation: walker-trail-echo 720ms ease-out forwards;
  box-shadow: none;
}

.walker-trail .mdi {
  display: none;
}

@keyframes walker-trail-echo {
  0% {
    transform: translate(-50%, -50%);
  }
  35% {
    transform: translate(-50%, -50%);
  }
  100% {
    transform: translate(-50%, -50%);
    opacity: 0;
  }
}

@media (max-width: 640px) {
  .container {
    padding: 10px;
  }

  .stack {
    width: 100%;
  }

  .btn {
    min-height: 44px;
    font-size: 14px;
  }

  .btn.compact {
    min-height: 40px;
  }

  .zone-tools {
    padding: 12px;
  }
}
