/* ============ Facet — feature-wise image editing ============ */
:root {
  --bg: #0b0e14;
  --bg-2: #10141d;
  --panel: #141927;
  --panel-2: #1a2032;
  --border: #232b3f;
  --border-2: #2e3852;
  --text: #e7ecf5;
  --text-dim: #8a94ab;
  --text-faint: #5b657c;
  --accent: #7c5cff;
  --accent-2: #22d3ee;
  --good: #34d399;
  --warn: #fbbf24;
  --bad: #f87171;
  --radius: 14px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(1200px 500px at 85% -10%, rgba(124, 92, 255, 0.14), transparent 60%),
    radial-gradient(900px 400px at -10% 110%, rgba(34, 211, 238, 0.08), transparent 60%),
    var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
a { color: var(--accent-2); }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--border);
  background: rgba(11, 14, 20, 0.7);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 20;
}
.logo { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px; letter-spacing: 0.3px; }
.logo .tag { font-weight: 400; font-size: 12.5px; color: var(--text-faint); margin-left: 4px; letter-spacing: 0.2px; }
.header-actions { display: flex; align-items: center; gap: 10px; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; border-radius: 9px;
  background: var(--panel); border: 1px solid var(--border); color: var(--text-dim);
  transition: 0.15s;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-2); background: var(--panel-2); }

.mode-badge {
  font-size: 11.5px; padding: 4px 10px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text-dim);
  letter-spacing: 0.3px; white-space: nowrap;
}
.mode-badge.demo { color: var(--warn); border-color: rgba(251, 191, 36, 0.35); }
.mode-badge.live { color: var(--good); border-color: rgba(52, 211, 153, 0.35); }
.mode-badge:empty { display: none; }

/* ---------- layout ---------- */
main {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(360px, 0.95fr);
  gap: 20px;
  padding: 20px 22px 28px;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}
@media (max-width: 980px) { main { grid-template-columns: 1fr; } }

/* ---------- dropzone ---------- */
.dropzone {
  border: 2px dashed var(--border-2);
  border-radius: var(--radius);
  background: var(--bg-2);
  min-height: 460px;
  display: flex; align-items: center; justify-content: center;
  transition: border-color 0.2s, background 0.2s;
  outline: none;
}
.dropzone.dragover {
  border-color: var(--accent);
  background: rgba(124, 92, 255, 0.07);
  box-shadow: 0 0 0 4px rgba(124, 92, 255, 0.12) inset;
}
.dz-inner { text-align: center; padding: 40px 30px; max-width: 480px; }
.dz-icon { color: var(--text-faint); margin-bottom: 10px; }
.dz-inner h2 { margin: 6px 0 6px; font-size: 20px; }
.dz-inner p { color: var(--text-dim); font-size: 13.5px; margin: 4px 0 18px; }
.dz-inner kbd {
  background: var(--panel-2); border: 1px solid var(--border-2); border-bottom-width: 2px;
  border-radius: 5px; padding: 1px 6px; font-size: 11.5px; font-family: inherit;
}
.dz-actions { display: flex; gap: 10px; justify-content: center; }
.dz-hint { font-size: 12px !important; color: var(--text-faint) !important; margin-top: 22px !important; }

/* ---------- buttons ---------- */
.primary {
  background: linear-gradient(135deg, var(--accent), #5a8bff);
  color: #fff; border: none; border-radius: 10px;
  padding: 10px 20px; font-weight: 600; font-size: 14px;
  box-shadow: 0 4px 18px rgba(124, 92, 255, 0.35);
  transition: 0.15s;
}
.primary:hover:not(:disabled) { filter: brightness(1.1); transform: translateY(-1px); }
.ghost {
  background: transparent; color: var(--text-dim);
  border: 1px solid var(--border-2); border-radius: 10px;
  padding: 10px 18px; font-weight: 500; font-size: 14px; transition: 0.15s;
}
.ghost:hover:not(:disabled) { color: var(--text); background: var(--panel-2); }

/* ---------- image stage ---------- */
.stage {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-2);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.stage-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap;
}
.version-label { font-size: 12.5px; color: var(--text-dim); font-weight: 600; letter-spacing: 0.2px; }
.stage-tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.tool-btn {
  background: var(--panel); border: 1px solid var(--border); color: var(--text-dim);
  border-radius: 8px; padding: 6px 12px; font-size: 12.5px; font-weight: 500; transition: 0.15s;
}
.tool-btn:hover:not(:disabled) { color: var(--text); border-color: var(--border-2); }
.tool-btn.ghost { background: transparent; }

.compare-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; color: var(--text-dim); cursor: pointer; user-select: none;
  padding: 6px 8px;
}
.compare-toggle input { accent-color: var(--accent); cursor: pointer; }

.canvas-wrap {
  position: relative;
  background:
    linear-gradient(45deg, #0d101a 25%, transparent 25%, transparent 75%, #0d101a 75%),
    linear-gradient(45deg, #0d101a 25%, #12151f 25%, #12151f 75%, #0d101a 75%);
  background-size: 22px 22px;
  background-position: 0 0, 11px 11px;
  min-height: 320px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
/* image stack: current img is in-flow and sizes the stack; prev overlays it */
.img-stack { position: relative; display: inline-block; max-width: 100%; line-height: 0; }
.img-stack img {
  max-width: 100%; max-height: min(66vh, 640px);
  display: block; user-select: none; -webkit-user-drag: none;
}
#imgPrev {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: contain; opacity: 0; pointer-events: none;
}
.canvas-wrap.comparing #imgPrev { opacity: 1; }
.compare-clip { position: relative; }
#compareDivider { position: absolute; inset: 0; cursor: ew-resize; }
.cd-line {
  position: absolute; top: 0; bottom: 0; left: 50%; width: 2px;
  background: linear-gradient(180deg, var(--accent-2), var(--accent));
  box-shadow: 0 0 10px rgba(34, 211, 238, 0.6);
  transform: translateX(-1px);
}
.cd-handle {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--panel-2); border: 1.5px solid var(--accent-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; color: var(--accent-2); box-shadow: 0 2px 12px rgba(0,0,0,0.5);
}

.stage-spinner {
  position: absolute; inset: 0; z-index: 5;
  display: flex; flex-direction: column; gap: 14px;
  align-items: center; justify-content: center;
  background: rgba(8, 10, 16, 0.72); backdrop-filter: blur(3px);
  font-size: 13.5px; color: var(--text-dim); font-weight: 500;
}
.spinner {
  width: 38px; height: 38px; border-radius: 50%;
  border: 3px solid var(--border-2); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.version-strip {
  display: flex; gap: 8px; padding: 10px 12px;
  border-top: 1px solid var(--border);
  overflow-x: auto; min-height: 66px; align-items: center;
}
.version-strip:empty::after {
  content: ""; 
}
.vthumb {
  position: relative; flex: 0 0 auto;
  width: 46px; height: 46px; border-radius: 9px; overflow: hidden;
  border: 2px solid var(--border); cursor: pointer; transition: 0.15s;
  background: #000;
}
.vthumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.vthumb:hover { border-color: var(--border-2); transform: translateY(-2px); }
.vthumb.active { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(124, 92, 255, 0.25); }
.vthumb .vnum {
  position: absolute; bottom: 1px; right: 3px;
  font-size: 10px; font-weight: 700; color: #fff; text-shadow: 0 1px 3px #000;
}

/* ---------- features panel ---------- */
.features-col {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  max-height: calc(100vh - 110px);
  position: sticky; top: 76px;
}
.features-header { padding: 16px 18px 12px; border-bottom: 1px solid var(--border); }
.features-header h2 { margin: 0 0 6px; font-size: 15.5px; letter-spacing: 0.2px; }
.img-description { margin: 0; font-size: 12.5px; color: var(--text-dim); line-height: 1.5; min-height: 19px; }

.features-list { overflow-y: auto; padding: 10px 12px; flex: 1; }
.features-list[aria-disabled="true"] .feature { opacity: 0.45; pointer-events: none; }

.feature {
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-2);
  padding: 10px 12px;
  margin-bottom: 8px;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.feature.modified {
  border-color: rgba(124, 92, 255, 0.6);
  background: rgba(124, 92, 255, 0.06);
  box-shadow: 0 0 0 1px rgba(124, 92, 255, 0.25) inset;
}
.feature.applying { border-color: var(--accent-2); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 4px rgba(34, 211, 238, 0.15); } }

.feature-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.f-icon { color: var(--text-faint); display: inline-flex; }
.feature.modified .f-icon { color: var(--accent); }
.f-name { font-size: 13.5px; font-weight: 600; }
.f-detected {
  margin-left: auto; font-size: 11.5px; color: var(--text-dim);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 2px 9px; border-radius: 999px; max-width: 55%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.feature-control { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

select, .custom-input, .detail-input {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border-2); border-radius: 8px;
  font-size: 13px; padding: 7px 10px;
  outline: none; flex: 1; min-width: 0; font-family: inherit;
  transition: border-color 0.15s;
}
select:focus, .custom-input:focus, .detail-input:focus { border-color: var(--accent); }
select option { background: var(--panel); }

.f-apply {
  flex: 0 0 auto;
  width: 34px; height: 34px; border-radius: 8px;
  background: var(--panel); border: 1px solid var(--border-2); color: var(--text-dim);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 15px; transition: 0.15s;
}
.feature.modified .f-apply {
  background: linear-gradient(135deg, var(--accent), #5a8bff);
  color: #fff; border-color: transparent;
  box-shadow: 0 2px 10px rgba(124, 92, 255, 0.4);
}
.f-apply:hover:not(:disabled) { transform: translateY(-1px); }

/* toggle switch */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; flex: 0 0 auto; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider-ui {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--panel-2); border: 1px solid var(--border-2);
  border-radius: 999px; transition: 0.2s;
}
.slider-ui::before {
  content: ""; position: absolute; height: 16px; width: 16px; left: 3px; top: 3px;
  background: var(--text-dim); border-radius: 50%; transition: 0.2s;
}
.switch input:checked + .slider-ui { background: rgba(124, 92, 255, 0.35); border-color: var(--accent); }
.switch input:checked + .slider-ui::before { transform: translateX(20px); background: #fff; }
.toggle-value { font-size: 12.5px; color: var(--text-dim); min-width: 66px; }
.feature.modified .toggle-value { color: var(--accent-2); font-weight: 600; }

.detail-wrap { width: 100%; margin-top: 8px; }
.detail-wrap label { font-size: 11px; color: var(--text-faint); display: block; margin-bottom: 4px; }

.f-changed-note {
  margin-top: 7px; font-size: 11.5px; color: var(--accent-2); font-weight: 500;
}
.f-changed-note .from { color: var(--text-faint); text-decoration: line-through; margin-right: 6px; }

.features-footer {
  display: flex; gap: 10px; padding: 14px 16px;
  border-top: 1px solid var(--border);
}
.features-footer .primary { flex: 1.4; } .features-footer .ghost { flex: 0.8; }

/* ---------- modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(5, 7, 12, 0.7); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  width: 100%; max-width: 480px;
  background: var(--panel); border: 1px solid var(--border-2);
  border-radius: 16px; box-shadow: var(--shadow);
  max-height: 90vh; overflow-y: auto;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 18px; }
.modal-foot { padding: 14px 20px 18px; display: flex; justify-content: flex-end; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 7px; }
.field .dim { font-weight: 400; color: var(--text-faint); font-size: 12px; }
.field-hint { font-size: 12px; color: var(--text-faint); margin: 7px 0 0; line-height: 1.5; }
.key-row { display: flex; gap: 8px; }
.key-row input { flex: 1; }
#apiKey, .custom-model {
  background: var(--bg-2); color: var(--text);
  border: 1px solid var(--border-2); border-radius: 8px;
  padding: 9px 12px; font-size: 13px; outline: none; font-family: inherit; width: 100%;
}
#apiKey:focus, .custom-model:focus { border-color: var(--accent); }
.custom-model { margin-top: 8px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  z-index: 60; max-width: min(560px, 90vw);
  background: var(--panel-2); border: 1px solid var(--border-2);
  color: var(--text); border-radius: 12px;
  padding: 12px 18px; font-size: 13.5px; line-height: 1.5;
  box-shadow: var(--shadow); animation: toastIn 0.25s ease;
}
.toast.error { border-color: rgba(248, 113, 113, 0.5); color: #fecaca; }
.toast.ok { border-color: rgba(52, 211, 153, 0.5); color: #a7f3d0; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } }

.hidden { display: none !important; }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 5px; }
::-webkit-scrollbar-track { background: transparent; }
