:root {
  --lego-red:    #d01012;
  --lego-yellow: #ffcf00;
  --lego-blue:   #006cb7;
  --lego-green:  #00852b;
  --lego-black:  #1b1b1b;
  --ink:         #1b1b1b;
  --paper:       #f4f6f8;
  --card:        #ffffff;
  --line:        #e2e6ea;
  --shadow:      0 4px 0 rgba(0,0,0,.12), 0 8px 24px rgba(0,0,0,.10);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Nunito', system-ui, -apple-system, sans-serif;
  background:
    radial-gradient(circle at 18px 18px, rgba(0,0,0,.04) 3px, transparent 4px) 0 0 / 36px 36px,
    var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }

/* ---------- LEGO logo brick ---------- */
.brick-logo {
  display: inline-grid; place-items: center;
  width: 56px; height: 56px;
  background: var(--lego-red);
  border-radius: 10px;
  box-shadow: inset 0 -5px 0 rgba(0,0,0,.18), 0 3px 0 rgba(0,0,0,.2);
  position: relative;
}
.brick-logo span {
  font-weight: 900; color: #fff; font-size: 15px; letter-spacing: .5px;
  text-shadow: 0 1px 0 rgba(0,0,0,.25);
}
.brick-logo.big { width: 84px; height: 84px; border-radius: 14px; margin: 0 auto 10px; }
.brick-logo.big span { font-size: 22px; }

/* ---------- Buttons ---------- */
.btn {
  font-family: inherit; font-weight: 800; font-size: 14px;
  border: none; border-radius: 10px; padding: 10px 16px; color: #fff;
  cursor: pointer; transition: transform .05s ease, filter .15s ease;
  box-shadow: 0 4px 0 rgba(0,0,0,.22);
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.22); }
.btn:hover { filter: brightness(1.05); }
.btn-red    { background: var(--lego-red); }
.btn-yellow { background: var(--lego-yellow); color: var(--ink); }
.btn-blue   { background: var(--lego-blue); }
.btn-green  { background: var(--lego-green); }
.btn-ghost  { background: #e8ecef; color: var(--ink); box-shadow: 0 4px 0 rgba(0,0,0,.10); }
.btn-block  { width: 100%; }

/* ---------- Auth ---------- */
.auth-screen {
  min-height: 100vh; display: grid; place-items: center; padding: 24px;
}
.auth-card {
  background: var(--card); width: 100%; max-width: 380px;
  border-radius: 18px; padding: 32px 28px; box-shadow: var(--shadow);
  border-top: 8px solid var(--lego-yellow);
}
.auth-title { text-align: center; margin: 4px 0 2px; font-size: 26px; font-weight: 900; }
.auth-sub { text-align: center; margin: 0 0 20px; color: #6b7480; font-weight: 700; font-size: 14px; }
.auth-tabs { display: flex; gap: 6px; background: #eef1f4; padding: 5px; border-radius: 12px; margin-bottom: 18px; }
.auth-tab {
  flex: 1; border: none; background: transparent; padding: 9px; border-radius: 9px;
  font-family: inherit; font-weight: 800; cursor: pointer; color: #6b7480;
}
.auth-tab.active { background: #fff; color: var(--ink); box-shadow: 0 2px 6px rgba(0,0,0,.08); }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form input {
  font-family: inherit; font-size: 15px; padding: 12px 14px;
  border: 2px solid var(--line); border-radius: 10px; outline: none;
}
.auth-form input:focus { border-color: var(--lego-blue); }
.auth-error { color: var(--lego-red); font-weight: 700; text-align: center; min-height: 20px; margin: 12px 0 0; font-size: 14px; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px; background: var(--lego-yellow);
  box-shadow: 0 4px 0 rgba(0,0,0,.12); position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-title { font-weight: 900; font-size: 20px; line-height: 1; }
.brand-sub { font-weight: 700; font-size: 12px; color: #7a6a00; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-label { font-weight: 800; font-size: 14px; }

/* ---------- Layout ---------- */
.app { max-width: 1180px; margin: 0 auto; padding: 22px; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
.stat { border-radius: 14px; padding: 18px; color: #fff; box-shadow: var(--shadow); position: relative; overflow: hidden; }
.stat::before {
  content: ""; position: absolute; top: 10px; right: 14px; width: 26px; height: 26px;
  border-radius: 50%; background: rgba(255,255,255,.25);
  box-shadow: 34px 0 0 rgba(255,255,255,.25), 0 0 0 6px rgba(255,255,255,.08);
}
.stat-num { font-size: 34px; font-weight: 900; line-height: 1; }
.stat-label { font-weight: 800; font-size: 13px; opacity: .95; margin-top: 6px; }
.stat-red { background: var(--lego-red); }
.stat-yellow { background: var(--lego-yellow); color: var(--ink); }
.stat-yellow::before { background: rgba(0,0,0,.12); box-shadow: 34px 0 0 rgba(0,0,0,.12); }
.stat-blue { background: var(--lego-blue); }
.stat-green { background: var(--lego-green); }

/* ---------- Panel ---------- */
.panel { background: var(--card); border-radius: 16px; padding: 20px 22px; box-shadow: var(--shadow); margin-bottom: 18px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.panel-head h2 { margin: 0; font-size: 19px; font-weight: 900; }
.studs { display: flex; gap: 7px; }
.studs i { width: 16px; height: 16px; border-radius: 50%; background: var(--lego-red);
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.2); }
.studs i:nth-child(2){ background: var(--lego-yellow); }
.studs i:nth-child(3){ background: var(--lego-blue); }
.studs i:nth-child(4){ background: var(--lego-green); }

.lookup-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  background: #eef6fb; border: 2px dashed var(--lego-blue); border-radius: 12px; padding: 12px; margin-bottom: 18px; }
.lookup-row input { font-family: inherit; padding: 10px 12px; border: 2px solid var(--line); border-radius: 9px; outline: none; }
#lookup-part { width: 220px; } #lookup-color { width: 160px; }
.lookup-status { font-weight: 700; font-size: 13px; }

/* Available-colours palette (after a fetch) */
.part-colors:not(:empty) { margin: 0 0 16px; padding: 12px 14px; background: #fafbfc; border: 2px solid var(--line); border-radius: 12px; }
.pc-label { font-weight: 800; font-size: 13px; color: #5a636d; }
.pc-swatches { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 9px; }
.pc-swatch {
  width: 26px; height: 26px; border-radius: 7px; border: 2px solid rgba(0,0,0,.2);
  cursor: pointer; padding: 0; transition: transform .08s ease;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.15);
}
.pc-swatch:hover { transform: scale(1.18); }
.pc-swatch.selected { border-color: var(--lego-blue); box-shadow: 0 0 0 2px var(--lego-blue), inset 0 -3px 0 rgba(0,0,0,.15); }

.item-form { display: flex; gap: 22px; align-items: flex-start; }
.form-grid { flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field-wide { grid-column: span 3; }
.field label { font-weight: 800; font-size: 12px; color: #5a636d; text-transform: uppercase; letter-spacing: .4px; }
.field input { font-family: inherit; font-size: 15px; padding: 10px 12px; border: 2px solid var(--line); border-radius: 10px; outline: none; }
.field input:focus { border-color: var(--lego-blue); }
.form-actions { grid-column: 1 / -1; display: flex; gap: 10px; }

/* Big image on the right */
.form-image { width: 240px; flex: none; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.big-preview {
  width: 240px; height: 240px; border: 3px solid var(--line); border-radius: 16px;
  display: grid; place-items: center; overflow: hidden; background: #fafbfc; position: relative;
}
.big-preview img { max-width: 92%; max-height: 92%; display: none; }
.big-preview img[src]:not([src=""]) { display: block; }
.img-placeholder { font-size: 64px; opacity: .25; }
.big-preview img[src]:not([src=""]) ~ .img-placeholder { display: none; }
.img-status { font-weight: 800; font-size: 13px; min-height: 18px; color: var(--lego-blue); text-align: center; }

/* ---------- Colour picker ---------- */
.color-input { position: relative; display: flex; align-items: center; }
.color-input .swatch {
  position: absolute; left: 10px; width: 18px; height: 18px; border-radius: 5px;
  border: 1px solid rgba(0,0,0,.2); background: #fff; pointer-events: none;
}
.color-input input { padding-left: 36px !important; width: 100%; }
.color-cell { display: inline-flex; align-items: center; gap: 7px; }
.tbl-swatch { width: 14px; height: 14px; border-radius: 4px; border: 1px solid rgba(0,0,0,.25); flex: none; }

/* searchable colour dropdown with swatches */
.color-input .combo { flex: 1; }
.combo { position: relative; }
.combo-panel {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 60;
  background: #fff; border: 2px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 26px rgba(0,0,0,.16); max-height: 250px; overflow-y: auto;
}
.combo-opt { display: flex; align-items: center; gap: 9px; padding: 8px 12px; cursor: pointer; font-weight: 700; font-size: 14px; }
.combo-opt:hover, .combo-opt.active { background: #eef6fb; }
.combo-opt .sw { width: 16px; height: 16px; border-radius: 4px; border: 1px solid rgba(0,0,0,.25); flex: none; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: grid; place-items: center; z-index: 100; padding: 20px;
}
.modal {
  background: #fff; width: 100%; max-width: 520px; border-radius: 16px;
  padding: 22px 24px; box-shadow: var(--shadow); border-top: 8px solid var(--lego-blue);
  max-height: 90vh; overflow: auto;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-weight: 900; }
.modal-sub { color: #5a636d; font-weight: 700; font-size: 14px; line-height: 1.5; }
.drop-zone {
  display: grid; place-items: center; text-align: center; cursor: pointer;
  border: 2px dashed var(--lego-blue); border-radius: 12px; padding: 26px 16px;
  background: #eef6fb; font-weight: 800; color: #3a4a57; margin: 8px 0 14px;
}
.drop-zone.dragover { background: #d9ecff; }
.paste-details summary { font-weight: 800; cursor: pointer; color: var(--lego-blue); }
.paste-details textarea {
  width: 100%; min-height: 120px; margin-top: 10px; font-family: ui-monospace, monospace;
  font-size: 12px; border: 2px solid var(--line); border-radius: 10px; padding: 10px; outline: none; resize: vertical;
}
.enrich-row { display: flex; gap: 9px; align-items: flex-start; margin: 14px 0 4px; font-weight: 700; font-size: 14px; }
.enrich-row em { color: #98a1ab; font-style: normal; font-weight: 700; }
.import-result { font-weight: 700; font-size: 14px; margin: 8px 0; white-space: pre-line; }
.import-result .err { color: var(--lego-red); font-size: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }

/* ---------- Build list ---------- */
.build-panel { border-top: 8px solid var(--lego-red); }
.build-count { font-size: 14px; font-weight: 800; color: #8a939d; }
.build-hint { margin: 0 0 12px; color: #5a636d; font-weight: 700; font-size: 14px; }
.build-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.build-row {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px;
  border: 2px solid var(--line); border-radius: 12px; background: #fafbfc;
}
.build-row.short { border-color: var(--lego-red); background: #fff3f3; }
.build-img .cell-img, .build-img .cell-img-empty { width: 44px; height: 44px; }
.build-info { flex: 1; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.build-sub { font-size: 13px; color: #6b7480; font-weight: 700; }
.build-qty { display: flex; align-items: center; gap: 8px; }
.build-qty-input {
  width: 64px; font-family: inherit; font-size: 15px; font-weight: 800; text-align: center;
  padding: 8px; border: 2px solid var(--line); border-radius: 9px; outline: none;
}
.build-qty-input:focus { border-color: var(--lego-blue); }
.build-avail { font-size: 13px; font-weight: 700; color: #6b7480; white-space: nowrap; }
.build-avail.short { color: var(--lego-red); }
.build-remove { padding: 6px 9px; }
.build-msg { font-weight: 800; font-size: 14px; margin: 6px 0; min-height: 18px; }
.build-msg.ok { color: var(--lego-green); }
.build-msg.err { color: var(--lego-red); }
.icon-btn.use:hover { background: #d7f0df; }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 14px; }
#search { flex: 1; font-family: inherit; font-size: 15px; padding: 12px 16px; border: 2px solid var(--line); border-radius: 12px; outline: none; }
#search:focus { border-color: var(--lego-blue); }
.toolbar-actions { display: flex; gap: 10px; }

/* ---------- Table ---------- */
.table-wrap { background: var(--card); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.inv-table { width: 100%; border-collapse: collapse; }
.inv-table th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .5px;
  padding: 14px 12px; background: var(--lego-black); color: #fff; font-weight: 800;
}
.inv-table th[data-sort] { cursor: pointer; user-select: none; }
.inv-table th[data-sort]:hover { color: var(--lego-yellow); }
.inv-table td { padding: 10px 12px; border-bottom: 1px solid var(--line); font-weight: 600; font-size: 14px; vertical-align: middle; }
.inv-table tbody tr:hover { background: #fffbe6; }
.cell-img { width: 52px; height: 52px; border-radius: 8px; border: 1px solid var(--line); object-fit: contain; background: #fafbfc; cursor: zoom-in; }
.cell-img:hover { border-color: var(--lego-blue); }
.check-col { width: 38px; text-align: center; }
.check-col input { width: 18px; height: 18px; cursor: pointer; }

/* Bulk selection bar */
.bulk-bar {
  display: flex; align-items: center; gap: 12px; margin-bottom: 12px;
  background: #fff3f3; border: 2px solid var(--lego-red); border-radius: 12px; padding: 10px 14px;
}
.bulk-bar #bulk-count { font-weight: 900; color: var(--lego-red); margin-right: auto; }

/* Image lightbox */
.img-modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.8); z-index: 200;
  display: grid; place-items: center; padding: 30px; cursor: zoom-out;
}
.img-modal img {
  max-width: 90vw; max-height: 90vh; border-radius: 14px; background: #fff;
  padding: 14px; box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.cell-img-empty { width: 52px; height: 52px; border-radius: 8px; background: #f0f2f4; display: grid; place-items: center; color: #c2c8ce; font-size: 22px; }
.item-num { font-weight: 900; }
.qty-cell { display: flex; align-items: center; gap: 6px; }
.qty-val { min-width: 28px; text-align: center; font-weight: 900; font-size: 16px; }
.qty-btn { width: 26px; height: 26px; border: none; border-radius: 7px; cursor: pointer; font-weight: 900; color: #fff; box-shadow: 0 3px 0 rgba(0,0,0,.2); }
.qty-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.2); }
.qty-plus { background: var(--lego-green); } .qty-minus { background: var(--lego-red); }
.src-tag { font-size: 11px; font-weight: 800; padding: 3px 8px; border-radius: 20px; background: #eef1f4; color: #5a636d; }
.src-tag.BrickLink { background: #ffe9c7; color: #8a5a00; }
.src-tag.Rebrickable { background: #d9ecff; color: #0a4d8c; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn { border: none; background: #eef1f4; border-radius: 8px; padding: 7px 9px; cursor: pointer; font-size: 14px; }
.icon-btn:hover { background: #dfe4e9; }
.icon-btn.del:hover { background: #ffd9da; }

.empty-state { text-align: center; padding: 48px 20px; color: #8a939d; }
.empty-state .brick-logo { margin: 0 auto 14px; }
.empty-state p { font-weight: 800; font-size: 16px; }

.footer { text-align: center; color: #98a1ab; font-weight: 700; font-size: 13px; padding: 26px 0 8px; }
.footer code { background: #e8ecef; padding: 2px 6px; border-radius: 5px; }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .item-form { flex-direction: column-reverse; }
  .form-image { width: 100%; }
  .big-preview { width: 100%; max-width: 280px; height: 280px; }
  .form-grid { grid-template-columns: repeat(2, 1fr); }
  .field-wide { grid-column: span 2; }
}
@media (max-width: 560px) {
  .form-grid { grid-template-columns: 1fr; }
  .field-wide { grid-column: span 1; }
  .table-wrap { overflow-x: auto; }
}
