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

:root {
  --sidebar-w:  200px;
  --palette-w:  240px;
  --topbar-h:    44px;
  --toolbar-h:   48px;
  --cell-size:   24px;

  --bg:         #1a1a2e;
  --surface:    #16213e;
  --border:     #0f3460;
  --accent:     #e94560;
  --text:       #eeeeee;
  --muted:      #7a8699;
}

body {
  font-family: system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

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

/* ── Top bar ────────────────────────────────────────────────────────────── */

#topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  flex-shrink: 0;
}

.project-name-group { display: flex; align-items: center; gap: .4rem; }
#project-name-display { font-weight: 600; }
#btn-edit-project  { padding: .15rem .4rem; font-size: .8rem; line-height: 1; }
#btn-project-info  { padding: .15rem .4rem; font-size: .8rem; line-height: 1; }

.topbar-actions { display: flex; gap: .5rem; }

/* ── Main three-column layout ───────────────────────────────────────────── */

#main { display: flex; flex: 1; overflow: hidden; }

/* ── Sidebars ───────────────────────────────────────────────────────────── */

aside {
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

#panel-projects { width: var(--sidebar-w); border-right: 1px solid var(--border); }
#panel-palette  { width: var(--palette-w);  border-left:  1px solid var(--border); }

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .6rem .75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
}

/* ── Palette header controls ────────────────────────────────────────────── */

.palette-name-display {
  font-size: .75rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.palette-ctrl-group         { display: flex; gap: .2rem; flex-shrink: 0; }
.palette-ctrl-group button  { padding: .15rem .4rem; font-size: .72rem; }

/* ── Project list ───────────────────────────────────────────────────────── */

#project-list { list-style: none; overflow-y: auto; flex: 1; }

#project-list li {
  padding: .55rem .75rem;
  cursor: pointer;
  font-size: .88rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#project-list li:hover  { background: rgba(255,255,255,.05); }
#project-list li.active { background: var(--accent); color: #fff; }

/* ── Grid panel ─────────────────────────────────────────────────────────── */

#panel-grid {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#grid-container {
  flex: 1;
  overflow: auto;
  padding: 1rem;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: var(--workspace-bg, #000);
}

#grid-placeholder { color: var(--muted); margin: auto; }

/* ── Grid ───────────────────────────────────────────────────────────────── */

.grid { user-select: none; cursor: crosshair; }

/* pencil */
.grid.tool-paint { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpolygon points='2,12 4,14 14,4 12,2' fill='%23f5c842' stroke='%23222' stroke-width='1'/%3E%3Cpolygon points='12,2 14,4 16,2 14,0' fill='%23f09' stroke='%23222' stroke-width='1'/%3E%3Cpolygon points='0,14 2,12 4,14 2,16' fill='%23bbb' stroke='%23222' stroke-width='0.5'/%3E%3C/svg%3E") 1 13, crosshair; }
/* eraser block */
.grid.tool-erase { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Crect x='1' y='5' width='14' height='8' rx='1' fill='%23fcc' stroke='%23333' stroke-width='1'/%3E%3Crect x='1' y='5' width='5' height='8' rx='1' fill='%23e99' stroke='%23333' stroke-width='1'/%3E%3Cline x1='6' y1='5' x2='6' y2='13' stroke='%23555' stroke-width='1'/%3E%3C/svg%3E") 3 9, cell; }
/* paint bucket */
.grid.tool-fill  { cursor: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpath d='M5,5 C4,2 12,2 11,5' fill='none' stroke='%23333' stroke-width='1.5'/%3E%3Cpath d='M3,5 L13,5 L11,14 L5,14 Z' fill='%23eee' stroke='%23333' stroke-width='1'/%3E%3Cpath d='M3,14 Q2,16 4,16 Q6,16 5,14 Z' fill='%23555'/%3E%3C/svg%3E") 2 14, copy; }

/* Square layout */
.grid.square {
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell-size));
  border-top:  1px solid var(--border);
  border-left: 1px solid var(--border);
}

.grid.square .cell {
  width:  var(--cell-size);
  height: var(--cell-size);
  border-right:  1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

/* Offset (brick) layout */
.grid.offset .grid-row { display: flex; }
.grid.offset .offset-row { margin-left: calc(var(--cell-size) / 2); }

.grid.offset .cell {
  width:  var(--cell-size);
  height: var(--cell-size);
  border-radius: 50%;
  border: 1px solid var(--border);
  margin: 1px;
}

/* ── Toolbar ────────────────────────────────────────────────────────────── */

#toolbar {
  height: var(--toolbar-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  flex-shrink: 0;
}

.tool-group { display: flex; gap: .25rem; }

.view-tabs { margin-left: auto; }

.grid.view-pattern .cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.5);
  font-weight: bold;
  color: #000;
}

/* ── Buttons ────────────────────────────────────────────────────────────── */

button {
  background: var(--border);
  color: var(--text);
  border: none;
  padding: .3rem .7rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: .82rem;
}
button:hover    { background: #1a4a7a; }
button:disabled { opacity: .4; cursor: default; }

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

.btn-danger       { background: #6b1a1a; }
.btn-danger:hover { background: #8f2020; }

/* ── Bead palette ───────────────────────────────────────────────────────── */

#bead-list {
  flex: 1;
  overflow-y: auto;
  padding: .5rem;
  display: flex;
  flex-direction: column;
  gap: .2rem;
}

.palette-empty { font-size: .82rem; color: var(--muted); padding: .5rem; }

.bead-item {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .35rem .4rem;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
}
.bead-item:hover  { background: rgba(255,255,255,.05); }
.bead-item.active { border-color: var(--accent); }

.bead-swatch {
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.2);
  flex-shrink: 0;
}

.bead-info     { flex: 1; min-width: 0; }
.bead-name-row { display: flex; align-items: center; gap: .3rem; }
.bead-name     { font-size: .83rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.bead-size     { font-size: .7rem;  color: var(--muted); }

.bead-pattern {
  font-family: monospace;
  font-size: .68rem;
  font-weight: 700;
  background: rgba(233, 69, 96, .15);
  color: var(--accent);
  border: 1px solid rgba(233, 69, 96, .3);
  border-radius: 3px;
  padding: 0 .22rem;
  flex-shrink: 0;
}

.bead-delete,
.bead-complement,
.bead-duplicate {
  padding: .1rem .3rem;
  font-size: .8rem;
  line-height: 1;
  flex-shrink: 0;
}

/* ── Split button + dropdown ────────────────────────────────────────────── */

.btn-split-group {
  position: relative;
  display: flex;
}

.btn-split-group > button:first-child {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.btn-split-group > #btn-bead-new-arrow {
  padding: .18rem .3rem;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-left: 1px solid rgba(255,255,255,.15);
  font-size: .65rem;
}

.dropdown-menu {
  position: absolute;
  bottom: calc(100% + 4px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: .3rem 0;
  display: flex;
  flex-direction: column;
  min-width: 120px;
  z-index: 50;
  box-shadow: 0 4px 12px rgba(0,0,0,.5);
}

.dropdown-menu.hidden { display: none; }

.dropdown-menu button {
  background: none;
  border: none;
  color: var(--text);
  padding: .28rem .75rem;
  text-align: left;
  font-size: .82rem;
  cursor: pointer;
  border-radius: 0;
}

.dropdown-menu button:hover { background: rgba(255,255,255,.07); }

.dropdown-menu hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: .25rem 0;
}

/* ── Bead editor ────────────────────────────────────────────────────────── */

#bead-editor {
  padding: .7rem;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: .45rem;
  flex-shrink: 0;
}

.bead-editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#be-title {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
}

#bead-editor label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: .82rem;
  gap: .5rem;
}

#bead-editor input {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 3px;
  padding: .2rem .4rem;
  font-size: .82rem;
  width: 90px;
}

#bead-editor input[type="color"] {
  width: 40px; height: 26px;
  padding: 1px;
  cursor: pointer;
}

.be-actions { display: flex; gap: .5rem; }
.be-actions button { flex: 1; font-size: .78rem; }

/* ── Modal overlay ──────────────────────────────────────────────────────── */

#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.5rem;
  min-width: 300px;
  display: flex;
  flex-direction: column;
  gap: .9rem;
}
.modal.hidden { display: none; }

.modal h2 { font-size: .95rem; }

.modal label {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  font-size: .84rem;
}

.modal input, .modal select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 4px;
  padding: .35rem .5rem;
  font-size: .88rem;
}

.modal-actions {
  display: flex;
  gap: .5rem;
  justify-content: flex-end;
  margin-top: .25rem;
}

#confirm-message { font-size: .88rem; line-height: 1.5; }

/* ── Link palette modal ─────────────────────────────────────────────────── */

.modal-hint { font-size: .8rem; color: var(--muted); line-height: 1.4; }

.lp-list {
  display: flex;
  flex-direction: column;
  gap: .2rem;
  max-height: 240px;
  overflow-y: auto;
}
.lp-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .4rem .6rem;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  font-size: .84rem;
}
.lp-item:hover  { background: rgba(255,255,255,.05); }
.lp-item.active { border-color: var(--accent); }
.lp-item-meta   { font-size: .72rem; color: var(--muted); }

/* ── Import / export modals ─────────────────────────────────────────────── */

.modal-checklist {
  display: flex;
  flex-direction: column;
  gap: .15rem;
  max-height: 280px;
  overflow-y: auto;
}
.modal-checklist.hidden { display: none; }

label.modal-checklist-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: .6rem;
  padding: .4rem .5rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: .88rem;
}
label.modal-checklist-item:hover { background: rgba(255,255,255,.05); }
label.modal-checklist-item input[type="checkbox"] { accent-color: var(--accent); cursor: pointer; }

.modal-error {
  font-size: .84rem;
  color: #e97070;
  background: rgba(233,112,112,.08);
  border: 1px solid rgba(233,112,112,.25);
  border-radius: 4px;
  padding: .5rem .7rem;
  line-height: 1.5;
}
.modal-error.hidden { display: none; }

/* ── User settings modal ────────────────────────────────────────────────── */

.us-add-color-row        { display: flex; gap: .5rem; align-items: center; }
.us-add-color-row.hidden { display: none; }
.us-add-color-row input[type="color"] { width: 40px; height: 28px; padding: 1px; cursor: pointer; }

/* ── Project info modal ─────────────────────────────────────────────────── */

.pi-section-label { font-size: .72rem; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin-bottom: .2rem; }
.pi-size-text     { font-size: .85rem; line-height: 1.6; }
.pi-bead-list   { display: flex; flex-direction: column; gap: .15rem; max-height: 260px; overflow-y: auto; }
.pi-bead-row    { display: flex; align-items: center; gap: .5rem; font-size: .84rem; }
.pi-bead-swatch { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(255,255,255,.2); flex-shrink: 0; }
.pi-bead-count  { color: var(--muted); min-width: 2.5rem; text-align: right; flex-shrink: 0; }
.pi-total       { font-size: .82rem; color: var(--muted); border-top: 1px solid var(--border); padding-top: .4rem; }
