/* ==========================================================
   APIMart Image Studio — "Darkroom"
   Matte-black photographer's workstation with vermillion accent.
   Layout: Composer LEFT, Output RIGHT.
   Mobile: single column, composer on top.
   ========================================================== */

/* ---------- tokens ---------- */
:root {
  --black:       #101010;
  --surface-0:   #141414;
  --surface-1:   #1a1a1a;
  --surface-2:   #222;
  --surface-3:   #2c2c2c;
  --border:      #2a2a2a;
  --border-hi:   #3a3a3a;
  --text:        #e8e4df;
  --text-2:      #a09a92;
  --text-3:      #6b665f;
  --red:         #e03c31;
  --red-dim:     rgba(224,60,49,.12);
  --red-glow:    rgba(224,60,49,.30);
  --green:       #3ddc84;
  --green-dim:   rgba(61,220,132,.12);

  --font-serif:  "Instrument Serif", "Noto Serif SC", Georgia, serif;
  --font-sans:   "Geist", "Noto Sans SC", system-ui, sans-serif;
  --font-mono:   "Geist Mono", "JetBrains Mono", Consolas, monospace;

  --ease:        cubic-bezier(.22,1,.36,1);
  --radius:      10px;
  --radius-lg:   16px;
}

/* ---------- reset ---------- */
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
[hidden]{display:none!important}
html{-webkit-text-size-adjust:100%;scroll-behavior:smooth}

/* ---------- base ---------- */
body {
  font-family: var(--font-sans);
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text);
  background: var(--black);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
  min-height: 100dvh;
}

::selection { background: var(--red-dim); color: var(--text); }

/* sr-only */
.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap}

/* ---------- layout ---------- */
.page {
  max-width: 1520px;
  margin: 0 auto;
  padding: 20px 20px 40px;
}

/* header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--border);
}
.header__brand { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.header__tag {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--red);
  background: var(--red-dim);
  padding: 3px 8px;
  border-radius: 4px;
  line-height: 1;
}
.header__title {
  font-family: var(--font-serif);
  font-size: clamp(1.25rem, 2.8vw, 1.75rem);
  font-weight: 400;
  letter-spacing: -.01em;
  line-height: 1.1;
  color: var(--text);
}
.header__title em {
  font-style: italic;
  color: var(--text-2);
}
.header__nav {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.header__status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-2);
}
.header__status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
  animation: blink 2.4s ease-in-out infinite;
}
@keyframes blink {
  0%,100%{opacity:.5}50%{opacity:1}
}

/* main grid */
.main {
  display: grid;
  grid-template-columns: minmax(340px, 1fr) minmax(0, 1.25fr);
  gap: 20px;
  align-items: start;
}
.col { min-width: 0; }
.col--composer { position: sticky; top: 20px; }
.col--output { display: grid; gap: 16px; }

/* ---------- cards ---------- */
.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.card--primary {
  border-color: var(--border-hi);
  background: var(--surface-0);
}
.card--compact { padding: 14px 18px; }
.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 18px;
}
.card__head h2 {
  font-family: var(--font-serif);
  font-size: 18px;
  font-weight: 400;
  letter-spacing: -.01em;
}
.card__head-actions {
  display: flex;
  gap: 6px;
}

/* pill badge */
.pill {
  font-family: var(--font-mono);
  font-size: 9px;
  font-weight: 600;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--red);
  background: var(--red-dim);
  padding: 2px 8px;
  border-radius: 4px;
}

/* ---------- form / controls ---------- */
.form { display: grid; gap: 16px; }
.params {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  border: none;
}
.ctrl { display: grid; gap: 4px; }
.ctrl--full { /* spans full width inside form, no special grid needed */ }
.ctrl__label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-3);
  letter-spacing: .03em;
  text-transform: uppercase;
}

input, textarea, select {
  font: inherit;
  width: 100%;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  outline: none;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
input:focus, textarea:focus, select:focus {
  border-color: var(--red);
  box-shadow: 0 0 0 2px var(--red-dim);
}
input::placeholder, textarea::placeholder { color: var(--text-3); }
input[readonly] { opacity: .55; cursor: default; }
textarea { resize: vertical; line-height: 1.6; }
select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%236b665f' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

/* ---------- buttons ---------- */
button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all .15s var(--ease);
}
button:disabled { opacity: .45; cursor: wait; pointer-events: none; }

.btn {
  padding: 10px 18px;
  font-weight: 600;
  font-size: 13px;
}
.btn--sm { padding: 6px 12px; font-size: 12px; }

.btn--accent {
  background: var(--red);
  color: #fff;
  box-shadow: 0 2px 12px var(--red-dim);
}
.btn--accent:hover { box-shadow: 0 4px 20px var(--red-glow); filter: brightness(1.1); }
.btn--ghost {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
}
.btn--ghost:hover { background: var(--surface-3); color: var(--text); border-color: var(--border-hi); }
.btn--outline {
  background: transparent;
  color: var(--red);
  border: 1px solid var(--red-dim);
}
.btn--outline:hover { background: var(--red-dim); }
.btn--danger {
  background: var(--red-dim);
  color: var(--red);
  border: 1px solid rgba(224,60,49,.18);
}
.btn--danger:hover { background: rgba(224,60,49,.2); }
.btn--icon {
  width: 38px; height: 38px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-2);
}
.btn--icon:hover { color: var(--text); border-color: var(--border-hi); background: var(--surface-3); }

.form__foot {
  display: flex;
  gap: 10px;
  padding-top: 6px;
  border-top: 1px solid var(--border);
}
.form__foot .btn--accent { flex: 1; }

/* ---------- foldable sections ---------- */
.fold { border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
.fold--sub { margin-top: 14px; }
.fold--static { border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 14px; }
.fold__head {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 16px;
  cursor: pointer;
  list-style: none;
  user-select: none;
  transition: background .12s;
}
.fold__head:hover { background: var(--surface-2); }
.fold__head::-webkit-details-marker { display: none; }
.fold__title { font-weight: 600; font-size: 13px; color: var(--text); }
.fold__hint { font-size: 11px; color: var(--text-3); margin-left: auto; }
.fold__arrow {
  color: var(--text-3);
  transition: transform .2s var(--ease);
  flex-shrink: 0;
}
details[open] > .fold__head .fold__arrow { transform: rotate(180deg); }
.fold__body { padding: 0 16px 16px; display: grid; gap: 12px; }
.fold__code {
  display: block; padding: 12px 16px;
  font-family: var(--font-mono); font-size: 11px; line-height: 1.6;
  color: var(--red); white-space: pre-wrap; word-break: break-word;
  background: var(--surface-0); border-top: 1px solid var(--border);
}

/* negative panel body — required class for app.js */
.negative-panel-body { display: grid; gap: 12px; padding: 0 16px 16px; }
.negative-summary-text { font-size: 11px; color: var(--text-3); }

/* chips */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-3);
  color: var(--text-2);
  border: 1px solid var(--border);
}
.chip.active, .template-chip.active {
  background: var(--red-dim);
  color: var(--red);
  border-color: rgba(224,60,49,.25);
}
.chip:hover { border-color: var(--border-hi); }

/* references */
.reference-panel { display: grid; gap: 10px; }
.reference-list { display: grid; gap: 8px; }
.reference-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: start;
}

/* ---------- indicators ---------- */
.indicators {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.indicator {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  transition: border-color .15s;
}
.indicator:hover { border-color: var(--border-hi); }
.indicator__label {
  display: block;
  font-size: 9.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--text-3);
  margin-bottom: 4px;
}
.indicator strong { font-size: 13px; }
.mono { font-family: var(--font-mono); font-size: 11px !important; word-break: break-all; }

/* ---------- result panel ---------- */
.result-panel { display: grid; gap: 0; }
.result-panel-layout { display: grid; }
.result-panel-layout #resultGallery  { order: 1; }
.result-panel-layout #requestDetails { order: 2; margin-top: 14px; }
.result-panel-layout .result-meta    { order: 3; }
.result-panel-layout #rawResult      { order: 4; margin-top: 8px; }

.result-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 14px;
}
.meta-cell {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
}
.meta-label {
  display: block;
  font-size: 9.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--text-3);
  margin-bottom: 3px;
}

/* ---------- gallery ---------- */
.gallery { display: grid; gap: 14px; }
.gallery.empty {
  min-height: 200px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border-hi);
  border-radius: var(--radius-lg);
  background: var(--surface-0);
}
.gallery__placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--text-3);
  font-size: 12px;
}
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}
.image-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--surface-2);
  transition: border-color .15s, box-shadow .2s;
}
.image-card:hover {
  border-color: var(--red-dim);
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
}
.image-card img {
  display: block; width: 100%;
  aspect-ratio: 1; object-fit: cover;
  background: var(--surface-0);
}
.image-card-body { padding: 12px; display: grid; gap: 6px; }
.image-card code {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-3);
  display: block;
}
.download-link {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 34px;
}

/* raw result */
.raw-result {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px;
  background: var(--surface-2);
}
.raw-result__title {
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 400;
  margin-bottom: 10px;
}
pre, code { font-family: var(--font-mono); }
pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 11px;
  line-height: 1.55;
  color: var(--text-2);
}

/* status-box */
.status-box { border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
.status-box[open] { border-color: var(--border-hi); }

/* collapsible-summary — class required by app.js */
.collapsible-summary {
  display: flex; align-items: center; gap: 8px;
  padding: 12px 16px; cursor: pointer; list-style: none;
}
.collapsible-summary::-webkit-details-marker { display: none; }

/* ---------- history ---------- */
/* history-actions compatibility classes */
.history-actions { margin-bottom: 12px; }
.history-actions-inline { display: flex; gap: 6px; flex-wrap: nowrap; }
.history-actions-inline button { flex: 1; }

.history-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.history-list.empty {
  min-height: 100px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--border-hi);
  border-radius: var(--radius);
  color: var(--text-3);
  font-size: 12px;
  background: var(--surface-0);
}
.history-item {
  display: grid; gap: 8px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  transition: border-color .15s;
}
.history-item:hover { border-color: var(--border-hi); }
.history-thumb {
  width: 100%;
  aspect-ratio: 16/9;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-0);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
}
.history-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.history-placeholder { color: var(--text-3); font-size: 11px; }
.history-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.history-task { font-family: var(--font-mono); font-size: 10px; color: var(--red); word-break: break-all; opacity: .8; }
.history-meta { color: var(--text-3); font-size: 10px; }
.history-actions-group { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.history-download-list { display: flex; gap: 6px; flex-wrap: wrap; }
.history-download {
  text-decoration: none;
  font-size: 11px;
  padding: 5px 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 30px;
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  padding: 20px;
  background: rgba(0,0,0,.72);
  backdrop-filter: blur(8px);
  z-index: 100;
  animation: fadeIn .18s var(--ease);
}
@keyframes fadeIn { from{opacity:0}to{opacity:1} }

.modal-card {
  width: min(100%, 580px);
  background: var(--surface-1);
  border: 1px solid var(--border-hi);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: 0 20px 60px rgba(0,0,0,.6);
  animation: modalIn .28s cubic-bezier(.34,1.56,.64,1);
}
@keyframes modalIn { from{opacity:0;transform:translateY(16px) scale(.97)}to{opacity:1;transform:none} }

.modal__head {
  display: flex; justify-content: space-between; align-items: start;
  gap: 16px; margin-bottom: 20px;
}
.modal__head h2 {
  font-family: var(--font-serif);
  font-size: 20px; font-weight: 400;
}
.modal__desc { font-size: 12px; color: var(--text-3); margin-top: 4px; }
.modal__body { display: grid; gap: 14px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.modal__foot { margin-top: 20px; display: flex; justify-content: flex-end; }

/* collapse-indicator (backward compat) */
.collapse-indicator { display: flex; align-items: center; color: var(--text-3); transition: transform .2s var(--ease); }
details[open] > summary .collapse-indicator { transform: rotate(180deg); }

/* ---------- scrollbar ---------- */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

/* ---------- focus ---------- */
:focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }

/* ===========================================================
   RESPONSIVE — mobile-first adjustments
   =========================================================== */

@media (max-width: 960px) {
  .main {
    grid-template-columns: 1fr;
  }
  .col--composer {
    position: static;
    order: -1;          /* composer stays on top */
  }
  .settings-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  .page { padding: 14px 12px 28px; }

  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .header__nav { width: 100%; justify-content: space-between; }
  .header__title { font-size: 1.15rem; }

  .params {
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  .card { padding: 14px; border-radius: 12px; }
  .card--compact { padding: 12px; }

  .indicators {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .result-meta { grid-template-columns: 1fr; }

  .history-list { grid-template-columns: 1fr; gap: 8px; }
  .history-thumb { aspect-ratio: 1; }
  .history-row { flex-direction: column; align-items: stretch; }
  .history-actions-group { display: grid; grid-template-columns: 1fr; }

  .reference-item { grid-template-columns: 1fr; }

  .form__foot { flex-direction: column; }
  .form__foot .btn--accent { order: -1; }

  .chips {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }

  .modal-card { padding: 18px; }
  .settings-grid { grid-template-columns: 1fr; }

  .gallery-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 380px) {
  .params { grid-template-columns: 1fr; }
  .chips  { grid-template-columns: 1fr; }
}

/* ---------- print ---------- */
@media print {
  .header__nav, .form__foot, .modal-backdrop { display: none; }
  body { background: #fff; color: #111; }
  .card { border: 1px solid #ccc; box-shadow: none; }
}

/* ---------- inline-actions / template-actions compat ---------- */
.inline-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.template-actions { /* no special override */ }
.template-actions-bottom { /* no special override */ }

/* ---------- secondary (used by renderGallery / renderHistory) ---------- */
.secondary {
  background: var(--red-dim);
  color: var(--red);
  border: 1px solid rgba(224,60,49,.18);
  border-radius: var(--radius);
  padding: 8px 14px;
  font-weight: 600;
  font-size: 12px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background .15s;
}
.secondary:hover { background: rgba(224,60,49,.2); }

/* ghost — also used in renderHistory */
.ghost {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  transition: all .15s;
}
.ghost:hover { background: var(--surface-3); color: var(--text); border-color: var(--border-hi); }

/* danger — used for remove-reference via template */
.danger {
  background: var(--red-dim);
  color: var(--red);
  border: 1px solid rgba(224,60,49,.18);
  border-radius: var(--radius);
  padding: 6px 12px;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
}
.danger:hover { background: rgba(224,60,49,.2); }

/* primary — used for saveSettingsBtn */
.primary {
  background: var(--red);
  color: #fff;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.primary:hover { filter: brightness(1.1); }
