/* styles.css – Idle Directive Body Optimizer */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-deep:    #0a1020;
  --bg-panel:   #111828;
  --bg-card:    #182235;
  --bg-hover:   #1e2e46;
  --border:     #2a4262;
  --border-hi:  #3a5880;
  --text:       #d0e8ff;
  --text-dim:   #8ab4d8;
  --text-bright:#f0f8ff;
  --accent:     #4a9fdf;
  --bus-color:  #FF7B2B;
  --ok-color:   #5abf60;
  --err-color:  #f05050;
  --warn-color: #FFA726;
  --font-mono:  'Courier New', Courier, monospace;
  --font-ui:    system-ui, -apple-system, sans-serif;
  --panel-w:    310px;
}

html, body {
  height: 100%;
  background: var(--bg-deep);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  line-height: 1.5;
  overflow: hidden;
}

/* ── Layout ──────────────────────────────────────────────────────────────── */

#app { display: flex; height: 100vh; width: 100vw; }

#left-panel {
  width: var(--panel-w);
  min-width: 260px;
  max-width: 360px;
  background: var(--bg-panel);
  border-right: 2px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

#main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-deep);
}

/* ── Left panel header ───────────────────────────────────────────────────── */

#panel-header {
  padding: 14px 14px 10px;
  border-bottom: 2px solid var(--border);
  background: #0c1525;
}

#panel-header h1 {
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 3px;
  color: var(--accent);
  margin-bottom: 8px;
}

#grid-info { display: flex; gap: 10px; align-items: center; }

#grid-size-label {
  font-family: var(--font-mono);
  font-size: 18px;
  font-weight: bold;
  color: var(--text-bright);
}

#grid-level-label {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--bus-color);
  background: #ff7b2b22;
  border: 1px solid #ff7b2b66;
  padding: 2px 10px;
  border-radius: 10px;
}

/* ── Control buttons ─────────────────────────────────────────────────────── */

#body-controls {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

button {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 1px;
  cursor: pointer;
  border-radius: 4px;
  border: none;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}

button:disabled { opacity: 0.3; cursor: not-allowed; }

#btn-expand {
  background: #ff7b2b25;
  color: var(--bus-color);
  border: 1px solid #ff7b2b66;
  padding: 8px 10px;
}
#btn-expand:hover:not(:disabled) { background: #ff7b2b44; }

#btn-auto {
  background: #4a9fdf25;
  color: var(--accent);
  border: 1px solid #4a9fdf66;
  padding: 8px 10px;
}
#btn-auto:hover:not(:disabled) { background: #4a9fdf44; }

#btn-reset {
  background: #f0505022;
  color: var(--err-color);
  border: 1px solid #f0505055;
  padding: 8px 10px;
}
#btn-reset:hover:not(:disabled) { background: #f0505044; }

#btn-new-comp {
  background: transparent;
  color: var(--text-dim);
  border: 1px dashed var(--border-hi);
  padding: 6px 10px;
  font-size: 11px;
}
#btn-new-comp:hover { color: var(--text-bright); border-color: var(--accent); }

/* ── Component library ───────────────────────────────────────────────────── */

#component-library {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
  border-bottom: 2px solid var(--border);
  min-height: 0;
}

#component-library h2,
#placed-components h2 {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--text-dim);
  padding: 6px 12px 4px;
  text-transform: uppercase;
}

.cat-label {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 2px;
  color: var(--accent);
  background: #4a9fdf14;
  padding: 5px 12px 3px;
  text-transform: uppercase;
  border-top: 1px solid var(--border);
  margin-top: 2px;
}

.comp-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.comp-item:hover { background: var(--bg-hover); }

.comp-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
}

.comp-info { flex: 1; min-width: 0; }

.comp-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-bright);
}

.comp-desc {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 2px;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.comp-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.comp-count {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--accent);
  background: #4a9fdf22;
  border: 1px solid #4a9fdf44;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 26px;
  text-align: center;
}

.btn-add {
  width: 28px; height: 28px;
  background: #5abf6025;
  color: var(--ok-color);
  border: 1px solid #5abf6066;
  font-size: 18px;
  line-height: 1;
  padding: 0;
  border-radius: 4px;
}
.btn-add:hover { background: #5abf6044; }

.btn-edit {
  width: 28px; height: 28px;
  background: #4a9fdf18;
  color: var(--accent);
  border: 1px solid #4a9fdf55;
  font-size: 13px;
  padding: 0;
  border-radius: 4px;
}
.btn-edit:hover { background: #4a9fdf33; }

/* ── Placed components ───────────────────────────────────────────────────── */

#placed-components {
  height: 200px;
  overflow-y: auto;
  padding: 4px 0;
  flex-shrink: 0;
}

.empty-hint {
  padding: 20px 16px;
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
}

.placed-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background 0.1s;
}
.placed-item:hover    { background: var(--bg-hover); }
.placed-item.selected { background: #4a9fdf18; border-left-color: var(--accent); }
.placed-item.powered  { border-left-color: #5abf6088; }
.placed-item.unpowered { opacity: 0.55; }

.placed-icon { font-size: 45px; line-height: 1; font-family: serif; }
.comp-icon-inline {
  font-size: 36px;
  line-height: 1;
  display: inline-block;
  vertical-align: middle;
  margin-right: 8px;
  font-family: serif;
}
.placed-name {
  flex: 1;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-bright);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.placed-pos {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-dim);
  white-space: nowrap;
}
.placed-power { font-size: 12px; }
.placed-power.on  { color: var(--bus-color); }
.placed-power.off { color: #f0505088; }

.btn-remove {
  width: 22px; height: 22px;
  background: transparent;
  color: var(--text-dim);
  border: none;
  font-size: 12px;
  padding: 0;
  border-radius: 3px;
  flex-shrink: 0;
}
.btn-remove:hover { background: #f0505033; color: var(--err-color); }

/* ── Status bar ──────────────────────────────────────────────────────────── */

#grid-container {
  flex: 1;
  overflow: auto;
  min-height: 0;
  padding: 12px;
}

#grid-scroll { display: inline-block; min-width: 100%; }
#body-grid   { display: block; }

#status-bar {
  height: 32px;
  padding: 0 14px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
  background: #0c1525;
  flex-shrink: 0;
}

#status-msg {
  font-family: var(--font-mono);
  font-size: 12px;
  flex: 1;
  color: var(--text);
}

.status-ok    { color: var(--ok-color); }
.status-error { color: var(--err-color); }
.status-warn  { color: var(--warn-color); }

#power-status {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
}

/* ── Better layout offer banner ──────────────────────────────────────────── */

#opt-offer {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 14px;
  background: #4a9fdf18;
  border-top: 1px solid var(--accent);
  border-bottom: 1px solid #4a9fdf44;
  flex-shrink: 0;
  animation: pulse-border 2s ease-in-out infinite;
}
#opt-offer.hidden { display: none; }

@keyframes pulse-border {
  0%, 100% { border-top-color: var(--accent); }
  50%       { border-top-color: #4a9fdf88; }
}

#opt-offer-msg {
  flex: 1;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent);
}

.opt-offer-btns { display: flex; gap: 6px; }

.opt-offer-btns button {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 5px 14px;
  border-radius: 4px;
  cursor: pointer;
}

.opt-offer-btns button:first-child {
  background: #5abf6025;
  color: var(--ok-color);
  border: 1px solid #5abf6066;
}
.opt-offer-btns button:first-child:hover { background: #5abf6044; }

.opt-offer-btns button:last-child {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.opt-offer-btns button:last-child:hover { background: var(--bg-hover); color: var(--text); }

/* ── Auto-wire tag ───────────────────────────────────────────────────────── */

.auto-tag {
  font-family: var(--font-mono);
  font-size: 9px;
  color: var(--bus-color);
  background: #ff7b2b22;
  border: 1px solid #ff7b2b44;
  padding: 1px 5px;
  border-radius: 8px;
  flex-shrink: 0;
}

/* ── Scrollbars ──────────────────────────────────────────────────────────── */
::-webkit-scrollbar       { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb { background: var(--border-hi); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  #left-panel { width: 230px; min-width: 200px; }
  .comp-desc  { display: none; }
}

/* ── Modal (glassmorphism: blurred translucent backdrop) ───────────────── */
.modal {
  position: fixed; inset: 0;
  background: rgba(8,14,24,0.45);
  backdrop-filter: blur(10px) saturate(140%);
  -webkit-backdrop-filter: blur(10px) saturate(140%);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  animation: modal-fade-in 180ms ease-out;
}
.modal.hidden { display: none; }
@keyframes modal-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.modal-content {
  /* Solid translucent panel — no nested backdrop-filter. Stacking blur(20px)
     on top of the modal's outer blur(10px) saturate(140%) caused Firefox
     to lock up when pasting a long string into a textarea: every keystroke
     forced both filters to re-composite the underlying page. */
  background: rgba(20,30,50,0.96);
  border: 1px solid rgba(74,159,223,0.3);
  border-radius: 10px;
  box-shadow: 0 12px 48px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.06);
  padding: 22px;
  width: min(640px, 92vw);
  max-height: 90vh;
  display: flex; flex-direction: column;
  animation: modal-content-in 220ms cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modal-content-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-content h3 {
  color: var(--text-bright);
  margin-bottom: 12px;
  font-size: 14px;
  font-weight: 600;
}
.modal-info {
  color: var(--text-dim);
  font-size: 12px;
  margin-bottom: 10px;
  background: var(--bg-card);
  padding: 8px 10px;
  border-radius: 4px;
  border: 1px solid var(--border);
}
.modal-info p { margin: 2px 0; }
.modal-info strong { color: var(--text); }
.modal-content textarea {
  width: 100%; flex: 1;
  font-family: 'Courier New', monospace; font-size: 11px;
  background: var(--bg-deep); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px;
  padding: 8px; resize: vertical;
}
.modal-buttons {
  display: flex; gap: 8px;
  margin-top: 12px;
  justify-content: flex-end;
}
.modal-buttons button {
  padding: 6px 16px;
  background: var(--bg-card);
  color: var(--text); border: 1px solid var(--border-hi);
  border-radius: 4px; cursor: pointer;
  font-size: 12px;
}
.modal-buttons button:hover { background: var(--bg-hover); }

/* ── Settings icon next to title ───────────────────────────────────────── */
#btn-settings {
  background: none; border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  margin-left: 8px;
  vertical-align: middle;
  line-height: 1;
}
#btn-settings:hover { color: var(--text-bright); }

/* ── Settings modal sections ───────────────────────────────────────────── */
.settings-section {
  margin: 12px 0;
  padding: 12px;
  background: var(--bg-card);
  border-radius: 4px;
  border: 1px solid var(--border);
}
.settings-section h4 {
  color: var(--text);
  margin-bottom: 10px;
  font-size: 12px;
  font-weight: 600;
}
.settings-section label {
  display: block;
  color: var(--text);
  margin-bottom: 6px;
  font-size: 12px;
}
.threads-control {
  display: flex;
  align-items: center;
  gap: 12px;
}
.threads-control input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}
#setting-threads-value {
  display: inline-block;
  min-width: 28px;
  text-align: center;
  font-weight: 600;
  color: var(--text-bright);
  background: var(--bg-deep);
  padding: 2px 8px;
  border-radius: 4px;
  font-family: monospace;
}
.setting-hint {
  color: var(--text-dim);
  font-size: 11px;
  margin-top: 8px;
  line-height: 1.4;
}
.setting-hint a {
  color: var(--accent);
  text-decoration: underline;
}
.setting-hint a:hover { color: var(--text-bright); }
.setting-hint a strong { color: inherit; }
.settings-buttons {
  display: flex;
  gap: 8px;
}
.settings-buttons button {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-panel);
  color: var(--text);
  border: 1px solid var(--border-hi);
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}
.settings-buttons button:hover { background: var(--bg-hover); }

/* Danger button (e.g. System reset) */
.settings-buttons button.danger {
  background: rgba(120,30,30,0.4);
  color: #ffb0b0;
  border-color: rgba(180,60,60,0.4);
}
.settings-buttons button.danger:hover {
  background: rgba(160,40,40,0.6);
  color: #ffd0d0;
}

/* SMART (Simulated Annealing) button — primary action */
#btn-anneal {
  background: linear-gradient(135deg, #5abf6022, #80cbc422);
  color: #80cbc4;
  border: 1px solid #5abf6066;
  padding: 8px 10px;
  font-weight: 600;
}
#btn-anneal:hover:not(:disabled) {
  background: linear-gradient(135deg, #5abf6044, #80cbc444);
  color: #b0e0e0;
}

/* Top-K SA results panel */
#opt-results {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(80, 203, 196, 0.05);
}
.result-slot {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 11px;
  transition: background 0.1s, border-color 0.1s;
}
.result-slot:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}
.result-rank {
  color: var(--accent);
  font-weight: bold;
  min-width: 24px;
}
.result-score {
  color: var(--text-bright);
  flex: 1;
  font-weight: 600;
}
.result-age {
  color: var(--text-dim);
  font-size: 10px;
}

/* STOP button */
#btn-stop {
  background: #f0505022;
  color: var(--err-color);
  border: 1px solid #f0505055;
  padding: 8px 10px;
}
#btn-stop:hover:not(:disabled) { background: #f0505044; }

/* TOP follow-mode button (header of results panel) */
.top-btn {
  background: var(--bg-panel);
  color: var(--text-dim);
  border: 1px solid var(--border-hi);
  padding: 6px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  transition: background 0.1s, color 0.1s, border-color 0.1s;
}
.top-btn:hover { background: var(--bg-hover); color: var(--text-bright); }
.top-btn.active {
  background: rgba(74,159,223,0.25);
  color: var(--accent);
  border-color: var(--accent);
  box-shadow: 0 0 8px rgba(74,159,223,0.3);
}

/* Scrollable results panel — up to 20 entries */
#opt-results {
  max-height: 280px;
  overflow-y: auto;
}
#opt-results::-webkit-scrollbar { width: 6px; }
#opt-results::-webkit-scrollbar-thumb { background: var(--border-hi); border-radius: 3px; }

/* Carry mode — component is "lifted" from grid and follows cursor */
g[data-carrying="true"] {
  opacity: 0.65;
  pointer-events: none;
  transition: transform 60ms ease-out;
}
g[data-carrying="true"] rect {
  stroke-dasharray: 4 3;
  stroke-width: 2 !important;
}
body.carry-mode {
  cursor: crosshair !important;
}
body.carry-mode g[data-comp] {
  cursor: crosshair !important;
}

/* Carry-mode trash button — fixed top-right, only visible while carrying. */
.carry-trash {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 200;
  padding: 12px 22px;
  font-size: 14px;
  font-family: var(--font-mono);
  font-weight: bold;
  letter-spacing: 1px;
  background: rgba(240, 80, 80, 0.92);
  color: white;
  border: 2px solid #ff5050;
  border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(240, 80, 80, 0.45);
  pointer-events: auto;
  transition: background 0.15s, transform 0.15s;
}
.carry-trash:hover {
  background: rgba(255, 60, 60, 1);
  transform: scale(1.05);
}
.carry-trash.hidden { display: none; }

/* Help button matches the settings gear: a small inline button in the panel header */
#btn-help {
  background: none; border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  margin-left: 4px;
  vertical-align: middle;
  line-height: 1;
}
#btn-help:hover { color: var(--text-bright); }

/* Help modal content: wider than settings, scrollable, semantic typography */
.help-content {
  width: min(720px, 92vw) !important;
  max-height: 86vh;
  overflow-y: auto;
}
.help-section {
  margin: 14px 0 18px;
}
.help-section h4 {
  color: var(--accent);
  font-size: 13px;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin: 0 0 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.help-section p {
  margin: 6px 0;
  line-height: 1.5;
  color: var(--text);
}
.help-section p strong { color: var(--text-bright); }
.help-section a {
  color: var(--accent);
  text-decoration: underline;
}
.help-section a:hover { color: var(--text-bright); }
.help-section dl {
  margin: 6px 0;
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 14px;
  row-gap: 8px;
  align-items: start;
}
.help-section dt {
  white-space: nowrap;
  color: var(--text-bright);
  font-size: 12px;
  padding-top: 2px;
}
.help-section dd {
  margin: 0;
  font-size: 12px;
  color: var(--text);
  line-height: 1.5;
}
.help-section ul {
  margin: 6px 0 6px 22px;
  padding: 0;
}
.help-section ul li {
  margin: 4px 0;
  font-size: 12px;
}
.help-section code {
  font-family: var(--font-mono);
  font-size: 11px;
  background: var(--bg-deep);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--text-bright);
}
.help-section .kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  background: var(--bg-deep);
  border: 1px solid var(--border-hi);
  padding: 2px 6px;
  border-radius: 3px;
  color: var(--text-bright);
  white-space: nowrap;
}
.help-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
}
.help-table th, .help-table td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}
.help-table th {
  color: var(--accent);
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
}
.help-table td:first-child {
  color: var(--text-bright);
  font-weight: 600;
}
