/* =============================================================================
   Фото → Раскраска — адаптивные стили
   ========================================================================== */

:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef2f9;
  --border: #d8dfeb;
  --text: #1f2a3d;
  --text-muted: #64748b;
  --primary: #3b6ef5;
  --primary-hover: #2f5bd6;
  --danger-bg: #fdecec;
  --danger-border: #f3b0b0;
  --danger-text: #b3261e;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(31, 42, 61, 0.08);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* --- Заголовок ------------------------------------------------------------- */
.header {
  text-align: center;
  padding: 2rem 1rem 1rem;
}

.logo {
  margin: 0;
  font-size: clamp(1.6rem, 5vw, 2.4rem);
  font-weight: 800;
  letter-spacing: -0.5px;
}

.logo span { color: var(--primary); }

.tagline {
  margin: 0.4rem 0 0;
  color: var(--text-muted);
  font-size: clamp(0.9rem, 2.6vw, 1.05rem);
}

/* --- Контейнер ------------------------------------------------------------- */
.container {
  width: 100%;
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 1rem 2rem;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}

.panel-title {
  margin: 0 0 1rem;
  font-size: 1.1rem;
}

/* --- Dropzone -------------------------------------------------------------- */
.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 1rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s, transform 0.15s;
  background: var(--surface-2);
}

.dropzone:hover,
.dropzone:focus-visible,
.dropzone.is-dragover {
  border-color: var(--primary);
  background: #e8efff;
  outline: none;
}

.dropzone.is-dragover { transform: scale(1.01); }

.dropzone-icon { font-size: 2.5rem; }

.dropzone-title {
  margin: 0.5rem 0 0.2rem;
  font-weight: 600;
  font-size: 1.05rem;
}

.dropzone-hint,
.dropzone-formats {
  margin: 0.15rem 0 0;
  color: var(--text-muted);
  font-size: 0.88rem;
}


/* --- Управление ------------------------------------------------------------ */
.control-row { margin-bottom: 1.1rem; }

.control-row label {
  display: block;
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.4rem;
}

.control-row output {
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

.control-hint {
  display: block;
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 0.25rem;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--primary);
  height: 1.6rem;
  cursor: pointer;
}

.control-row.check label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
  cursor: pointer;
}

/* Скрытие групп параметров, не относящихся к выбранному движку. */
.hidden { display: none !important; }

/* --- Переключатель движка ---------------------------------------------------- */
.control-label {
  display: block;
  font-weight: 600;
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
}

.engine-switch {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.25rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.engine-option {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  border-radius: 9px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.engine-option input[type="radio"] {
  accent-color: var(--primary);
  margin: 0;
}

.engine-option:has(input:checked) {
  background: var(--primary);
  color: #fff;
}

.engine-option:has(input:checked) input[type="radio"] {
  accent-color: #fff;
}

.engine-row { margin-bottom: 1.25rem; }


input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  accent-color: var(--primary);
}

/* --- Кнопки ----------------------------------------------------------------- */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.5rem;
}

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 0.7rem 1.3rem;
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.1s, opacity 0.15s;
}

.btn:active { transform: translateY(1px); }

.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:not(:disabled):hover { background: var(--primary-hover); }

.btn-ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.btn-ghost:hover { background: var(--surface-2); }

/* --- Результат -------------------------------------------------------------- */
.result-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

.result-item {
  margin: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.result-item figcaption {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-align: center;
}

.result-item img {
  width: 100%;
  height: auto;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 6px;
  background: #fff;
}

/* --- Алерты и загрузчик ----------------------------------------------------- */
.alert {
  margin-top: 0.9rem;
  padding: 0.75rem 1rem;
  border-radius: 10px;
  font-size: 0.92rem;
}

.alert-error {
  background: var(--danger-bg);
  border: 1px solid var(--danger-border);
  color: var(--danger-text);
}

.alert-info {
  background: #eaf7ee;
  border: 1px solid #b6e0c2;
  color: #1d6b38;
}

.loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 10px;
  font-weight: 600;
  color: var(--text-muted);
  z-index: 5;
}

/* Атрибут hidden обязан побеждать display: flex (иначе лоадер «залипает»). */
.loader[hidden] { display: none !important; }

.loader span {
  width: 34px;
  height: 34px;
  border: 4px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* --- Футер ------------------------------------------------------------------ */
.footer {
  text-align: center;
  padding: 1.25rem;
  color: var(--text-muted);
  font-size: 0.82rem;
}

/* --- Режим «Портрет»: правка маски ------------------------------------------ */
.portrait-panel { position: relative; }

.portrait-mask-area {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.brush-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
}

.brush-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: 10px;
  padding: 0.55rem 1rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.brush-btn:hover { background: #e4ebf7; }

.brush-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.brush-btn.ghost { margin-left: auto; }

.brush-size-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 0.9rem;
}

.brush-size-label input[type="range"] {
  width: 140px;
  height: 1.2rem;
}

.mask-canvas-wrap {
  width: 100%;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: repeating-conic-gradient(#eef2f9 0% 25%, #ffffff 0% 50%) 50% /
    24px 24px;
  display: flex;
  justify-content: center;
  padding: 0.5rem;
}

#mask-canvas {
  /* Размеры задаёт JS (setupCanvasSize) с учётом ширины контейнера и DPR.
     max-width/height не задаём, чтобы не конфликтовать с inline-размерами. */
  cursor: crosshair;
  touch-action: none; /* чтобы палец рисовал, а не скроллил */
  border-radius: 6px;
  display: block;
}

.portrait-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
}

.portrait-controls label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 0.92rem;
}

.portrait-controls select {
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.92rem;
  background: #fff;
  color: var(--text);
}

.portrait-controls input[type="range"] { width: 120px; }

.portrait-result-figure {
  margin-top: 1.25rem;
}

/* --- Мобильные -------------------------------------------------------------- */
@media (max-width: 640px) {
  .result-grid { grid-template-columns: 1fr; }
  .dropzone { padding: 1.8rem 0.75rem; }
  .actions .btn { flex: 1 1 100%; }
  .panel { padding: 1rem; }
  .brush-controls { gap: 0.5rem; }
  .brush-btn.ghost { margin-left: 0; }
  .portrait-controls { flex-direction: column; align-items: stretch; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
