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

:root {
  --bg-top: #f4efe5;
  --bg-bottom: #dfe8ee;
  --surface: rgba(255, 251, 244, 0.84);
  --surface-strong: rgba(255, 252, 247, 0.96);
  --surface-soft: rgba(255, 247, 236, 0.88);
  --line: #d7cfbf;
  --line-strong: #bea98b;
  --ink: #22303d;
  --muted: #6e746f;
  --accent: #db6b42;
  --primary: #3f6494;
  --success: #2f6f59;
  --danger: #a6493d;
  --canvas: #d7e2ea;
  --shadow: 0 18px 40px rgba(87, 80, 63, 0.14);
}

html, body {
  height: 100%;
  font-family: 'Aptos', 'Segoe UI Variable Text', 'Trebuchet MS', sans-serif;
  font-size: 13px;
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.75), transparent 28%),
    radial-gradient(circle at top right, rgba(255, 211, 160, 0.22), transparent 22%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--ink);
  overflow: hidden;
}

/* ── App shell ───────────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

/* ── Toolbar ──────────────────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 18px;
  min-height: 76px;
  background: rgba(255, 252, 246, 0.78);
  border-bottom: 1px solid rgba(190, 169, 139, 0.5);
  backdrop-filter: blur(18px);
  box-shadow: 0 8px 24px rgba(78, 71, 55, 0.08);
  flex-shrink: 0;
  z-index: 10;
}

.toolbar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.toolbar-brand .logo {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 14px;
  font-size: 22px;
  background: linear-gradient(135deg, #ffd26f, #eb7a45);
  color: #482c14;
  box-shadow: 0 10px 24px rgba(217, 109, 68, 0.28);
}

.brand-copy {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.brand-title {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: 0.03em;
  color: #2b3541;
}

.brand-subtitle {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: #8c7761;
}

.toolbar-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.meta-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--surface-soft);
  border: 1px solid #ead5ba;
  color: #6d5a46;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.toolbar-tools {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.tool-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.9), rgba(245, 237, 227, 0.95));
  border: 1px solid #d9cbb7;
  border-radius: 14px;
  color: #4b5865;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
  box-shadow: 0 8px 16px rgba(133, 115, 90, 0.08);
  transition: transform 0.18s, background 0.18s, color 0.18s, border-color 0.18s, box-shadow 0.18s;
}

.tool-btn:hover {
  transform: translateY(-1px);
  background: #fffdf9;
  color: #334454;
  border-color: #cdb18a;
  box-shadow: 0 12px 22px rgba(133, 115, 90, 0.14);
}

.tool-btn.active {
  background: linear-gradient(180deg, #f2f7ff, #dce9fb);
  color: #2c5888;
  border-color: #9cbce5;
  box-shadow: 0 14px 24px rgba(63, 100, 148, 0.18);
}

.tool-btn svg { flex-shrink: 0; }

.toolbar-actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid #d7cab7;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(243, 235, 225, 0.95));
  color: #4a5763;
  box-shadow: 0 8px 18px rgba(133, 115, 90, 0.08);
  transition: transform 0.18s, background 0.18s, color 0.18s, border-color 0.18s, box-shadow 0.18s;
}

.action-btn:hover {
  transform: translateY(-1px);
  background: #fffdf9;
  border-color: #cdb18a;
  box-shadow: 0 12px 20px rgba(133, 115, 90, 0.14);
}

.action-btn.primary {
  background: linear-gradient(180deg, #ebfff6, #d6f5e8);
  color: var(--success);
  border-color: #9bc5b5;
}

.action-btn.primary:hover {
  background: linear-gradient(180deg, #f2fff8, #dff8ec);
}

.action-btn.danger {
  background: linear-gradient(180deg, #fff2ef, #f8dcd7);
  color: var(--danger);
  border-color: #d9a49a;
}

.action-btn.danger:hover {
  background: linear-gradient(180deg, #fff6f4, #f9e2dc);
}

.action-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.action-btn.full-width { width: 100%; }

/* ── Global focus rings (keyboard navigation) ───────────────────────────── */
:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 3px;
  border-radius: 6px;
}
.action-btn:focus-visible,
.tool-btn:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 3px;
}
/* Suppress mouse-click outlines on form controls */
.form-group input:focus:not(:focus-visible),
.form-group select:focus:not(:focus-visible) {
  outline: none;
}
.form-group input:focus-visible,
.form-group select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

/* ── Main layout ──────────────────────────────────────────────────────────── */
#main-content {
  display: flex;
  flex: 1;
  gap: 16px;
  padding: 16px;
  overflow: hidden;
}

/* ── Side panels ──────────────────────────────────────────────────────────── */
aside {
  width: 250px;
  flex-shrink: 0;
  background: var(--surface);
  border: 1px solid rgba(190, 169, 139, 0.6);
  border-radius: 26px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0;
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

#right-panel {
  border-left: 1px solid rgba(190, 169, 139, 0.6);
}

.panel-section {
  padding: 14px;
  border-bottom: 1px solid rgba(215, 207, 191, 0.8);
}

.panel-section h3 {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: #7e6852;
  margin-bottom: 6px;
}

.section-lead {
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
  margin-bottom: 12px;
}

.form-group {
  margin-bottom: 10px;
}

.form-group label {
  display: block;
  font-size: 11px;
  color: #7a7067;
  margin-bottom: 4px;
  font-weight: 700;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group select {
  width: 100%;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid #d8cdbb;
  border-radius: 10px;
  color: #30404d;
  font-size: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7);
}

.form-group input[type="color"] {
  width: 100%;
  height: 34px;
  padding: 3px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid #d8cdbb;
  border-radius: 10px;
  cursor: pointer;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #94b6df;
  box-shadow: 0 0 0 3px rgba(126, 165, 218, 0.18);
}

.hint-text {
  color: #7e827a;
  font-size: 11px;
  font-style: italic;
}

/* ── Wire colour picker row ───────────────────────────────────────────────── */
.wire-color-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.wire-color-row .form-label {
  font-size: 11px;
  color: var(--muted);
  flex-shrink: 0;
  width: 100%;
  margin-bottom: 2px;
}

.swatch-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  align-items: center;
}

.wire-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  outline: 2px solid rgba(0,0,0,0.18);
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: transform 0.1s, outline-color 0.1s;
}

.wire-swatch:hover {
  transform: scale(1.18);
  outline-color: rgba(0,0,0,0.45);
}

.wire-swatch.active {
  border-color: #fff;
  outline: 3px solid var(--primary);
  transform: scale(1.12);
}

.wire-color-row input[type="color"] {
  width: 28px;
  height: 28px;
  padding: 2px;
  border: 2px solid #d8cdbb;
  border-radius: 50%;
  cursor: pointer;
  background: none;
  flex-shrink: 0;
}

.wire-color-row input[type="color"]:hover {
  border-color: var(--primary);
}

/* ── Wire recolor popup ───────────────────────────────────────────────────── */
.color-popup {
  position: fixed;
  z-index: 200;
  background: var(--surface-strong);
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: 0 8px 24px rgba(60, 50, 30, 0.2);
  min-width: 200px;
}

.color-popup.hidden { display: none; }

.color-popup-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 8px;
}

.color-popup-custom {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
}

.color-popup-custom input[type="color"] {
  width: 26px;
  height: 26px;
  padding: 2px;
  border: 2px solid #d8cdbb;
  border-radius: 50%;
  cursor: pointer;
  background: none;
}

/* ── Wire list ────────────────────────────────────────────────────────────── */
.wire-list-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.15s;
}

.wire-list-item:hover {
  background: rgba(243, 237, 228, 0.95);
  border-color: #dac7ab;
  transform: translateX(2px);
}

.wire-list-item.selected {
  background: rgba(229, 239, 255, 0.92);
  border-color: #a8c0e2;
}

.wire-list-swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8);
}

.wire-list-label {
  flex: 1;
  font-size: 11px;
  font-family: monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.wire-list-del {
  background: none;
  border: none;
  color: #90645e;
  font-size: 12px;
  cursor: pointer;
  padding: 0 3px;
  transition: color 0.1s;
}

.wire-list-del:hover { color: #d45d55; }

/* ── Canvas area ──────────────────────────────────────────────────────────── */
#canvas-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface-strong);
  border: 1px solid rgba(190, 169, 139, 0.6);
  border-radius: 30px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

#canvas-container {
  flex: 1;
  overflow: hidden;
  background:
    radial-gradient(circle at 1px 1px, rgba(125, 146, 164, 0.28) 1px, transparent 0),
    linear-gradient(180deg, #edf3f7, var(--canvas));
  background-size: 18px 18px, auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board-svg {
  display: block;
  width: 100%;
  height: 100%;
  background: transparent;
  filter: drop-shadow(0 26px 40px rgba(115, 102, 78, 0.24));
}

/* ── Status bar ───────────────────────────────────────────────────────────── */
#status-bar {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 16px;
  background: rgba(255, 250, 243, 0.96);
  border-top: 1px solid rgba(215, 207, 191, 0.85);
  font-size: 11px;
  color: #6e6d68;
  flex-shrink: 0;
}

#status-tool {
  color: var(--primary);
  font-weight: 700;
}

#status-board-meta {
  padding: 4px 8px;
  border-radius: 999px;
  background: #f4eee4;
  color: #7a6753;
  border: 1px solid #e1d5c3;
}

#status-coords {
  margin-left: auto;
  font-family: monospace;
  color: var(--success);
  font-weight: 700;
}

/* ── Hole hover / selection styles ───────────────────────────────────────── */
.hole-hover .hole-inner  { fill: #85c8f0 !important; }
.hole-start .hole-inner  { fill: #66c96f !important; }
.hole:hover  .hole-inner { fill: #b7dcff; }

/* ── Selected wire / component highlight ─────────────────────────────────── */
.component.selected rect,
.component.selected ellipse { filter: drop-shadow(0 0 8px rgba(104, 160, 235, 0.9)); }

/* ── Selection panel ──────────────────────────────────────────────────────── */
.sel-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}

.sel-swatch {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 3px;
}

.sel-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.sel-table td {
  padding: 3px 4px;
  border-bottom: 1px solid rgba(215, 207, 191, 0.75);
}

.sel-table td:first-child {
  color: #7a6f65;
  width: 50%;
}

/* ── Mini guide summary table ────────────────────────────────────────────── */
.mini-wire-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.mini-wire-table th {
  color: #6f624f;
  text-align: left;
  padding: 2px 4px;
  border-bottom: 1px solid rgba(215, 207, 191, 0.75);
}

.mini-wire-table td {
  padding: 4px;
  border-bottom: 1px solid rgba(228, 221, 209, 0.8);
}

.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 2px;
  vertical-align: middle;
}

/* ── Guide modal ──────────────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(32, 25, 16, 0.52);
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 24px;
  overflow-y: auto;
}

.modal.hidden { display: none; }

.modal-content {
  background: #fff;
  color: #222;
  border-radius: 18px;
  width: 100%;
  max-width: 760px;
  box-shadow: 0 24px 40px rgba(71, 59, 40, 0.28);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  background: #faf4ea;
  border-bottom: 1px solid #e8dcc9;
}

.modal-header h2 {
  font-size: 16px;
  flex: 1;
}

.modal-close {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #555;
  padding: 0 4px;
}

.modal-close:hover { color: #e00; }

#guide-modal-body {
  padding: 16px 18px;
  font-size: 12px;
  max-height: calc(100vh - 160px);
  overflow-y: auto;
}

/* ── Guide styles ─────────────────────────────────────────────────────────── */
.guide-intro { margin-bottom: 14px; color: #555; font-size: 11px; line-height: 1.5; }

.guide-wire {
  border: 1px solid #ddd;
  border-radius: 10px;
  margin-bottom: 14px;
  overflow: hidden;
}

.guide-wire-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: #f8f4ed;
  border-bottom: 1px solid #eee2d0;
}

.guide-wire-swatch {
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 4px;
  flex-shrink: 0;
}

.guide-wire-title { flex: 1; font-weight: 600; font-size: 13px; }
.guide-wire-length { font-weight: 700; color: #31527b; }
.guide-wire-detail { padding: 10px 12px; }

.guide-strip-info { font-size: 11px; color: #555; margin-bottom: 8px; }

.guide-ruler-wrap { margin: 6px 0 10px; overflow-x: auto; }
.guide-ruler { display: block; }

.guide-events-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.guide-events-table th,
.guide-events-table td {
  border: 1px solid #e0e0e0;
  padding: 4px 8px;
  text-align: left;
}

.guide-events-table th { background: #f4f4f4; font-weight: 600; color: #444; }
.guide-events-table small { display: block; color: #888; font-size: 10px; }
.row-3d td { background: #fff8e1; }
.row-ic td { background: #e8f5e9; }

.guide-no-events { color: #888; font-style: italic; font-size: 11px; }
.guide-empty     { color: #888; font-style: italic; padding: 12px 0; }

/* ── Scrollbar styling ────────────────────────────────────────────────────── */
::-webkit-scrollbar       { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: rgba(244, 235, 220, 0.55); }
::-webkit-scrollbar-thumb { background: #c9baa4; border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: #b59f80; }

/* ── Responsive layout ───────────────────────────────────────────────────── */
@media (max-width: 1100px) {
  html, body {
    overflow: auto;
  }

  #app {
    height: auto;
    min-height: 100vh;
  }

  #toolbar {
    flex-wrap: wrap;
    padding: 10px 14px;
  }

  .toolbar-meta {
    width: 100%;
  }

  .toolbar-actions {
    width: 100%;
    justify-content: flex-end;
  }

  #main-content {
    flex-direction: column;
    padding: 14px;
    overflow: visible;
  }

  aside,
  #right-panel {
    width: 100%;
  }

  #canvas-area,
  aside {
    min-height: 0;
  }

  #canvas-container {
    min-height: 420px;
  }

  #status-bar {
    flex-wrap: wrap;
  }

  #status-coords {
    width: 100%;
    margin-left: 0;
  }
}

@media (max-width: 720px) {
  #toolbar {
    gap: 8px;
    padding: 10px;
  }

  .toolbar-tools,
  .toolbar-meta,
  .toolbar-actions {
    width: 100%;
    flex-wrap: wrap;
  }

  .tool-btn,
  .action-btn {
    flex: 1 1 140px;
    justify-content: center;
  }

  #canvas-container {
    padding: 10px;
  }

  .panel-section {
    padding: 10px;
  }

  .guide-wire-header {
    flex-wrap: wrap;
  }

  .guide-wire-length {
    width: 100%;
    margin-left: 28px;
  }

  #status-board-meta {
    order: 4;
  }
}

/* ── Print ────────────────────────────────────────────────────────────────── */
@media print {
  .modal-header .modal-close,
  .modal-header .action-btn {
    display: none;
  }
}
