:root {
  --bg: #faf6f0;
  --panel: #ffffff;
  --panel-2: #f6f1e9;
  --line: #e8ddcd;
  --text: #2a2320;
  --muted: #8a7d70;
  --maroon: #8a1538;
  --maroon-dark: #6d0f2c;
  --gold: #b8892b;
  --gold-bright: #a9761f;
  --gold-dim: #dcc79a;
  --accent: #8a1538;
  --on-primary: #ffffff;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(90,20,45,.10);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', system-ui, sans-serif;
  display: flex; flex-direction: column;
  height: 100vh; overflow: hidden;
}

/* Topbar — centered logo, Prince Jewellery style */
.topbar {
  height: 74px; flex: none;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 0 22px; border-bottom: 2px solid var(--maroon);
  background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.04);
}
.brand { text-align: center; display: flex; flex-direction: column; align-items: center; }
.brand-logo { height: 46px; width: auto; display: block; }
.brand-sub { color: var(--gold); font-size: 10px; letter-spacing: 3.5px; text-transform: uppercase; margin-top: 3px; }
.nav-btn {
  justify-self: start; background: #fff; border: 1px solid var(--line); color: var(--maroon);
  border-radius: 999px; padding: 9px 16px; font-size: 13px; font-weight: 600; cursor: pointer; transition: .15s;
}
.nav-btn:hover { background: var(--maroon); color: #fff; border-color: var(--maroon); }
.status { justify-self: end; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: #666; }
.status.ok .dot { background: #46c26b; box-shadow: 0 0 8px #46c26b; }
.status.warn .dot { background: #e0a33e; box-shadow: 0 0 8px #e0a33e; }

/* Layout: 75 / 25 */
.layout { flex: 1; display: grid; grid-template-columns: 75% 25%; min-height: 0; }
.stage { overflow-y: auto; border-right: 1px solid var(--line); }
.stage-inner { padding: 28px 32px 60px; max-width: 1100px; margin: 0 auto; }

.stage-head h1 { font-family: 'Cormorant Garamond', serif; font-weight: 700; font-size: 34px; margin: 6px 0 4px; }
.stage-head p { color: var(--muted); margin: 0 0 20px; }

/* Top tools: category select + photo uploader */
.stage-tools {
  display: flex; flex-wrap: wrap; gap: 26px; align-items: flex-start;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 20px; margin-bottom: 20px;
}
.tool-group { display: flex; flex-direction: column; gap: 8px; }
.tool-group > label { font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); }
.tool-group select {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 9px; padding: 10px 14px; font-size: 14px; min-width: 180px; cursor: pointer; outline: none;
}
.tool-group select:focus { border-color: var(--gold-dim); }
.photo-tool { flex: 1; min-width: 240px; }
.photo-status { color: var(--muted); font-size: 12px; align-self: center; }
.photo-zone { margin-top: 10px; max-width: 260px; }

/* Filters */
.filters { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 22px; }
.filter-chip {
  border: 1px solid var(--line); background: var(--panel); color: var(--muted);
  padding: 8px 16px; border-radius: 999px; cursor: pointer; font-size: 13px;
  transition: .18s; text-transform: capitalize;
}
.filter-chip:hover { border-color: var(--maroon); color: var(--maroon); }
.filter-chip.active { background: var(--maroon); color: #fff; border-color: var(--maroon); font-weight: 600; }

/* Product grid */
.product-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 18px; }
.product-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; transition: .2s; display: flex; flex-direction: column;
}
.product-card:hover { transform: translateY(-4px); border-color: var(--maroon); box-shadow: var(--shadow); }
.product-card img { width: 100%; aspect-ratio: 1; object-fit: contain; background: #fff; padding: 14px; border-bottom: 1px solid var(--line); }
.product-card .info { padding: 12px 14px 16px; }
.product-card .name { font-family: 'Cormorant Garamond', serif; font-size: 19px; font-weight: 600; line-height: 1.15; }
.product-card .cat { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; margin-top: 2px; }
.product-card .price { color: var(--gold-bright); font-weight: 600; margin-top: 8px; font-size: 15px; }

/* Drawer */
.drawer {
  margin-top: 30px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 24px; position: relative; box-shadow: var(--shadow);
  animation: rise .3s ease;
}
@keyframes rise { from { opacity: 0; transform: translateY(12px);} to {opacity:1; transform:none;} }
.drawer-close { position: absolute; top: 14px; right: 16px; background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; }
.drawer-close:hover { color: var(--text); }
.drawer-grid { display: grid; grid-template-columns: 320px 1fr; gap: 28px; }

.drawer-preview img { width: 100%; aspect-ratio: 1; object-fit: contain; border-radius: 12px; border: 1px solid var(--line); background: #f7f2ea; padding: 18px; }
.preview-caption h2 { font-family: 'Cormorant Garamond', serif; font-size: 24px; margin: 14px 0 2px; }
.preview-caption .price { color: var(--gold-bright); font-weight: 600; font-size: 18px; }
.preview-caption .muted { color: var(--muted); font-size: 13px; line-height: 1.5; margin-top: 8px; }

.opt-block { margin-bottom: 18px; }
.opt-block > label { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--muted); margin-bottom: 9px; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.opt-chip {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-size: 13px; transition: .15s;
  display: inline-flex; align-items: center; gap: 7px;
}
.opt-chip:hover { border-color: var(--maroon); }
.opt-chip.active { border-color: var(--maroon); background: rgba(138,21,56,.08); color: var(--maroon); font-weight: 600; }
.swatch { width: 13px; height: 13px; border-radius: 50%; border: 1px solid rgba(0,0,0,.2); }
.engine-model { display: block; font-size: 10px; color: var(--muted); margin-top: 2px; letter-spacing: .3px; }

/* Photo zone */
.photo-zone {
  border: 1.5px dashed var(--line); border-radius: 12px; min-height: 120px;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
  overflow: hidden; transition: .15s; background: var(--panel-2);
}
.photo-zone:hover { border-color: var(--gold-dim); }
.photo-empty { text-align: center; color: var(--muted); padding: 20px; }
.photo-empty span { display: block; font-size: 14px; margin-bottom: 4px; }
.photo-empty small { font-size: 11px; opacity: .7; }
#photoPreview { width: 100%; max-height: 240px; object-fit: contain; }
#camVideo { width: 100%; max-height: 260px; object-fit: cover; background: #000; transform: scaleX(-1); }
.photo-actions { display: flex; gap: 8px; margin-bottom: 10px; }
.btn-ghost {
  flex: 1; padding: 10px; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  border-radius: 9px; cursor: pointer; font-size: 13px; transition: .15s;
}
.btn-ghost:hover { border-color: var(--maroon); color: var(--maroon); }
.cam-controls { display: flex; gap: 8px; margin-top: 10px; }
.cam-controls .btn-primary { margin-top: 0; }
.cam-controls .btn-ghost { flex: 0 0 auto; }

.btn-primary {
  width: 100%; margin-top: 6px; padding: 14px; border: none; border-radius: 11px; cursor: pointer;
  background: linear-gradient(180deg, var(--maroon), var(--maroon-dark)); color: var(--on-primary);
  font-weight: 700; font-size: 15px; letter-spacing: .3px; transition: .15s;
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.06); transform: translateY(-1px); }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; }
.hint { color: var(--muted); font-size: 12px; text-align: center; margin-top: 8px; }

/* Complete-the-look */
.look-list { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.look-chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 6px 8px 6px 6px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; font-size: 13px;
}
.look-chip img { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; }
.look-chip.base { border-color: var(--maroon); }
.look-chip .remove { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 14px; line-height: 1; }
.look-chip .remove:hover { color: #e08a8a; }

/* Add-a-piece picker modal */
.picker-overlay { position: fixed; inset: 0; background: rgba(8,9,12,.8); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 60; }
.picker { width: min(680px, 92vw); max-height: 80vh; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.picker-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); font-family: 'Cormorant Garamond', serif; font-size: 20px; }
.picker-head button { background: none; border: none; color: var(--muted); font-size: 18px; cursor: pointer; }
.picker-grid { padding: 18px; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; background: var(--panel-2); }
.picker-card { background: #fff; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; cursor: pointer; transition: .18s; box-shadow: 0 1px 5px rgba(90,20,45,.06); }
.picker-card:hover { border-color: var(--maroon); transform: translateY(-3px); box-shadow: 0 6px 16px rgba(90,20,45,.12); }
.picker-card img { width: 100%; aspect-ratio: 1; object-fit: contain; background: #fff; padding: 10px; border-bottom: 1px solid var(--line); }
.picker-card .pk-name { padding: 8px 10px; font-size: 13px; }
.picker-card .pk-cat { padding: 0 10px 10px; font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }

/* Results */
.results { margin-top: 26px; border-top: 1px solid var(--line); padding-top: 22px; }
.results h3 { font-family: 'Cormorant Garamond', serif; font-size: 22px; margin: 0 0 16px; }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.result-grid img { width: 100%; border-radius: 12px; border: 1px solid var(--line); cursor: zoom-in; transition: .2s; }
.result-grid img:hover { border-color: var(--gold); }
.result-slot {
  aspect-ratio: 1; border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
}
.slot-spinner { width: 34px; height: 34px; border: 3px solid var(--line); border-top-color: var(--gold-bright); border-radius: 50%; animation: spin 1s linear infinite; }
.slot-label { color: var(--muted); font-size: 12px; }
.slot-error { color: #e08a8a; font-size: 13px; text-align: center; padding: 12px; }
.slot-error small { color: var(--muted); }
@media (max-width: 900px) { .drawer-grid { grid-template-columns: 1fr; } .result-grid { grid-template-columns: 1fr; } }

/* Chat */
.chat { display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.chat { border-left: 1px solid var(--line); }
.chat-avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  background: linear-gradient(140deg, var(--maroon), var(--maroon-dark));
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700;
  font-family: 'Cormorant Garamond', serif; font-size: 22px;
}
.chat-title { font-weight: 600; }
.chat-role { color: var(--muted); font-size: 12px; }
.chat-messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: 88%; padding: 11px 14px; border-radius: 14px; font-size: 14px; line-height: 1.5; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: var(--maroon); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot { align-self: flex-start; background: var(--panel-2); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.typing { color: var(--muted); font-style: italic; }
.chat-input { display: flex; gap: 8px; padding: 14px; border-top: 1px solid var(--line); }
.chat-input input {
  flex: 1; background: var(--panel-2); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 12px 14px; font-size: 14px; outline: none;
}
.chat-input input:focus { border-color: var(--gold-dim); }
.chat-input button {
  flex: none; width: 46px; border: none; border-radius: 10px; cursor: pointer; font-size: 16px;
  background: var(--maroon); color: #fff;
}
.chat-input button:hover { filter: brightness(1.06); }

/* Loading overlay */
.loading-overlay { position: fixed; inset: 0; background: rgba(8,9,12,.82); backdrop-filter: blur(4px); display: flex; align-items: center; justify-content: center; z-index: 50; }
.loading-card { text-align: center; }
.spinner { width: 52px; height: 52px; border: 3px solid var(--line); border-top-color: var(--gold-bright); border-radius: 50%; margin: 0 auto 18px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-family: 'Cormorant Garamond', serif; font-size: 22px; }
.loading-sub { color: var(--muted); font-size: 13px; margin-top: 6px; }

/* Lightbox */
.lightbox { position: fixed; inset: 0; z-index: 80; background: rgba(6,7,10,.94); backdrop-filter: blur(6px); display: flex; align-items: center; justify-content: center; }
.lb-stage { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; cursor: grab; }
.lb-stage.panning { cursor: grabbing; }
.lb-stage img { max-width: 92vw; max-height: 82vh; border-radius: 8px; transform-origin: center center; transition: transform .08s ease-out; user-select: none; box-shadow: 0 20px 80px rgba(0,0,0,.6); }
.lb-toolbar { position: absolute; top: 16px; right: 16px; z-index: 2; display: flex; align-items: center; gap: 8px; background: rgba(20,23,29,.85); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.lb-toolbar button { background: var(--panel-2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 7px 12px; font-size: 15px; cursor: pointer; min-width: 38px; }
.lb-toolbar button:hover { border-color: var(--gold-dim); color: var(--gold-bright); }
#lbHistory { min-width: auto; }
/* History modal must sit above the lightbox when opened from the full view */
#historyOverlay { z-index: 90; }
.lb-toolbar #lbZoomLabel { color: var(--muted); font-size: 13px; min-width: 46px; text-align: center; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); z-index: 2; background: rgba(20,23,29,.8); border: 1px solid var(--line); color: var(--text); width: 52px; height: 52px; border-radius: 50%; font-size: 30px; cursor: pointer; display: flex; align-items: center; justify-content: center; }
.lb-nav:hover { border-color: var(--gold); color: var(--gold-bright); }
.lb-prev { left: 20px; } .lb-next { right: 20px; }
.lb-nav[hidden] { display: none; }
.lb-hint { position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%); color: var(--muted); font-size: 12px; background: rgba(20,23,29,.7); padding: 6px 12px; border-radius: 999px; }
.lb-stage img { cursor: zoom-in; }
/* Left strip: the selected jewellery pieces, one by one (medium thumbnails) */
.lb-focus { position: absolute; left: 16px; top: 50%; transform: translateY(-50%); z-index: 3; display: flex; flex-direction: column; gap: 10px; width: 132px; max-height: 88vh; overflow-y: auto; padding: 4px; }
.lb-piece { display: flex; flex-direction: column; align-items: center; gap: 5px; background: rgba(20,23,29,.9); border: 1px solid var(--line); border-radius: 12px; padding: 8px; cursor: pointer; color: var(--text); font-family: inherit; }
.lb-piece img { width: 104px; height: 104px; object-fit: contain; border-radius: 8px; background: #fff; padding: 6px; }
.lb-piece span { font-size: 11px; text-align: center; line-height: 1.2; }
.lb-piece:hover { border-color: var(--gold-dim); color: var(--gold-bright); }
.lb-piece.active { border-color: var(--gold); box-shadow: 0 0 0 1px var(--gold); }
.lb-full { flex-direction: row; justify-content: center; font-size: 12px; padding: 10px; }
.lb-prev { left: 160px; }

.muted { color: var(--muted); }
::-webkit-scrollbar { width: 10px; }
::-webkit-scrollbar-thumb { background: #d9ccbb; border-radius: 6px; border: 2px solid var(--bg); }

/* History modal (SQLite) */
.history-list { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.history-row { display: flex; gap: 14px; align-items: center; background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.history-row img { width: 92px; height: 92px; object-fit: cover; border-radius: 8px; cursor: zoom-in; flex: none; background: #f3ede4; }
.history-info { flex: 1; font-size: 13px; min-width: 0; }
.history-info .h-date { color: var(--muted); font-size: 12px; }
.history-info .h-pieces { margin-top: 4px; line-height: 1.4; }
.history-info .h-prov { color: var(--maroon); font-size: 11px; text-transform: capitalize; margin-top: 3px; }
.history-actions { display: flex; flex-direction: column; gap: 6px; flex: none; }
.history-actions button { border: 1px solid var(--line); background: #fff; color: var(--text); border-radius: 8px; padding: 7px 12px; font-size: 12px; cursor: pointer; }
.history-actions .h-open:hover { border-color: var(--maroon); color: var(--maroon); }
.history-actions .h-del:hover { border-color: #c0392b; color: #c0392b; }
.history-empty { padding: 40px; text-align: center; color: var(--muted); }
.hist-clear { background: none; border: 1px solid var(--line); color: var(--muted); border-radius: 8px; padding: 6px 12px; font-size: 12px; cursor: pointer; margin-right: 10px; }
.hist-clear:hover { border-color: #c0392b; color: #c0392b; }
.picker-head .head-actions { display: flex; align-items: center; }
