:root {
  --bg: #f5f1ea;
  --panel: #ffffff;
  --border: #d9d2c4;
  --text: #2c2620;
  --accent: #8a4b3b;
  --accent-light: #c98b6f;
  --grid-dot: #b9ad98;
  --pin: #2c2620;
  --selected: #1b6f8c;
  --selected-bg: rgba(27, 111, 140, 0.12);
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app-header {
  display: flex;
  align-items: center;
  padding: 0.55rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.app-header-title {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--accent);
}

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 1rem 0.6rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.toolbar-tabs {
  display: flex;
  gap: 0.3rem;
}

.toolbar-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.75rem;
  border: 1px solid transparent;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: #6b6255;
  font-size: 0.78rem;
  cursor: pointer;
}

.toolbar-tab:hover {
  color: var(--accent);
}

.toolbar-tab.active {
  background: var(--bg);
  border-color: var(--border);
  border-bottom-color: var(--bg);
  color: var(--accent);
  font-weight: 600;
  margin-bottom: -1px;
}

.toolbar-panel {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.25rem;
  padding: 0.5rem 0.6rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0 6px 6px 6px;
}

.toolbar-panel[hidden] {
  display: none;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.toolbar label {
  display: flex;
  flex-direction: column;
  font-size: 0.7rem;
  color: #6b6255;
  gap: 0.15rem;
}

.toolbar label.checkbox {
  flex-direction: row;
  align-items: center;
  font-size: 0.8rem;
  color: var(--text);
}

.toolbar input[type="number"],
.toolbar select {
  font-size: 0.85rem;
  padding: 0.25rem 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  width: 6rem;
  background: white;
}

.toolbar input[type="number"] {
  width: 4.5rem;
}

button, .file-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  font-size: 0.8rem;
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: white;
  color: var(--text);
  cursor: pointer;
  line-height: 1;
}

/* Toolbar/palette buttons are icon-only: square, no visible label (the label
   lives in the title attribute and shows as the browser's native hover tooltip). */
.toolbar-panel button,
.toolbar-panel .file-button,
.tool-palette button {
  width: 34px;
  height: 34px;
  padding: 0;
  flex-shrink: 0;
}

button .icon, .file-button .icon {
  flex-shrink: 0;
}

button:hover, .file-button:hover {
  border-color: var(--accent-light);
  color: var(--accent);
}

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

button:disabled:hover {
  border-color: var(--border);
  color: var(--text);
}

.file-button {
  display: inline-flex;
}

.tool-palette {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.25rem;
  background: var(--panel);
  padding: 0.3rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  box-shadow: 0 3px 10px rgba(44, 38, 32, 0.15);
}

.tool-palette-handle {
  width: 34px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #b9ad98;
  cursor: grab;
  touch-action: none;
}

.tool-palette-handle:active {
  cursor: grabbing;
}

.tool-palette-divider {
  height: 1px;
  margin: 0.15rem 0.25rem;
  background: var(--border);
}

.color-swatch {
  -webkit-appearance: none;
  appearance: none;
  width: 34px;
  height: 34px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: pointer;
}

.color-swatch::-webkit-color-swatch-wrapper {
  padding: 0;
}

.color-swatch::-webkit-color-swatch {
  border: none;
  border-radius: 3px;
}

.color-swatch::-moz-color-swatch {
  border: none;
  border-radius: 3px;
}

.tool-btn {
  border-color: transparent;
  background: transparent;
}

.tool-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.tool-btn.active:hover {
  color: white;
}

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
}

.canvas-container {
  flex: 1;
  overflow: hidden;
  position: relative;
  background: repeating-linear-gradient(45deg, #efeae0 0 1px, var(--bg) 1px 14px);
  cursor: grab;
  touch-action: none;
}

.canvas-container:active {
  cursor: grabbing;
}

.print-footer {
  display: none;
}

.recent-panel {
  width: 200px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 0.9rem 0.75rem;
  overflow-y: auto;
}

.recent-panel h2 {
  margin: 0 0 0.6rem;
  font-size: 0.85rem;
  color: var(--accent);
}

.layers-panel {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 20;
  width: 210px;
  max-width: calc(100% - 32px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 9px;
  box-shadow: 0 3px 10px rgba(44, 38, 32, 0.15);
  max-height: min(60vh, 420px);
  overflow: hidden;
}

.layers-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  padding: 0.4rem 0.4rem 0.4rem 0.6rem;
  border-bottom: 1px solid var(--border);
  cursor: grab;
  touch-action: none;
  flex-shrink: 0;
}

.layers-panel-header:active {
  cursor: grabbing;
}

.layers-panel-title {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}

.layers-panel-actions {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  flex-shrink: 0;
}

.layers-panel-actions button {
  width: 23px;
  height: 23px;
  padding: 0;
  border-color: transparent;
  background: transparent;
}

.layers-panel-toggle svg {
  transition: transform 0.15s;
}

.layers-panel.collapsed .layers-panel-toggle svg {
  transform: rotate(-90deg);
}

.layers-panel.collapsed {
  max-height: none;
}

.layers-panel.collapsed .layers-list {
  display: none;
}

.layers-panel .layers-list {
  overflow-y: auto;
  padding: 0.4rem;
}

.layers-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.layer-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.25rem 0.35rem;
  border: 1px solid transparent;
  border-radius: 5px;
}

.layer-row.active {
  border-color: var(--border);
  background: white;
}

.layer-row input[type="radio"] {
  flex-shrink: 0;
  accent-color: var(--accent);
  cursor: pointer;
}

.layer-visibility {
  width: 24px;
  height: 24px;
  padding: 0;
  flex-shrink: 0;
  border-color: transparent;
  background: transparent;
  color: #6b6255;
}

.layer-row:not(.visible) .layer-visibility {
  color: #b9ad98;
}

.layer-name {
  flex: 1;
  min-width: 0;
  border: 1px solid transparent;
  background: transparent;
  font-size: 0.78rem;
  color: var(--text);
  padding: 0.15rem 0.3rem;
  border-radius: 4px;
}

.layer-name:hover, .layer-name:focus {
  border-color: var(--border);
  background: white;
  outline: none;
}

.layer-row:not(.visible) .layer-name {
  color: #a89e8f;
  font-style: italic;
}

.layer-delete {
  width: 24px;
  height: 24px;
  padding: 0;
  flex-shrink: 0;
  border-color: transparent;
  background: transparent;
  color: #6b6255;
}

.layer-delete:disabled {
  visibility: hidden;
}

.recent-status {
  margin: 0 0 0.6rem;
  font-size: 0.72rem;
  color: #6b6255;
}

.recent-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.recent-item {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: pointer;
  overflow: hidden;
  text-align: left;
  padding: 0;
  width: 100%;
  height: auto;
}

.recent-item:hover {
  border-color: var(--accent-light);
}

.recent-item.current {
  border-color: var(--selected);
  box-shadow: 0 0 0 1px var(--selected);
}

.thumb-frame {
  aspect-ratio: 1 / 1;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--border);
}

.thumb-svg {
  width: 88%;
  height: 88%;
}

.thumb-pin {
  fill: var(--pin);
}

.thumb-line {
  stroke: var(--text);
  stroke-width: 0.5;
  fill: none;
}

.recent-item-name {
  display: block;
  font-size: 0.72rem;
  padding: 0.35rem 0.5rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}

.picado-svg {
  position: absolute;
  top: 24px;
  left: 24px;
  background: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
  transform-origin: 0 0;
  user-select: none;
}

/* Purely visual layers must not intercept pointer events — they're painted on
   top of the invisible .pin-hit / .element-hit targets and would otherwise
   swallow clicks meant for those. */
.grid-dot, .pin-dot, .pin-label, .line-path, .arc-path,
.pending-dot, .preview-path {
  pointer-events: none;
}

.grid-dot {
  fill: var(--grid-dot);
}

.pin-dot {
  fill: var(--pin);
}

.pin-dot.selected {
  /* fill stays the pin's own color (set inline) — a ring indicates selection instead
     of overriding it, so a custom drawing color remains visible while selected. */
  stroke: var(--selected);
  stroke-width: 0.5;
}

.pin-hit {
  fill: transparent;
  cursor: pointer;
}

.pin-hit.draggable {
  cursor: grab;
}

.pin-hit.draggable:active {
  cursor: grabbing;
}

.pin-label {
  font-size: 2.4px;
  fill: var(--accent);
  font-family: monospace;
}

.line-path {
  stroke: var(--text);
  stroke-width: 0.35;
  fill: none;
}

.arc-path {
  stroke: var(--text);
  stroke-width: 0.35;
  fill: none;
}

.line-path.selected, .arc-path.selected {
  /* stroke stays the element's own color (set inline); the halo underneath it
     (see .selection-halo) is what actually signals the selected state. */
  stroke-width: 0.5;
}

.selection-halo {
  stroke: var(--selected);
  stroke-width: 1.6;
  stroke-linecap: round;
  fill: none;
  opacity: 0.45;
  pointer-events: none;
}

.element-hit {
  stroke: transparent;
  stroke-width: 2.4;
  fill: none;
  cursor: pointer;
}

.pending-dot {
  fill: var(--selected);
}

.preview-path {
  stroke: var(--selected);
  stroke-width: 0.35;
  stroke-dasharray: 1 1;
  fill: none;
}

.marquee {
  fill: var(--selected-bg);
  stroke: var(--selected);
  stroke-width: 0.3;
  stroke-dasharray: 1 1;
  vector-effect: non-scaling-stroke;
  pointer-events: none;
}

.statusbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.35rem 1rem;
  font-size: 0.75rem;
  color: #6b6255;
  background: var(--panel);
  border-top: 1px solid var(--border);
}

#toolHint {
  color: var(--accent);
}

dialog#libraryDialog {
  width: min(32rem, 90vw);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  color: var(--text);
}

dialog#libraryDialog::backdrop {
  background: rgba(44, 38, 32, 0.35);
}

dialog#libraryDialog h2 {
  margin: 0 0 0.75rem;
  font-size: 1.05rem;
  color: var(--accent);
}

.library-status {
  margin: 0 0 0.5rem;
  font-size: 0.8rem;
  color: #6b6255;
  min-height: 1.1em;
}

.library-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 50vh;
  overflow-y: auto;
  border-top: 1px solid var(--border);
}

.library-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.55rem 0.1rem;
  border-bottom: 1px solid var(--border);
}

.library-item-info {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  min-width: 0;
}

.library-item-info .name {
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-item-info .meta {
  font-size: 0.72rem;
  color: #6b6255;
}

.library-item-actions {
  display: flex;
  gap: 0.4rem;
  flex-shrink: 0;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.6rem;
  margin-top: 1rem;
}

dialog.confirm-dialog {
  width: min(26rem, 90vw);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.25rem 1.5rem;
  color: var(--text);
}

dialog.confirm-dialog::backdrop {
  background: rgba(44, 38, 32, 0.35);
}

#confirmMessage {
  margin: 0;
  font-size: 0.92rem;
  line-height: 1.4;
}

.dialog-actions .btn-danger {
  background: #a53b2e;
  color: #fff;
  border-color: #a53b2e;
}

.dialog-actions .btn-danger:hover {
  background: #8c3125;
}

@media print {
  @page {
    size: A4;
    margin: 0;
  }

  html, body {
    height: auto;
  }

  .app {
    display: block;
    height: auto;
  }

  .app-header, .toolbar, .statusbar, .recent-panel, .tool-palette, .layers-panel {
    display: none;
  }

  .workspace {
    display: block;
  }

  .canvas-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 210mm;
    height: 297mm;
    overflow: visible;
    position: relative;
    background: none;
    cursor: auto;
  }

  .picado-svg {
    position: static;
    box-shadow: none;
    transform: none !important;
  }

  .print-footer {
    display: block;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 8mm;
    text-align: center;
  }

  .print-footer-name {
    font-size: 11pt;
    font-weight: 600;
    color: #2c2620;
  }

  .print-footer-site {
    font-size: 8.5pt;
    color: #8a4b3b;
    margin-top: 1mm;
  }
}
