:root {
  /* Themeable colors (dark defaults) */
  --page-bg: #111;
  --page-fg: #eee;
  --ruler-bg: rgba(20, 20, 20, 0.5);
  --ruler-fg: #e4e4e4;
  --ruler-tick: #bdbdbd;
  --ruler-border: #333;
  --grid-blend-mode: overlay; /* blend effect for grid lines */

  /* Grid and UI */
  --grid-color: rgba(0, 0, 0, 0.6);
  --ruler-height: 28px;
  --ruler-width: 40px;
  --toast-bg: rgba(0,0,0,0.8);
  --toast-fg: white;
}

@media (prefers-color-scheme: light) {
  :root {
    --page-bg: #f6f6f6;
    --page-fg: #111;
    --ruler-bg: rgba(245, 245, 245, 0.9);
    --ruler-fg: #222;
    --ruler-tick: #777;
    --ruler-border: #c8c8c8;
    --toast-bg: rgba(0,0,0,0.85);
    --toast-fg: white;
    /* grid color can stay; color-burn adapts to bg */
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html, body { margin: 0; padding: 0; overflow: hidden; background: var(--page-bg); color: var(--page-fg); }

.app { height: 100%; width: 100%; }

.viewport {
  position: fixed;
  inset: 0;
  overflow: hidden; /* page must not scroll */
  background: var(--page-bg);
  cursor: grab;
  touch-action: none; /* allow custom pinch/pan gestures */
  user-select: none;
  -webkit-user-select: none;
}

/* The stage contains the image and the grid overlay and is transformed */
.stage {
  position: absolute;
  top: 0; left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.map-image {
  position: absolute;
  top: 0; left: 0;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none; /* mouse events handled on viewport */
  z-index: 1;
}

.grid-canvas {
  position: absolute;
  inset: 0; /* cover the entire viewport */
  pointer-events: none;
  mix-blend-mode: var(--grid-blend-mode, color-burn); /* requested blend effect */
  opacity: 0.9;
  z-index: 2; /* above the image, below rulers */
}
.marker-canvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3; /* above grid, below rulers */
}

/* Rulers for edge labels */
.ruler {
  position: absolute;
  z-index: 5;
  image-rendering: pixelated;
}
.ruler-top {
  left: var(--ruler-width);
  right: 0;
  top: 0;
  height: var(--ruler-height);
}
.ruler-left {
  top: var(--ruler-height);
  bottom: 0;
  left: 0;
  width: var(--ruler-width);
}

/* Corner block to cover intersection of rulers */
.viewport::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: var(--ruler-width);
  height: var(--ruler-height);
  background: var(--ruler-bg);
  border-right: 1px solid var(--ruler-border);
  border-bottom: 1px solid var(--ruler-border);
  z-index: 6;
}

/* Toast for copy feedback */
.toast {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--toast-fg);
  padding: 8px 12px;
  border-radius: 6px;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 13px;
  opacity: 0;
  transition: opacity 150ms ease;
  pointer-events: none;
  z-index: 10;
}
.toast.show { opacity: 1; }

/* Cursor states */
.grabbing { cursor: grabbing; }
.grab { cursor: grab; }

/* Settings UI */
.settings-btn {
  position: absolute;
  top: 0; left: 0;
  width: var(--ruler-width);
  height: var(--ruler-height);
  z-index: 7; /* above corner block */
  border: none;
  background: transparent;
  color: var(--ruler-fg);
  font-size: 16px;
  cursor: pointer;
}
.settings-btn:focus-visible { outline: 2px solid #66a3ff; outline-offset: -2px; }

.settings-panel {
  position: absolute;
  top: calc(var(--ruler-height) + 6px);
  left: 6px;
  width: min(380px, calc(100% - 12px));
  max-height: none;
  background: var(--ruler-bg);
  color: var(--ruler-fg);
  border: 1px solid var(--ruler-border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 8;
  display: none;
  overflow: visible;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  font-size: 0.8rem; /* ~80% of system default */
  user-select: text; /* re-enable selection inside panel */
  -webkit-user-select: text;
}
.settings-panel.open { display: block; }

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--ruler-border);
}
.settings-header h2 { margin: 0; font-weight: 600; line-height: 1.2; }
.close-btn {
  border: none; background: transparent; color: var(--ruler-fg); cursor: pointer; font-size: 14px;
}
.settings-body { padding: 8px 10px 12px; }
.settings-grid { display: block; }
.setting-row { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 8px 12px; padding: 10px 0; border-top: 1px solid var(--ruler-border); }
.setting-row:first-child { border-top: none; }
.setting-row .name { color: var(--ruler-fg); opacity: 0.9; }
.setting-row .control { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.setting-row .control.control-vertical { flex-direction: column; align-items: flex-start; }
.reset-btn { margin-left: auto; border: 1px solid var(--ruler-border); background: transparent; color: var(--ruler-fg); padding: 4px 8px; border-radius: 6px; cursor: pointer; }

.settings-footer { display: flex; gap: 8px; justify-content: flex-end; padding: 10px; border-top: 1px solid var(--ruler-border); position: sticky; bottom: 0; background: var(--ruler-bg); }
.settings-footer .reset-btn { margin-left: 0; }

/* Bottom sheet for small screens */
@media (max-width: 640px), (max-height: 540px) {
  .settings-panel {
    left: 0; right: 0; width: auto;
    top: auto; bottom: 0;
    max-height: 70vh;
    border-radius: 10px 10px 0 0;
    margin: 0 0 env(safe-area-inset-bottom) 0;
    overflow: auto;
  }
}

/* Theme overrides via data-theme */
:root[data-theme="light"] {
  --page-bg: #f6f6f6;
  --page-fg: #111;
  --ruler-bg: rgba(245, 245, 245, 0.9);
  --ruler-fg: #222;
  --ruler-tick: #777;
  --ruler-border: #c8c8c8;
  --toast-bg: rgba(0,0,0,0.85);
  --toast-fg: white;
}
:root[data-theme="dark"] {
  --page-bg: #111;
  --page-fg: #eee;
  --ruler-bg: rgba(20, 20, 20, 0.5);
  --ruler-fg: #e4e4e4;
  --ruler-tick: #bdbdbd;
  --ruler-border: #333;
}
