*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; user-select: none; -webkit-user-select: none; }

#app {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  grid-template-rows: 44px auto 1fr auto;
  grid-template-areas:
    "top top top"
    "ai ai ai"
    "tools canvas settings"
    "commands commands commands";
  height: 100vh;
  width: 100vw;
}

#top-panel {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: #2b2b2b;
  color: #eee;
  border-bottom: 1px solid #111;
}

.top-btn {
  background: #3a3a3a;
  border: 1px solid #1a1a1a;
  color: #eee;
  width: 34px; height: 34px;
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0;
}
.top-btn:hover { background: #4a4a4a; }
.top-btn.active { background: #1e88e5; }
.top-btn.color-btn .swatch { display: inline-block; width: 24px; height: 24px; border-radius: 3px; border: 1px solid #000; font-size: 14px; color: white; text-align: center; line-height: 22px; }

/* Text buttons (home link, account) auto-size and keep readable padding. */
.top-btn.brand-home, .top-btn.account-btn {
  width: auto; padding: 0 12px; font-size: 13px; text-decoration: none; white-space: nowrap;
}
.top-btn.brand-home { color: #9ad; }
.top-btn.account-btn { margin-left: 4px; }
.top-btn.account-btn.signed-in { background: #1e6fae; border-color: #15527f; }
.top-btn.account-btn.signed-in:hover { background: #2480c4; }

/* AI chat-to-draw bar — full-width strip below the toolbar. */
#ai-bar {
  grid-area: ai;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: #232323;
  border-bottom: 1px solid #111;
}
#ai-bar .ai-icon { font-size: 16px; flex: none; }
#ai-input {
  flex: 1;
  min-width: 0;
  background: #1a1a1a;
  color: #eee;
  border: 1px solid #3a3a3a;
  border-radius: 6px;
  padding: 7px 11px;
  font-size: 13px;
  outline: none;
}
#ai-input::placeholder { color: #777; }
#ai-input:focus { border-color: #1e88e5; }
.ai-send {
  flex: none;
  background: #1e88e5;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 18px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.ai-send:hover { background: #2b95ef; }
.ai-send:disabled { background: #555; cursor: default; }
.ai-status {
  flex: none;
  max-width: 38%;
  font-size: 12px;
  color: #8fa3b0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ai-status.error { color: #e57373; }

#doc-tabs { flex: 1; display: flex; gap: 2px; overflow-x: auto; }
.doc-tab { background: #3a3a3a; color: #ccc; padding: 4px 10px; border-radius: 4px 4px 0 0; cursor: pointer; font-size: 12px; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; max-width: 160px; }
.doc-tab-label { overflow: hidden; text-overflow: ellipsis; }
.doc-tab.active { background: #555; color: #fff; }
.doc-tab .close { cursor: pointer; opacity: 0.6; }
.doc-tab .close:hover { opacity: 1; }

#tools-panel {
  grid-area: tools;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px;
  background: #222;
  border-right: 1px solid #111;
}
.tool-btn {
  width: 44px; height: 44px;
  background: #3a3a3a;
  border: 1px solid #1a1a1a;
  color: #eee;
  font-size: 18px;
  cursor: pointer;
  border-radius: 4px;
}
.tool-btn:hover { background: #4a4a4a; }
.tool-btn.active { background: #1e88e5; }
.tool-icon { width: 20px; height: 20px; fill: currentColor; vertical-align: middle; pointer-events: none; }

#canvas-wrap {
  grid-area: canvas;
  overflow: auto;
  background: #444;
  position: relative;
}
#code-view {
  grid-area: canvas;
  margin: 0;
  padding: 16px;
  overflow: auto;
  background: #1a1a1a;
  color: #d4d4d4;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  user-select: text;
  -webkit-user-select: text;
}
#code-view.hidden { display: none; }
#canvas-wrap.hidden { display: none; }
#pasteboard {
  min-width: 100%;
  min-height: 100%;
  padding: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#canvas {
  background: #888; /* pasteboard visible around page */
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  touch-action: none;
}

.panel {
  background: #2b2b2b;
  color: #eee;
  border: 1px solid #111;
  font-size: 13px;
}
.panel.hidden { display: none !important; }
.panel header { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; background: #1e1e1e; border-bottom: 1px solid #111; }
.panel header h3 { margin: 0; font-size: 13px; font-weight: 600; }
.panel-close { background: transparent; color: #ccc; border: none; font-size: 18px; cursor: pointer; }
.panel-close:hover { color: #fff; }

#settings-panel {
  grid-area: settings;
  width: 260px;
  overflow-y: auto;
}
#settings-body { padding: 10px; display: flex; flex-direction: column; gap: 10px; }
#settings-body .row { display: flex; align-items: center; gap: 8px; }
#settings-body label { flex: 1; font-size: 12px; }
#settings-body input[type=range] { flex: 2; }
#settings-body input[type=number], #settings-body input[type=text] { width: 64px; background: #1a1a1a; color: #eee; border: 1px solid #000; padding: 3px 5px; border-radius: 3px; }
#settings-body select { background: #1a1a1a; color: #eee; border: 1px solid #000; padding: 3px; }
#settings-body .toggle { cursor: pointer; padding: 4px 10px; background: #3a3a3a; border-radius: 3px; border: 1px solid #000; }
#settings-body .toggle.on { background: #1e88e5; }

#commands-panel {
  grid-area: commands;
  max-height: 180px;
  overflow-y: auto;
}
#commands-body { display: flex; gap: 10px; padding: 10px; flex-wrap: wrap; }
.cmd-group { display: flex; flex-direction: column; gap: 4px; min-width: 120px; }
.cmd-group h4 { margin: 0 0 4px 0; font-size: 11px; color: #aaa; text-transform: uppercase; }
.cmd-btn { background: #3a3a3a; color: #eee; border: 1px solid #1a1a1a; padding: 4px 8px; cursor: pointer; border-radius: 3px; text-align: left; font-size: 12px; }
.cmd-btn:hover { background: #4a4a4a; }
.cmd-btn:disabled { opacity: 0.4; cursor: not-allowed; }

#color-panel {
  position: fixed;
  top: 48px;
  right: 270px;
  width: 320px;
  max-height: 80vh;
  overflow-y: auto;
  z-index: 20;
}
#color-body { padding: 10px; }

#zoom-panel {
  position: fixed;
  top: 48px;
  left: 60px;
  padding: 6px 10px;
  display: flex; gap: 6px; align-items: center;
  z-index: 20;
}
#zoom-panel button { background: #3a3a3a; color: #eee; border: 1px solid #000; padding: 3px 8px; cursor: pointer; border-radius: 3px; font-size: 11px; }

#file-menu {
  position: fixed;
  top: 48px;
  left: 4px;
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 25;
  min-width: 160px;
}
#file-menu button { background: #2b2b2b; color: #eee; border: 1px solid transparent; padding: 6px 10px; cursor: pointer; text-align: left; font-size: 13px; }
#file-menu button:hover { background: #3a3a3a; border-color: #555; }

#shape-flyout {
  position: fixed;
  left: 56px;
  top: 250px;
  padding: 4px;
  display: grid;
  grid-template-columns: repeat(4, 36px);
  gap: 2px;
  z-index: 25;
}
#shape-flyout .shape-variant {
  width: 36px; height: 36px;
  background: #3a3a3a;
  color: #eee;
  border: 1px solid #1a1a1a;
  font-size: 16px;
  cursor: pointer;
  border-radius: 4px;
}
#shape-flyout .shape-variant:hover { background: #4a4a4a; }
#shape-flyout .shape-variant.active { background: #1e88e5; }

/* Inline text editor — an <input> overlaid on the canvas at the click point. */
.text-editor {
  position: fixed;
  z-index: 80;
  margin: 0;
  padding: 0 1px;
  border: none;
  outline: 1px dashed #1e88e5;
  background: rgba(255, 255, 255, 0.9);
  line-height: 1.15;
  min-width: 6px;
  white-space: pre;
  caret-color: #1e88e5;
}

#settings-body input[type=color].color-input {
  width: 40px;
  height: 24px;
  padding: 0;
  background: #1a1a1a;
  border: 1px solid #000;
  border-radius: 3px;
  cursor: pointer;
}

.selection-marquee {
  fill: rgba(30, 136, 229, 0.1);
  stroke: #1e88e5;
  stroke-dasharray: 4 2;
  stroke-width: 1;
}
.sel-outline {
  fill: none;
  stroke: #1e88e5;
  stroke-width: 1.5;
  stroke-dasharray: 3 3;
  pointer-events: none;
}

/* Path tool: live preview of the segment being drawn + placed vertices. */
.path-rubber {
  fill: none;
  stroke-width: 1.5;
  stroke-dasharray: 5 3;
  opacity: 0.8;
  pointer-events: none;
}
.path-vertex {
  stroke: #fff;
  stroke-width: 1;
  pointer-events: none;
}
.path-vertex.path-close-hint {
  r: 6;
  fill: #ff9800;
  stroke: #fff;
  stroke-width: 2;
}

/* Quick blink when a shape enters the selection marquee. */
.flash-blink {
  animation: flashBlink 0.4s ease-out;
}
@keyframes flashBlink {
  0%   { filter: drop-shadow(0 0 0 #1e88e5); }
  35%  { filter: drop-shadow(0 0 6px #1e88e5); opacity: 0.35; }
  100% { filter: none; opacity: 1; }
}

/* Sequential highlight of each freshly-cut piece. */
.cut-flash {
  animation: cutFlash 0.6s ease-out;
}
@keyframes cutFlash {
  0%   { filter: drop-shadow(0 0 0 #ff9800); opacity: 1; }
  25%  { filter: drop-shadow(0 0 8px #ff9800); opacity: 0.3; }
  60%  { filter: drop-shadow(0 0 6px #ff9800); opacity: 1; }
  100% { filter: none; opacity: 1; }
}

/* Numbered badge shown over each cut piece, in order. */
.cut-badge { pointer-events: none; }
.cut-badge circle {
  fill: #ff9800;
  stroke: #fff;
  stroke-width: 1.5;
  transform-box: fill-box;
  transform-origin: center;
  animation: badgePop 0.9s ease-out forwards;
}
.cut-badge text {
  fill: #fff;
  font: 700 13px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  text-anchor: middle;
  dominant-baseline: central;
  transform-box: fill-box;
  transform-origin: center;
  animation: badgePop 0.9s ease-out forwards;
}
@keyframes badgePop {
  0%   { opacity: 0; transform: scale(0.4); }
  20%  { opacity: 1; transform: scale(1.1); }
  35%  { transform: scale(1); }
  80%  { opacity: 1; }
  100% { opacity: 0; }
}

/* Color palette */
.palette-grid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 2px; }
.palette-cell { aspect-ratio: 1; border: 1px solid #111; cursor: pointer; position: relative; }
.palette-cell.selected::after { content: "✓"; position: absolute; top: -2px; right: 0; color: white; text-shadow: 0 0 2px #000; font-size: 11px; }
.palette-cell.transparent { background-image: linear-gradient(45deg, #fff 25%, #ccc 25%, #ccc 50%, #fff 50%, #fff 75%, #ccc 75%); background-size: 8px 8px; }
.palette-cell.transparent::before { content: ""; position: absolute; inset: 0; background: linear-gradient(to top right, transparent 47%, red 47%, red 53%, transparent 53%); }

/* Modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 100; display: flex; align-items: center; justify-content: center; }
.modal { background: #2b2b2b; color: #eee; padding: 16px 18px; border-radius: 6px; min-width: 340px; max-width: 90vw; max-height: 90vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,0.5); }
.modal h2 { margin: 0 0 10px 0; font-size: 16px; }
.modal p { margin: 0 0 12px 0; color: #ccc; font-size: 13px; white-space: pre-wrap; }
.modal .modal-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.modal .modal-field label { font-size: 12px; color: #aaa; }
.modal .modal-field input, .modal .modal-field select, .modal .modal-field textarea {
  background: #1a1a1a; color: #eee; border: 1px solid #000; padding: 6px 8px;
  border-radius: 3px; font-size: 13px; font-family: inherit; width: 100%;
}
.modal .modal-field textarea { resize: vertical; min-height: 60px; }
.modal .modal-field input:focus, .modal .modal-field select:focus, .modal .modal-field textarea:focus {
  outline: none; border-color: #1e88e5;
}
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal button { background: #3a3a3a; color: #eee; border: 1px solid #000; padding: 6px 14px; cursor: pointer; border-radius: 3px; font-size: 13px; }
.modal button:hover { background: #4a4a4a; }
.modal button.primary { background: #1e88e5; border-color: #1565c0; }
.modal button.primary:hover { background: #2196f3; }

@media print {
  body * { visibility: hidden; }
  #canvas, #canvas * { visibility: visible; }
  #canvas { position: absolute; left: 0; top: 0; }
}
