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

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #e2e4e8;
  --text: #1e2126;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --radius: 12px;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.app { display: flex; min-height: 100vh; }

/* Боковая панель истории */
.sidebar {
  width: 270px;
  flex-shrink: 0;
  background: #1b1e24;
  color: #e5e7eb;
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  gap: 10px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar.collapsed { display: none; }
.sidebar-head { display: flex; gap: 8px; align-items: center; }
.new-dialog-btn {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid #3a3f4a;
  background: transparent;
  color: #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  text-align: left;
}
.new-dialog-btn:hover { background: #262a33; }
.sidebar-toggle {
  border: none;
  background: transparent;
  color: inherit;
  font-size: 18px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 6px;
}
.sidebar-toggle:hover { background: #262a33; }
.mobile-only { display: none; }
.history-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #9ca3af;
  padding: 0 4px;
}
.history-list { display: flex; flex-direction: column; gap: 4px; }
.history-empty { color: #6b7280; font-size: 13px; padding: 8px 4px; }
.history-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
}
.history-item:hover { background: #262a33; }
.history-item.active { background: #2c3444; }
.history-thumb {
  width: 44px;
  height: 44px;
  border-radius: 6px;
  object-fit: cover;
  flex-shrink: 0;
  background: #333;
}
.history-meta { min-width: 0; flex: 1; }
.history-name {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-date { font-size: 11px; color: #9ca3af; margin-top: 2px; }
.history-del {
  border: none;
  background: transparent;
  color: #6b7280;
  font-size: 15px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 5px;
  visibility: hidden;
}
.history-item:hover .history-del { visibility: visible; }
.history-del:hover { color: #f87171; background: #33272b; }

/* Основная область */
.content {
  flex: 1;
  min-width: 0;
  padding: 24px;
  max-width: 1100px;
  margin: 0 auto;
}

.topbar { margin-bottom: 24px; display: flex; gap: 12px; align-items: flex-start; }
.topbar h1 { font-size: 26px; }
.topbar .sidebar-toggle { color: var(--text); }
.subtitle { color: var(--muted); margin-top: 4px; }

.layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}
@media (max-width: 860px) { .layout { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.panel h2 { font-size: 16px; margin-bottom: 14px; }
.panel h2:not(:first-child) { margin-top: 22px; }

/* Слоты загрузки */
.slots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.slot {
  position: relative;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.15s;
  text-align: center;
}
.slot:hover { border-color: var(--accent); }
.add-slot-btn {
  width: 100%;
  margin-top: 10px;
  padding: 9px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}
.add-slot-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.add-slot-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.slot-label { color: var(--muted); font-size: 14px; padding: 8px; }
.slot-label small { font-size: 12px; }
.slot-preview {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.slot-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  line-height: 1;
}

/* Выбор модели */
.model-picker input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 8px;
}
.model-picker input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.model-list {
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.model-item {
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.model-item:last-child { border-bottom: none; }
.model-item:hover { background: #f0f4ff; }
.model-item.selected { background: #e5edff; border-left: 3px solid var(--accent); }
.model-item .model-id { color: var(--muted); font-size: 12px; }
.model-empty { padding: 12px; color: var(--muted); font-size: 14px; }

/* Конструктор */
.group { margin-bottom: 16px; }
.group-title { font-size: 13px; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.04em; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; }
.btns button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.12s;
}
.btns button:hover { border-color: var(--accent); }
.btns button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Текстовые поля конструктора */
.text-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
}
.text-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.note-input { margin-top: 8px; font-size: 13px; }

/* Цветовые ряды RAL */
.color-row { display: flex; gap: 8px; align-items: center; }
.color-row .ral-input { width: 130px; flex-shrink: 0; }
.ral-swatch {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background:
    linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%),
    linear-gradient(45deg, #eee 25%, #fff 25%, #fff 75%, #eee 75%);
  background-size: 12px 12px;
  background-position: 0 0, 6px 6px;
  flex-shrink: 0;
}
.palette-btn {
  padding: 8px 12px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
}
.palette-btn:hover { border-color: var(--accent); color: var(--accent); }
.photo-select {
  padding: 8px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
  min-width: 0;
  flex: 1;
}

/* Модалка палитры RAL */
.modal-overlay[hidden] { display: none; }
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 24, 0.55);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal {
  background: #fff;
  border-radius: 14px;
  width: min(680px, 100%);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-head {
  display: flex;
  gap: 8px;
  padding: 14px;
  border-bottom: 1px solid var(--border);
}
.modal-close {
  border: none;
  background: transparent;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
  padding: 0 10px;
  border-radius: 8px;
}
.modal-close:hover { background: #f3f4f6; color: var(--text); }
.ral-grid {
  overflow-y: auto;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
}
.ral-cell {
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  overflow: hidden;
  background: #fff;
  text-align: center;
  transition: transform 0.08s;
}
.ral-cell:hover { transform: scale(1.05); border-color: var(--accent); }
.ral-cell .ral-color { height: 44px; }
.ral-cell .ral-code { font-size: 12px; font-weight: 600; padding: 4px 2px 0; }
.ral-cell .ral-name {
  font-size: 10px;
  color: var(--muted);
  padding: 0 3px 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ral-empty { color: var(--muted); padding: 16px; grid-column: 1 / -1; }

.generate-btn {
  width: 100%;
  margin-top: 10px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: background 0.15s;
}
.generate-btn:hover:not(:disabled) { background: var(--accent-hover); }
.generate-btn:disabled { background: #b8c4d9; cursor: not-allowed; }

.error {
  margin-top: 12px;
  padding: 10px 12px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #b91c1c;
  border-radius: 8px;
  font-size: 14px;
  white-space: pre-wrap;
}

/* Результат */
.result-panel { margin-top: 20px; }
.loader { text-align: center; padding: 40px 0; color: var(--muted); }
.spinner {
  width: 48px;
  height: 48px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.results { display: flex; flex-direction: column; gap: 16px; }
.result-item img {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}
.download-btn {
  display: inline-block;
  margin-top: 10px;
  padding: 10px 18px;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  text-decoration: none;
  font-size: 14px;
}
.download-btn:hover { background: var(--accent-hover); }

/* Корректировка промпта */
.prompt-block { margin-top: 20px; }
.prompt-block textarea {
  width: 100%;
  margin-top: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  font-family: Consolas, monospace;
  resize: vertical;
  line-height: 1.45;
}
.prompt-block textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.generate-btn.secondary {
  margin-top: 10px;
  background: #fff;
  color: var(--accent);
  border: 1px solid var(--accent);
}
.generate-btn.secondary:hover:not(:disabled) { background: #eff4ff; }
.generate-btn.secondary:disabled { background: #f3f4f6; color: #9ca3af; border-color: var(--border); }

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    z-index: 50;
    left: 0;
    box-shadow: 4px 0 24px rgba(0,0,0,0.35);
  }
  .mobile-only { display: inline-block; }
  .content { padding: 16px; }
}
