:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #f1f3f9;
  --border: #e2e6ee;
  --border-strong: #cdd3df;
  --text: #1c2333;
  --muted: #6b7385;
  --accent: #2563eb;
  --accent-strong: #1d4ed8;
  --good: #15803d;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 1px 1px rgba(15, 23, 42, 0.03);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; }
.logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px; height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, #2563eb 0%, #6d28d9 60%, #db2777 100%);
  box-shadow: 0 4px 10px -2px rgba(37, 99, 235, 0.35), inset 0 1px 0 rgba(255,255,255,0.2);
  color: #fff;
  flex-shrink: 0;
}
.logo svg { width: 18px; height: 18px; }
.brand { font-weight: 700; letter-spacing: -0.01em; }
.topbar-meta { display: flex; align-items: center; gap: 14px; }
.pill {
  font-size: 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 999px;
  color: var(--muted);
}
.link { color: var(--accent); text-decoration: none; font-size: 13px; cursor: pointer; }
.link:hover { text-decoration: underline; }

.layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 14px;
  padding: 14px;
  height: calc(100vh - 49px);
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.side {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
}
.side h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 0 0 6px; }

#job-form { display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 500; }
.field input, .field select, .field textarea, #chat-input {
  background: var(--panel);
  border: 1.5px solid var(--border-strong);
  color: var(--text);
  padding: 11px 13px;
  border-radius: 8px;
  font: inherit;
  font-size: 14px;
  resize: vertical;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.field input:hover:not(:focus), .field select:hover:not(:focus), .field textarea:hover:not(:focus), #chat-input:hover:not(:focus) {
  border-color: #b7bdcc;
}
.field input:focus, .field select:focus, .field textarea:focus, #chat-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
  background: #fcfdff;
}
.field input::placeholder, .field textarea::placeholder, #chat-input::placeholder {
  color: #98a0b3;
}

.dims {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin: 0;
  background: var(--panel-2);
}
.dims legend { font-size: 12px; color: var(--muted); padding: 0 6px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.dims label { display: flex; flex-direction: column; gap: 6px; }
.dims label span { font-size: 11px; color: var(--muted); font-weight: 500; }
.dims label input {
  background: var(--panel);
  border: 1.5px solid var(--border-strong);
  border-radius: 8px;
  padding: 10px 8px;
  font: inherit;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  text-align: center;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
.dims label input::-webkit-outer-spin-button,
.dims label input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.dims label input[type=number] {
  -moz-appearance: textfield;
}
.dims label input:hover:not(:focus) { border-color: #b7bdcc; }
.dims label input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.14);
}
.dims.auto label input { opacity: 0.35; pointer-events: none; }
.dims.auto label span::after { content: " — auto"; color: var(--accent); font-weight: 600; font-size: 10px; }

.auto-size-toggle {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.auto-size-toggle input { margin-top: 1px; cursor: pointer; }
.auto-size-toggle span b { color: var(--text); }
.auto-size-toggle:has(input:checked) { background: rgba(37, 99, 235, 0.08); border-color: var(--accent); }

.actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}
.actions + .actions {
  margin-top: 4px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}
button {
  cursor: pointer;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 6px;
  font: inherit;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover:not(:disabled) { background: var(--panel-2); border-color: var(--muted); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover:not(:disabled) { background: var(--accent-strong); border-color: var(--accent-strong); }
button.ghost {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 8px 12px;
  font-size: 13px;
}
button.ghost:hover:not(:disabled) {
  color: var(--text);
  background: var(--panel);
  border-color: var(--border-strong);
}
button.danger { color: var(--bad); }
button.danger:hover:not(:disabled) { background: #fef2f2; border-color: #fecaca; }

.hint { font-size: 12px; color: var(--muted); margin: 0; }

.chat { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--border); padding-top: 14px; }
.messages {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 280px;
  overflow-y: auto;
}
.msg { padding: 6px 10px; border-radius: 6px; line-height: 1.4; font-size: 13px; }
.msg.user { background: #e0ecff; align-self: flex-end; max-width: 90%; }
.msg.assistant { background: var(--panel-2); align-self: flex-start; max-width: 90%; color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 11px; white-space: pre-wrap; }
#chat-form { display: flex; gap: 8px; }
#chat-input { flex: 1; }

.viewer { display: flex; flex-direction: column; }
#viewer-host { position: relative; flex: 1; background: linear-gradient(180deg, #ffffff 0%, #eef1f8 100%); }
#viewer-placeholder {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; color: var(--muted); text-align: center; padding: 20px;
}
#viewer-placeholder h2 { color: var(--text); margin: 0; font-weight: 500; }
#viewer-canvas-host { position: absolute; inset: 0; }
.viewer-status {
  /* Float as a card in the top-left of the viewer — wireframe stays visible. */
  position: absolute;
  top: 16px; left: 16px; right: 16px;
  z-index: 10;
  font-size: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  color: var(--text);
  padding: 14px 16px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 30px -12px rgba(15, 23, 42, 0.25);
  max-height: calc(100% - 32px);
  overflow: hidden;
}
.viewer-status .busy-header {
  display: flex; align-items: center; gap: 12px;
  flex-wrap: wrap;
}
.viewer-status .busy-msg { font-weight: 500; flex: 1; min-width: 0; }
.viewer-status .busy-elapsed { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.viewer-status .busy-stream {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 11.5px;
  line-height: 1.5;
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 8px;
  padding: 12px 14px;
  margin: 0;
  width: 100%;
  max-height: 240px;
  min-height: 60px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid #1e293b;
  text-align: left;
  box-sizing: border-box;
}
.viewer-status .busy-tip {
  font-size: 13px;
  color: var(--muted);
  max-width: 46ch;
  text-align: center;
  line-height: 1.45;
}
.viewer-status .busy-cancel {
  background: var(--panel);
  border: 1px solid var(--border-strong);
  padding: 8px 18px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  font-weight: 500;
}
.viewer-status .busy-cancel:hover { background: var(--panel-2); }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.viewer-meta {
  display: flex;
  gap: 18px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  background: var(--panel-2);
}

/* Make sure the [hidden] attribute beats any author-set display rule. */
[hidden] { display: none !important; }

.drawer {
  position: fixed;
  top: 0; right: 0;
  width: 380px;
  max-width: 100vw;
  height: 100vh;
  background: var(--panel);
  border-left: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.08);
}
.drawer header { display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.drawer header h3 { margin: 0; font-size: 14px; }
.drawer header button.close {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 20px;
  line-height: 1;
  width: 32px; height: 32px;
  padding: 0;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.drawer header button.close:hover { background: var(--border); }
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.18);
  z-index: 49;
}
#history-list { list-style: none; margin: 0; padding: 8px; overflow-y: auto; flex: 1; }
#history-list li { padding: 10px 12px; border-radius: 6px; border: 1px solid var(--border); margin-bottom: 6px; background: var(--panel); }
#history-list li:hover { background: var(--panel-2); }
#history-list li .row-main { cursor: pointer; }
#history-list li .name { font-size: 13px; font-weight: 600; color: var(--text); }
#history-list li .name.placeholder { color: var(--muted); font-weight: 400; font-style: italic; }
#history-list li .prompt { font-size: 12px; color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 2px; }
#history-list li .meta { font-size: 11px; color: var(--muted); margin-top: 4px; display: flex; gap: 10px; flex-wrap: wrap; }
#history-list li .row-actions { display: flex; gap: 4px; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
#history-list li .row-actions a, #history-list li .row-actions button {
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 4px;
  text-decoration: none;
}
#history-list li .row-actions a { color: var(--accent); border: 1px solid var(--border); background: var(--panel); }
#history-list li .row-actions a:hover { background: var(--panel-2); }
.status-pill { font-size: 10px; padding: 1px 6px; border-radius: 4px; }
.status-pill.ready { background: rgba(21, 128, 61, 0.12); color: var(--good); }
.status-pill.failed { background: rgba(220, 38, 38, 0.12); color: var(--bad); }
.status-pill.generating { background: rgba(37, 99, 235, 0.12); color: var(--accent); }

/* Settings panel uses .drawer; specific bits: */
.settings-body { padding: 14px 16px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 12px; }
.settings-body label { font-size: 12px; color: var(--muted); }
.settings-body textarea {
  width: 100%;
  min-height: 360px;
  background: var(--panel);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 10px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  resize: vertical;
}
.settings-body textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.settings-actions { display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border); }

.printers-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.printer-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-2);
  font-size: 13px;
}
.printer-row.is-default { border-color: var(--accent); background: rgba(37, 99, 235, 0.05); }
.printer-row .printer-name { flex: 1; font-weight: 600; }
.printer-row .printer-dims { color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.printer-row button { padding: 4px 8px; font-size: 11px; }
#add-printer-panel { margin-top: 8px; }
#add-printer-panel summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--accent);
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px dashed var(--border-strong);
  border-radius: 6px;
  list-style: none;
  user-select: none;
}
#add-printer-panel summary::-webkit-details-marker { display: none; }
#add-printer-panel[open] summary { background: var(--panel); border-style: solid; }
.printer-form { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; padding: 12px; background: var(--panel-2); border-radius: 8px; border: 1px solid var(--border); }
.printer-form-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.printer-form input, .printer-form select { background: var(--panel); border: 1px solid var(--border-strong); padding: 8px 10px; border-radius: 6px; font: inherit; }
.printer-form select:focus, .printer-form input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.printer-form button { padding: 10px 12px; align-self: flex-start; }

.dims input.over-limit { border-color: var(--bad); background: #fef2f2; }

/* Printer field — show name + dims as a tidy card */
#printer-field { gap: 6px; }
#printer-info {
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  color: var(--text);
  font-weight: 500;
}
#printer-info:empty { display: none; }
#printer-select { padding: 8px 10px; }
#dims-warning { padding: 8px 10px; background: #fef2f2; border-radius: 6px; border: 1px solid #fecaca; }

/* Upload drop zone */
.upload-dropzone {
  position: relative;
  border: 2px dashed var(--border-strong);
  border-radius: 10px;
  padding: 18px 14px;
  background: var(--panel-2);
  text-align: center;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.upload-dropzone:hover { border-color: var(--accent); background: rgba(37, 99, 235, 0.04); }
.upload-dropzone.dragover { border-color: var(--accent); border-style: solid; background: rgba(37, 99, 235, 0.08); }
.upload-prompt b { display: block; font-size: 14px; color: var(--text); margin-bottom: 4px; }
.upload-prompt span { font-size: 12px; color: var(--muted); }
.upload-prompt a { color: var(--accent); text-decoration: underline; cursor: pointer; }
#upload-progress {
  font-size: 13px;
  color: var(--accent);
  font-weight: 500;
}

.attached-uploads { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.attached-upload {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}
.attached-upload .thumb {
  flex: 0 0 36px;
  width: 36px; height: 36px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  overflow: hidden;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 11px; color: var(--muted); font-weight: 600;
}
.attached-upload .thumb img { width: 100%; height: 100%; object-fit: cover; }
.attached-upload .info { flex: 1; min-width: 0; }
.attached-upload .info .name { font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attached-upload .info .meta { font-size: 11px; color: var(--muted); }
.attached-upload button {
  background: transparent; border: none; color: var(--muted); cursor: pointer;
  font-size: 18px; line-height: 1; padding: 0 4px;
}
.attached-upload button:hover { color: var(--bad); }

.history-preview-popover {
  position: fixed;
  width: 240px;
  height: 200px;
  background: linear-gradient(180deg, #ffffff 0%, #eef1f8 100%);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: 0 20px 40px -12px rgba(15, 23, 42, 0.25);
  z-index: 100;
  overflow: hidden;
  pointer-events: none;
}
.history-preview-popover canvas {
  display: block;
  width: 240px !important;
  height: 200px !important;
}
.preview-spinner {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 24px; height: 24px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .viewer { min-height: 60vh; }
}
