:root {
  --bg: #0a0a0a;
  --pane-bg: #050505;
  --pane-border: #1f1f1f;
  --text: #e8e8e8;
  --muted: #888;
  --accent: #4a9eff;
  --accent-dim: #2d5d99;
  --handle-source: #ff4a4a;
  --handle-dest: #4aff8c;
  --handle-selected: #ffd24a;
  --handle-size: 18px;
  --handle-touch: 44px;
  --controls-w: 320px;
  --shadow: 0 8px 32px rgba(0,0,0,0.6);
}

* { box-sizing: border-box; }

/* The [hidden] attribute should always win over author display rules. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

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

button {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  color: var(--text);
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 80ms ease;
}
button:hover { background: #232323; }
button:active { background: #2a2a2a; }
button.primary { background: var(--accent-dim); border-color: var(--accent); }
button.primary:hover { background: var(--accent); }
button.block { display: block; width: 100%; margin-top: 6px; }
button.big { padding: 12px; font-size: 15px; font-weight: 600; }
button.icon-btn {
  width: 28px; height: 28px;
  padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; line-height: 1;
}

input[type=number], input[type=text], select {
  background: #111;
  border: 1px solid #2a2a2a;
  color: var(--text);
  padding: 6px 8px;
  border-radius: 4px;
  width: 70px;
}
input[type=range] { width: 100%; accent-color: var(--accent); }

.app {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  position: relative;
  overflow: hidden;
}

.pane {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--pane-bg);
}
.projection-pane { background: #000; }

/* Setup mode: source only. Project mode: projection only. */
.app[data-mode="setup"] .projection-pane { display: none !important; }
.app[data-mode="project"] .source-pane { display: none !important; }

.pane-label {
  position: absolute;
  top: 8px; left: 8px;
  font-size: 10px;
  letter-spacing: 0.15em;
  font-weight: 600;
  color: var(--muted);
  background: rgba(0,0,0,0.5);
  padding: 4px 8px;
  border-radius: 4px;
  z-index: 5;
  pointer-events: none;
}
.pane-empty {
  position: absolute;
  inset: 0;
  display: flex; align-items: center; justify-content: center;
  color: var(--muted);
  pointer-events: none;
}
.pane-empty[hidden] { display: none; }

/* Source pan/zoom */
.pan-viewport {
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: grab;
}
.pan-viewport:active { cursor: grabbing; }
.pan-content {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
}
.pan-content img {
  display: block;
  pointer-events: none;
  -webkit-user-drag: none;
  max-width: none;
}

/* Projection */
.projection-pane #projection-img {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
  pointer-events: none;
  -webkit-user-drag: none;
  max-width: none;
}

/* Handles */
.handles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.handle {
  position: absolute;
  width: var(--handle-touch);
  height: var(--handle-touch);
  margin-left: calc(var(--handle-touch) / -2);
  margin-top: calc(var(--handle-touch) / -2);
  pointer-events: auto;
  display: flex; align-items: center; justify-content: center;
  cursor: grab;
  touch-action: none;
}
.handle:active { cursor: grabbing; }
.handle .dot {
  width: var(--handle-size);
  height: var(--handle-size);
  border-radius: 50%;
  border: 2px solid #000;
  box-shadow: 0 0 0 1px currentColor, 0 2px 6px rgba(0,0,0,0.6);
  background: currentColor;
  transition: transform 60ms ease;
}
.handle.source .dot { color: var(--handle-source); }
.handle.dest .dot { color: var(--handle-dest); }
.handle.selected .dot {
  color: var(--handle-selected);
  transform: scale(1.3);
}
.handle .label {
  position: absolute;
  bottom: -4px;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  text-shadow: 0 0 4px #000, 0 0 4px #000;
  pointer-events: none;
}

/* Quad outline */
.handles svg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
}
.handles svg polygon {
  fill: none;
  stroke: currentColor;
  stroke-width: 1;
  stroke-dasharray: 4 4;
  opacity: 0.6;
}
#source-handles svg polygon { color: var(--handle-source); }
#dest-handles svg polygon { color: var(--handle-dest); }

.pane-swap { display: none !important; }

/* Controls */
.controls {
  position: absolute;
  top: 12px;
  right: 12px;
  width: var(--controls-w);
  max-height: calc(100% - 24px);
  background: rgba(15,15,15,0.95);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid #2a2a2a;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 40;
  display: flex;
  flex-direction: column;
}
.controls-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid #1f1f1f;
}
.controls-header .title { font-weight: 600; font-size: 13px; }
.controls-body {
  overflow-y: auto;
  padding: 8px 12px 12px;
}
.controls.collapsed .controls-body { display: none; }
.controls.collapsed .controls-header { border-bottom: none; }

.ctrl-section {
  padding: 10px 0;
  border-bottom: 1px solid #1a1a1a;
}
.ctrl-section:last-child { border-bottom: none; }
.ctrl-section h3 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  font-weight: 600;
}
.dim-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.dim-row label { display: flex; gap: 4px; align-items: center; font-size: 12px; color: var(--muted); }
.dim-row select { width: 60px; }

.row { display: flex; gap: 6px; }
.row > * { flex: 1; }

.check { display: flex; align-items: center; gap: 6px; cursor: pointer; padding: 4px 0; }
.check input { accent-color: var(--accent); }
.slider { display: flex; flex-direction: column; gap: 4px; padding: 4px 0; font-size: 12px; color: var(--muted); }
.slider span { font-variant-numeric: tabular-nums; color: var(--text); align-self: flex-end; }

.muted { color: var(--muted); }
.small { font-size: 11px; }
.file-info { margin-top: 6px; }
.pdf-controls { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.pdf-controls label { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.pdf-controls input { width: 60px; }

.actions { padding-top: 10px; }

.controls-restore {
  position: absolute;
  top: 12px; right: 12px;
  z-index: 40;
  width: 40px; height: 40px;
  font-size: 18px;
  border-radius: 8px;
  background: rgba(15,15,15,0.95);
  backdrop-filter: blur(12px);
}

.project-exit {
  position: absolute;
  bottom: 12px; right: 12px;
  z-index: 50;
  width: 44px; height: 44px;
  font-size: 18px;
  border-radius: 50%;
  background: rgba(20,20,20,0.85);
  backdrop-filter: blur(12px);
  display: none;
}

/* Projection-mode toolbar */
.proj-toolbar {
  position: absolute;
  bottom: 12px; left: 12px;
  z-index: 50;
  display: none;
  gap: 8px;
  background: rgba(20,20,20,0.85);
  backdrop-filter: blur(12px);
  border: 1px solid #2a2a2a;
  padding: 6px;
  border-radius: 10px;
}
.app[data-mode="project"] .proj-toolbar { display: flex; }
.app[data-mode="project"] .proj-toolbar.hidden { display: none; }
.app[data-mode="project"] .proj-toolbar-show { display: flex; }
.proj-toolbar-show {
  position: absolute;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 12px;
  z-index: 50;
  display: none;
  width: 44px;
  height: 44px;
  font-size: 18px;
  border-radius: 22px;
  background: rgba(20,20,20,0.85);
  backdrop-filter: blur(12px);
  align-items: center;
  justify-content: center;
}
.proj-toolbar .tb-group {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 0 4px;
  border-right: 1px solid #2a2a2a;
}
.proj-toolbar .tb-group:last-child { border-right: none; }
.proj-toolbar .icon-btn {
  width: 36px;
  height: 36px;
  font-size: 14px;
  background: rgba(255,255,255,0.06);
}
.proj-toolbar .icon-btn:hover { background: rgba(255,255,255,0.12); }
.proj-toolbar .nudge { display: flex; align-items: center; gap: 6px; }
.proj-toolbar .nudge-pad {
  display: grid;
  grid-template-columns: repeat(3, 28px);
  grid-template-rows: repeat(2, 28px);
  gap: 2px;
}
.proj-toolbar .nudge-pad .icon-btn {
  width: 28px;
  height: 28px;
  font-size: 11px;
}
/* Layout the 4 nudge buttons in a + pattern */
#proj-nudge-up { grid-column: 2; grid-row: 1; }
#proj-nudge-left { grid-column: 1; grid-row: 2; }
#proj-nudge-right { grid-column: 3; grid-row: 2; }
#proj-nudge-down { grid-column: 2; grid-row: 2; }
.proj-toolbar #proj-nudge-step {
  width: 44px;
  font-variant-numeric: tabular-nums;
}

/* Projection viewport (zoom container) */
.proj-viewport {
  position: absolute;
  inset: 0;
  transform-origin: 0 0;
  will-change: transform;
}

/* Anchor 3x3 grid */
.anchor-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin-top: 4px;
}
.anchor-grid button {
  aspect-ratio: 1;
  padding: 0;
  font-size: 14px;
  background: #1a1a1a;
}
.anchor-grid button.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: white;
}

/* Drag-drop overlay */
#drag-overlay {
  position: fixed;
  inset: 16px;
  border: 4px dashed var(--accent);
  border-radius: 16px;
  background: rgba(74, 158, 255, 0.12);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 600;
  letter-spacing: 0.05em;
  z-index: 9999;
  pointer-events: none;
}

/* Paste-setup dialog */
dialog#paste-dialog {
  background: #111;
  color: var(--text);
  border: 1px solid #2a2a2a;
  border-radius: 10px;
  padding: 16px;
  max-width: 90vw;
  width: 480px;
}
dialog#paste-dialog::backdrop { background: rgba(0,0,0,0.6); }
dialog#paste-dialog h3 { margin: 0 0 8px; font-size: 14px; }
dialog#paste-dialog textarea {
  width: 100%;
  background: #0a0a0a;
  border: 1px solid #2a2a2a;
  color: var(--text);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  padding: 8px;
  border-radius: 6px;
  resize: vertical;
}

/* Danger button */
button.danger {
  background: #2a1010;
  border-color: #4a1818;
  color: #ff8080;
}
button.danger:hover { background: #3a1414; }

.toast {
  position: absolute;
  bottom: 20px; left: 50%;
  transform: translateX(-50%);
  background: rgba(20,20,20,0.95);
  border: 1px solid #2a2a2a;
  padding: 10px 16px;
  border-radius: 6px;
  font-size: 13px;
  z-index: 60;
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms;
}
.toast.show { opacity: 1; }

/* PROJECT MODE */
.app[data-mode="project"] {
  background: #000;
}
.app[data-mode="project"] .controls,
.app[data-mode="project"] .controls-restore { display: none !important; }
.app[data-mode="project"] .project-exit { display: flex; align-items: center; justify-content: center; }

/* Handles visibility (sticky preference) */
.app[data-show-handles="false"] .handles { display: none; }

/* Narrow viewports just shrink controls */
@media (max-width: 900px) {
  :root { --controls-w: 280px; }
}

/* Phone-sized screens: rework controls + toolbar so everything fits + is tappable */
@media (max-width: 600px) {
  /* Controls panel: narrower so source image is visible behind it */
  .controls {
    width: min(85vw, 300px);
    max-height: 80vh;
  }
  /* Bigger collapse button so it's findable / tappable */
  .controls-header { padding: 12px; }
  .controls-header .icon-btn {
    width: 44px;
    height: 44px;
    font-size: 24px;
    background: rgba(255,255,255,0.08);
  }
  .controls-restore {
    width: 48px;
    height: 48px;
    font-size: 22px;
  }

  /* Project toolbar: wrap groups onto multiple rows, keep on-screen */
  .proj-toolbar {
    bottom: max(12px, env(safe-area-inset-bottom));
    left: 12px;
    right: 12px;
    flex-wrap: wrap;
    justify-content: center;
    max-width: calc(100vw - 24px);
    gap: 6px;
    padding: 6px;
  }
  .proj-toolbar .tb-group {
    border-right: none;
    background: rgba(255,255,255,0.04);
    border-radius: 8px;
    padding: 4px;
  }

  /* Project-exit moves to top-right so it doesn't fight the toolbar */
  .app[data-mode="project"] .project-exit {
    top: max(12px, env(safe-area-inset-top));
    right: 12px;
    bottom: auto;
  }
}
