/* ─── RESET & ROOT ───────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ─── TOUCH ERGONOMICS ───────────────────────────────────────────── */
/* Remove the 300ms tap delay / double-tap zoom and tap highlight on UI
   chrome, while leaving scroll containers free to scroll. */
.menu-item, .menu-entry, .tool-btn, .mac-btn, .color-swatch,
.layer-row, .page-row, .layer-vis, .layer-lock,
.halftone-opt, .grad-type-btn, .pat-type-btn, .color-mode-btn,
.close-box, .size-options label, .project-entry, .screentone-item {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --black: #000;
  --white: #fff;
  --gray: #aaa;
  --light-gray: #ddd;
  --dark-gray: #555;
  --window-bg: #fff;
  --font: 'Press Start 2P', monospace;
  --font-size: 10px;
  --title-h: 20px;
}

body {
  font-family: var(--font);
  font-size: var(--font-size);
  background-color: #aaa;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4'%3E%3Crect width='2' height='2' fill='%23888'/%3E%3Crect x='2' y='2' width='2' height='2' fill='%23888'/%3E%3C/svg%3E");
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  color: var(--black);
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* ─── MAC WINDOW CHROME ─────────────────────────────────────────── */
.mac-window {
  border: 2px solid var(--black);
  background: var(--window-bg);
  box-shadow: 2px 2px 0 var(--black);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.mac-window .titlebar {
  background: repeating-linear-gradient(
    90deg,
    var(--black) 0px, var(--black) 1px,
    var(--white) 1px, var(--white) 2px
  );
  height: var(--title-h);
  min-height: var(--title-h);
  display: flex;
  align-items: center;
  padding: 0 4px;
  border-bottom: 2px solid var(--black);
  flex-shrink: 0;
}
.mac-window .titlebar .close-box {
  width: 13px; height: 13px;
  border: 2px solid var(--black);
  background: var(--white);
  flex-shrink: 0;
  cursor: pointer;
  margin-right: 6px;
}
.mac-window .titlebar .close-box:hover { background: var(--black); }
.mac-window .titlebar .title-text {
  flex: 1;
  text-align: center;
  font-family: var(--font);
  font-size: 9px;
  background: var(--white);
  padding: 2px 6px;
  border: 1px solid var(--black);
  margin: 2px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.window-content {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ─── MENU BAR ──────────────────────────────────────────────────── */
#menu-bar {
  background: var(--white);
  border-bottom: 2px solid var(--black);
  height: calc(24px + env(safe-area-inset-top));
  min-height: calc(24px + env(safe-area-inset-top));
  display: flex;
  align-items: center;
  padding: env(safe-area-inset-top) 8px 0;
  flex-shrink: 0;
  z-index: 1000;
}
#menu-bar .app-icon {
  font-size: 9px;
  padding: 4px 12px 4px 4px;
  border-right: 1px solid var(--black);
  margin-right: 4px;
}
.menu-item {
  position: relative;
  padding: 4px 10px;
  font-size: 9px;
  cursor: pointer;
}
.menu-item:hover, .menu-item.active { background: var(--black); color: var(--white); }
.menu-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background: var(--white);
  border: 2px solid var(--black);
  box-shadow: 2px 2px 0 var(--black);
  min-width: 160px;
  z-index: 2000;
  color: var(--black);
}
.menu-item.active .menu-dropdown { display: block; }
.menu-dropdown .menu-entry {
  padding: 6px 14px;
  font-size: 9px;
  cursor: pointer;
  white-space: nowrap;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.menu-dropdown .menu-entry:hover { background: var(--black); color: var(--white); }
.menu-dropdown .menu-divider { border-bottom: 1px solid var(--black); margin: 2px 0; }
.menu-dropdown .menu-entry .shortcut { opacity: 0.6; font-size: 8px; margin-left: 12px; }

/* ─── WORKSPACE ─────────────────────────────────────────────────── */
#workspace {
  flex: 1;
  display: flex;
  gap: 4px;
  padding: 4px;
  padding-left: calc(4px + env(safe-area-inset-left));
  padding-right: calc(4px + env(safe-area-inset-right));
  overflow: hidden;
}

/* ─── TOOLBAR ───────────────────────────────────────────────────── */
#toolbar-window { width: 52px; min-width: 52px; flex-shrink: 0; }
.tool-btn {
  width: 38px; height: 38px;
  border: 2px solid var(--black);
  background: var(--light-gray);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  font-size: 16px;
  flex-shrink: 0;
  padding: 0;
}
.tool-btn:hover { background: var(--gray); }
.tool-btn.active {
  background: var(--black);
  color: var(--white);
  box-shadow: inset 2px 2px 0 #333;
}
.tool-separator { width: 32px; border-bottom: 2px solid var(--black); margin: 2px 0; }
#shape-options, #shape-stroke-row, #poly-options, #star-ratio-row {
  display: flex; flex-direction: column; align-items: center; gap: 4px; width: 100%;
}

/* ─── CANVAS AREA ────────────────────────────────────────────────── */
#canvas-window { flex: 1; min-width: 0; }
#canvas-scroll {
  flex: 1;
  overflow: auto;
  background: #888;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8'%3E%3Crect width='4' height='4' fill='%23777'/%3E%3Crect x='4' y='4' width='4' height='4' fill='%23777'/%3E%3C/svg%3E");
  display: flex;
  padding: 12px;
}
#canvas-wrapper {
  position: relative;
  flex-shrink: 0;
  margin: auto;
  box-shadow: 4px 4px 0 rgba(0,0,0,0.5);
}
#display-canvas {
  display: block;
  background: white;
}
#display-canvas    { position: relative; z-index: 1; }
#interaction-overlay {
  position: absolute;
  top: 0; left: 0;
  z-index: 20;
  touch-action: none;
}
#mask-canvas-overlay {
  position: absolute;
  top: 0; left: 0;
  z-index: 10;
  opacity: 0.4;
  display: none;
  pointer-events: none;
}

/* ─── RIGHT PANELS ───────────────────────────────────────────────── */
#right-panel {
  width: 286px;
  min-width: 286px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow: hidden;
}
#layers-window { flex: 0 0 auto; }
#pages-window { flex: 0 0 auto; }
#properties-window { flex: 1; overflow: hidden; }
#properties-window .window-content { min-height: 0; }

/* ─── LAYER LIST ─────────────────────────────────────────────────── */
#layer-list { flex: 1; overflow-y: auto; max-height: 160px; }
.layer-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px;
  cursor: pointer;
  border-bottom: 1px solid var(--light-gray);
  font-size: 8px;
}
.layer-row:hover { background: #f0f0f0; }
.layer-row.selected { background: var(--black); color: var(--white); }
.layer-row--mask { padding-left: 16px; }
.layer-row--masked { border-left: 2px solid var(--black); }
.layer-vis { cursor: pointer; font-size: 10px; flex-shrink: 0; }
.layer-lock { cursor: pointer; font-size: 10px; flex-shrink: 0; opacity: 0.5; }
.layer-lock:hover { opacity: 1; }
.layer-lock.locked { opacity: 1; }
.layer-row.locked-row { opacity: 0.6; }
.layer-row.locked-row .layer-name { font-style: italic; }
.layer-row.dragging { opacity: 0.4; }
.layer-row.drag-over-top { border-top: 2px solid var(--black); }
.layer-row.drag-over-bottom { border-bottom: 2px solid var(--black); }
.layer-color-dot {
  width: 9px; height: 9px;
  border: 1px solid var(--black);
  border-radius: 50%;
  flex-shrink: 0;
}
.layer-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 2;
}
#layer-buttons {
  display: flex;
  gap: 2px;
  padding: 4px;
  border-top: 1px solid var(--black);
  flex-shrink: 0;
}
#page-buttons {
  display: flex;
  gap: 2px;
  padding: 4px;
  border-top: 1px solid var(--black);
  flex-shrink: 0;
}

/* ─── PAGE LIST ──────────────────────────────────────────────────── */
#page-list { flex: 1; overflow-y: auto; max-height: 120px; }
.page-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  cursor: pointer;
  border-bottom: 1px solid var(--light-gray);
  font-size: 8px;
}
.page-row:hover { background: #f0f0f0; }
.page-row.selected { background: var(--black); color: var(--white); }
.page-row.selected .page-spread-meta { color: var(--white); }
.page-thumb {
  width: 18px; height: 24px;
  border: 1px solid var(--black);
  background: #fff;
  flex-shrink: 0;
}
.page-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 2;
}
/* ─── PROPERTIES ─────────────────────────────────────────────────── */
#properties-content {
  padding: 6px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.prop-section {
  border: 1px solid var(--black);
  padding: 5px;
}
.prop-section-title {
  font-size: 7px;
  text-transform: uppercase;
  letter-spacing: 1px;
  border-bottom: 1px solid var(--black);
  padding-bottom: 3px;
  margin-bottom: 5px;
}
.prop-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin-bottom: 5px;
}
.prop-row:last-child { margin-bottom: 0; }
.prop-label { font-size: 7px; flex-shrink: 0; min-width: 32px; }
.prop-value { font-size: 7px; color: var(--dark-gray); min-width: 20px; text-align: right; flex-shrink: 0; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 6px;
  background: var(--light-gray);
  border: 1px solid var(--black);
  outline: none;
  cursor: pointer;
  flex: 1;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 10px; height: 14px;
  background: var(--white);
  border: 2px solid var(--black);
  cursor: pointer;
}
input[type="number"] {
  width: 44px;
  font-family: var(--font);
  font-size: 7px;
  border: 1px solid var(--black);
  padding: 2px 3px;
  background: var(--white);
  text-align: right;
}

.transform-grid {
  display: grid;
  grid-template-columns: auto 1fr auto 1fr;
  gap: 3px 4px;
  align-items: center;
  font-size: 7px;
  margin-bottom: 4px;
}
.transform-grid input[type="number"] { width: 100%; }

.halftone-opts { display: flex; gap: 2px; flex-wrap: wrap; }
.halftone-opt,
.color-mode-btn,
.grad-type-btn,
.pat-type-btn,
.stop-remove-btn,
#btn-add-stop,
.mac-btn,
.mac-btn.sm,
.size-options label {
  font-family: var(--font);
  font-size: 8px;
  padding: 4px 8px;
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--dark-gray);
  border-right: 2px solid var(--dark-gray);
  background: var(--light-gray);
  cursor: pointer;
  line-height: 1.4;
  white-space: nowrap;
}
.halftone-opt:hover,
.color-mode-btn:hover,
.grad-type-btn:hover,
.pat-type-btn:hover,
.stop-remove-btn:hover,
#btn-add-stop:hover,
.mac-btn:hover,
.size-options label:hover {
  background: var(--gray);
}
.halftone-opt.active,
.color-mode-btn.active,
.grad-type-btn.active,
.pat-type-btn.active,
.mac-btn.active,
.mac-btn.primary,
.size-options label:has(input:checked) {
  background: var(--black);
  color: var(--white);
  border-color: var(--black);
}
.mac-btn.danger { color: #800; }
.mac-btn.primary:hover,
.size-options label:hover:has(input:checked) {
  background: var(--dark-gray);
  border-color: var(--dark-gray);
}
.mac-btn:active,
.halftone-opt:active,
.color-mode-btn:active,
.grad-type-btn:active,
.pat-type-btn:active,
.stop-remove-btn:active,
#btn-add-stop:active {
  border-top: 2px solid var(--dark-gray);
  border-left: 2px solid var(--dark-gray);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
}

.color-swatches { display: flex; flex-wrap: wrap; gap: 3px; }
.color-swatch {
  width: 20px; height: 20px;
  border: 2px solid var(--black);
  cursor: pointer;
  flex-shrink: 0;
}
.color-swatch.selected {
  box-shadow: 0 0 0 2px var(--white), 0 0 0 4px var(--black);
}

/* ─── UNIFIED BUTTONS ───────────────────────────────────────────── */
/* Base styles consolidated above with .halftone-opt etc. */

/* ─── STATUS BAR ─────────────────────────────────────────────────── */
#status-bar {
  background: var(--white);
  border-top: 2px solid var(--black);
  height: calc(20px + env(safe-area-inset-bottom));
  min-height: calc(20px + env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  padding: 0 8px env(safe-area-inset-bottom);
  gap: 16px;
  flex-shrink: 0;
  font-size: 7px;
}
#status-bar span { color: var(--dark-gray); }
#status-bar strong { color: var(--black); }

/* ─── DIALOGS ────────────────────────────────────────────────────── */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4'%3E%3Crect width='2' height='2' fill='rgba(0,0,0,0.3)'/%3E%3C/svg%3E");
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5000;
}
.dialog-overlay.hidden { display: none; }
.hidden { display: none; }
.mac-dialog {
  background: var(--white);
  border: 2px solid var(--black);
  box-shadow: 4px 4px 0 var(--black);
  min-width: 340px;
  max-width: 500px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}
.dialog-titlebar {
  background: repeating-linear-gradient(90deg, var(--black) 0px, var(--black) 1px, var(--white) 1px, var(--white) 2px);
  height: var(--title-h);
  display: flex;
  align-items: center;
  padding: 0 6px;
  border-bottom: 2px solid var(--black);
  flex-shrink: 0;
}
.dialog-title {
  flex: 1;
  text-align: center;
  font-size: 9px;
  background: var(--white);
  padding: 2px 8px;
  border: 1px solid var(--black);
  margin: 2px 8px;
}
.dialog-close {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  margin: 2px 2px 2px 0;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--white);
  border: 1px solid var(--black);
  font-family: inherit;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
.dialog-close:active { background: var(--black); color: var(--white); }
.dialog-body { padding: 16px; overflow-y: auto; flex: 1; }
.dialog-body p {
  padding: 14px 0;
}
.dialog-footer {
  padding: 10px 16px;
  border-top: 1px solid var(--black);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
}
.project-list {
  border: 1px solid var(--black);
  max-height: 200px;
  overflow-y: auto;
  margin-bottom: 12px;
}
.project-entry {
  padding: 7px 10px;
  cursor: pointer;
  border-bottom: 1px solid var(--light-gray);
  font-size: 9px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.project-entry:hover { background: #f0f0f0; }
.project-entry.selected { background: var(--black); color: var(--white); }
.project-date { font-size: 7px; color: var(--dark-gray); }
.project-entry.selected .project-date { color: var(--gray); }
.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.input-row label { font-size: 9px; flex-shrink: 0; }
.size-row {
  margin-bottom: 10px;
}
.size-row-label {
  font-size: 9px;
  margin-bottom: 6px;
}
.size-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}
.size-options input[type="radio"] { display: none; }
.mac-input {
  flex: 1;
  font-family: var(--font);
  font-size: 9px;
  border: 2px solid var(--dark-gray);
  padding: 4px 6px;
  background: var(--white);
  outline: none;
}
.mac-input:focus { outline: 2px solid var(--black); }
.margin-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.margin-grid label {
  display: flex;
  flex-direction: column;
  font-size: 9px;
  gap: 4px;
}
.margin-grid .mac-input { width: 100%; box-sizing: border-box; }
.grid-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 9px;
}
.grid-row label { flex-shrink: 0; }
.grid-row .mac-input { width: 70px; flex: none; }

/* Scrollbars */
::-webkit-scrollbar { width: 12px; height: 12px; }
::-webkit-scrollbar-track { background: var(--light-gray); border: 1px solid var(--black); }
::-webkit-scrollbar-thumb { background: var(--gray); border: 1px solid var(--black); }

/* Loading spinner */
.loading-spinner {
  width: 24px; height: 24px;
  border: 3px solid var(--light-gray);
  border-top-color: var(--black);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Cursor modes */
body.tool-select #interaction-overlay { cursor: default; }
body.tool-move #interaction-overlay { cursor: move; }
body.tool-mask-draw #interaction-overlay { cursor: none; }
body.tool-mask-erase #interaction-overlay { cursor: none; }

#brush-cursor {
  position: absolute;
  border-radius: 50%;
  pointer-events: none;
  z-index: 30;
  transform: translate(-50%, -50%);
  display: none;
}
body.tool-mask-draw #brush-cursor  { border: 1.5px solid rgba(0,0,0,0.8); background: rgba(0,0,0,0.08); }
body.tool-mask-erase #brush-cursor { border: 1.5px solid rgba(0,0,0,0.5); background: rgba(255,255,255,0.15); }

/* Zoom bar */
#zoom-controls {
  display: flex;
  gap: 4px;
  align-items: center;
  border-top: 1px solid var(--black);
  padding: 4px;
  flex-shrink: 0;
}
#zoom-display { font-size: 8px; min-width: 36px; text-align: center; }

/* No-layer hint */
#no-layer-msg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  color: var(--dark-gray);
  text-align: center;
  pointer-events: none;
  line-height: 3;
}

/* ─── GRADIENT EDITOR ────────────────────────────────────────────── */
.color-mode-toggle { display:flex; gap:2px; margin-bottom:6px; }
#gradient-editor { margin-top:4px; }
#gradient-editor.hidden { display:none; }
.gradient-type-row { display:flex; gap:2px; flex-wrap:wrap; margin-bottom:5px; }
#gradient-bar-canvas {
  width:100%; height:20px; display:block;
  border:1px solid var(--black); cursor:crosshair; margin-bottom:4px;
  touch-action: none;
}
.gradient-stop-row {
  display:flex; align-items:center; gap:3px;
  margin-bottom:3px; font-size:10px; font-family:var(--font);
}
.stop-swatch {
  width:14px; height:14px; flex-shrink:0;
  border:1px solid var(--black);
}
.stop-riso-swatches { display:flex; gap:2px; flex-wrap:wrap; margin-bottom:3px; }
.stop-riso-sw {
  width:12px; height:12px; cursor:pointer; flex-shrink:0;
  border:1px solid transparent;
}
.stop-riso-sw.selected { border-color:var(--black); box-shadow:0 0 0 1px var(--black) inset; }
.stop-remove-btn:disabled { opacity:0.4; cursor:default; }

/* ─── PATTERN EDITOR ─────────────────────────────────────────────── */
#pattern-editor { margin-top:4px; }
#pattern-editor.hidden { display:none; }
.pattern-type-row { display:flex; gap:2px; flex-wrap:wrap; margin-bottom:5px; }
.pattern-color-row { margin-bottom:4px; }
.pattern-color-row .prop-label { display:block; margin-bottom:2px; font-size:10px; }
.pattern-color-swatches { display:flex; gap:2px; flex-wrap:wrap; }
.pat-color-sw {
  width:12px; height:12px; cursor:pointer; flex-shrink:0;
  border:1px solid transparent;
}
.pat-color-sw.selected { border-color:var(--black); box-shadow:0 0 0 1px var(--black) inset; }
.pat-slider-row {
  display:flex; align-items:center; gap:4px; margin-bottom:4px;
}
.pat-slider-row .prop-label { width:30px; font-size:10px; flex-shrink:0; }
.pat-slider-row input[type=range] { flex:1; }
.pat-slider-row .prop-value { width:28px; font-size:10px; text-align:right; }
.grad-angle-row, .grad-center-row {
  display:flex; align-items:center; gap:3px; margin-bottom:4px;
}

/* ─── SCREENTONE DIALOG ──────────────────────────────────────────── */
.screentone-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 8px;
  padding: 4px;
}
.screentone-item {
  border: 2px solid var(--black);
  background: var(--white);
  cursor: pointer;
  padding: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: background 0.1s;
}
.screentone-item:hover {
  background: var(--light-gray);
}
.screentone-item.selected {
  background: var(--black);
  color: var(--white);
}
.screentone-item img {
  width: 64px;
  height: 64px;
  object-fit: cover;
  image-rendering: pixelated;
  border: 1px solid var(--black);
  background: var(--white);
}
.screentone-item.selected img {
  border-color: var(--white);
}
.screentone-item .screentone-name {
  font-size: 8px;
  text-align: center;
  line-height: 1.4;
  word-break: break-word;
  max-width: 100%;
}
