* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  background: #101010;
  color: #f2f2f2;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
}

body {
  overscroll-behavior: none;
}

h1 {
  font-size: 1.05rem;
  margin: 0 0 3px;
}

p {
  color: #bbb;
  margin: 0;
  line-height: 1.3;
  font-size: 0.82rem;
}

.wrap {
  min-height: 100vh;
}

.canvasShell {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #fff;
  touch-action: none;
}

.canvasShell::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at center, transparent 58%, rgba(16, 16, 16, 0.18) 100%),
    linear-gradient(135deg, rgba(125, 211, 252, 0.08), transparent 34%, rgba(216, 180, 254, 0.08));
  transition: background 260ms ease, opacity 260ms ease;
}

.canvasShell.running::after {
  background:
    radial-gradient(circle at center, transparent 56%, rgba(16, 16, 16, 0.14) 100%),
    linear-gradient(135deg, rgba(125, 211, 252, 0.11), transparent 32%, rgba(216, 180, 254, 0.13));
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #fff;
  image-rendering: pixelated;
  touch-action: none;
  cursor: crosshair;
}

.canvasShell.eraseModeActive canvas {
  cursor: cell;
}

.canvasShell.panningActive canvas {
  cursor: grabbing;
}

.zoomControls {
  position: fixed;
  left: max(12px, env(safe-area-inset-left));
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  display: grid;
  gap: 8px;
}

.zoomControls button {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  font-size: 1.45rem;
  line-height: 1;
  background: rgba(16, 16, 16, 0.76);
  border-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
}

#frameView {
  font-size: 1.12rem;
}

.feedback {
  position: fixed;
  left: 50%;
  top: max(14px, env(safe-area-inset-top));
  z-index: 2;
  transform: translate(-50%, -8px);
  min-width: 92px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(16, 16, 16, 0.74);
  border: 1px solid rgba(255, 255, 255, 0.16);
  color: #f2f2f2;
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  transition: opacity 160ms ease, transform 160ms ease;
}

.feedback.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.ui {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 1;
  width: min(360px, calc(100vw - 24px));
  display: grid;
  gap: 8px;
  pointer-events: none;
}

.titleBar,
.buttons,
.panel {
  pointer-events: auto;
}

.titleBar {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(16, 16, 16, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}

.buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 6px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 13px;
  background: rgba(16, 16, 16, 0.62);
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
}

button {
  appearance: none;
  border: 1px solid #555;
  background: rgba(42, 42, 42, 0.92);
  color: #f2f2f2;
  padding: 9px 7px;
  border-radius: 9px;
  font-weight: 700;
  font-size: 0.84rem;
  transition: background-color 140ms ease, border-color 140ms ease, transform 120ms ease;
}

button:hover {
  background: rgba(56, 56, 56, 0.94);
  border-color: #888;
}

button.primary:hover {
  background: #fff;
  border-color: #fff;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

button:disabled:hover {
  border-color: #555;
}

button:active {
  transform: translateY(1px);
}

button.primary {
  background: #eee;
  color: #111;
  border-color: #eee;
}

button.dangerPending {
  background: rgba(153, 27, 27, 0.94);
  border-color: rgba(252, 165, 165, 0.9);
  color: #fff;
}

button.dangerPending:hover {
  background: rgba(185, 28, 28, 0.96);
  border-color: rgba(254, 202, 202, 0.95);
}

button[aria-pressed="true"] {
  box-shadow: 0 0 0 2px rgba(125, 211, 252, 0.36), 0 8px 24px rgba(0, 0, 0, 0.24);
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid #7dd3fc;
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

.panel {
  width: 100%;
  max-width: 360px;
  max-height: min(58vh, 500px);
  overflow: auto;
  background: rgba(27, 27, 27, 0.9);
  border: 1px solid #333;
  border-radius: 12px;
  padding: 10px;
  backdrop-filter: blur(6px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  transition: max-height 180ms ease, background-color 180ms ease;
}

.panelToggle {
  width: 100%;
  margin-bottom: 8px;
  padding: 8px;
  background: rgba(238, 238, 238, 0.92);
  color: #111;
  border-color: #eee;
}

.panel.collapsed {
  max-height: none;
  overflow: hidden;
  background: rgba(16, 16, 16, 0.72);
  border-color: rgba(255, 255, 255, 0.12);
}

.panel.collapsed .stats {
  margin-bottom: 0;
  padding: 2px 0;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.panel.collapsed .stats div {
  padding: 4px 6px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
}

.panel.collapsed .controlsGrid {
  display: none;
}

.panel.collapsed .hint {
  display: none;
}

.controlsGrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 10px;
}

.control {
  margin: 0;
}

label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: #e7e7e7;
  font-size: 0.78rem;
  margin-bottom: 3px;
}

label span:last-child {
  color: #aaa;
  font-variant-numeric: tabular-nums;
}

.trendRising {
  color: #86efac;
}

.trendFalling {
  color: #fca5a5;
}

.trendSteady {
  color: #e5e7eb;
}

input[type="range"],
select {
  width: 100%;
  min-height: 32px;
  accent-color: #f2f2f2;
}

input[type="range"] {
  cursor: ew-resize;
}

select {
  appearance: none;
  background: rgba(42, 42, 42, 0.92);
  border: 1px solid #555;
  border-radius: 8px;
  color: #f2f2f2;
  padding: 4px 8px;
}

.toggleControl {
  display: flex;
  flex-direction: column;
  justify-content: end;
}

.toggleControl input[type="checkbox"] {
  width: 24px;
  height: 24px;
  margin: 0;
  accent-color: #f2f2f2;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  color: #bbb;
  font-size: 0.78rem;
  margin: 0 0 8px;
}

.hint {
  font-size: 0.76rem;
  color: #999;
  margin-top: 8px;
}

@media (max-width: 640px) {
  .zoomControls {
    left: max(8px, env(safe-area-inset-left));
    top: 42%;
  }

  .zoomControls button {
    width: 42px;
    height: 42px;
    font-size: 1.35rem;
  }

  #resetView {
    font-size: 1.05rem;
  }

  .ui {
    top: auto;
    right: max(8px, env(safe-area-inset-right));
    bottom: max(8px, env(safe-area-inset-bottom));
    left: max(8px, env(safe-area-inset-left));
    width: auto;
  }

  .titleBar {
    display: none;
  }

  .buttons {
    grid-template-columns: repeat(4, 1fr);
  }

  button {
    padding: 8px 4px;
    font-size: 0.75rem;
  }

  .panel {
    max-width: none;
    max-height: 34vh;
  }

  .feedback {
    top: max(8px, env(safe-area-inset-top));
  }
}

@media (pointer: coarse) {
  .zoomControls button {
    width: 48px;
    height: 48px;
  }

  button {
    min-height: 44px;
  }
}
