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

:root {
  /* strictly neutral greys (equal RGB) — no hue cast anywhere */
  --bg: #161616;
  --panel: #1e1e1e;
  --panel-2: #282828;
  --border: #3a3a3a;
  --text: #e8e8e8;
  --muted: #9b9b9b;
  --accent: #f5c518;       /* banana */
  --accent-2: #ff8a3d;     /* horse */
  --radius: 10px;
  font-size: 15px;
  color-scheme: dark; /* keeps scrollbars & native video controls neutral dark */
}

html { background: var(--bg); }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; /* frames scroll internally; the page itself never scrolls */
}

/* three independent frames, each scrolling vertically */
.layout {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr) 260px;
  grid-template-areas: "panel gallery story";
  height: 100vh;
}

/* ============ left panel ============ */
.panel {
  grid-area: panel;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 20px;
  gap: 16px;
}

.panel-header h1 {
  font-size: 1.5rem;
  letter-spacing: 0.5px;
}
.subtitle { color: var(--muted); font-size: 0.8rem; }

.mode-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mode-tab {
  padding: 10px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.95rem;
  transition: all 0.15s;
}
.mode-tab.active[data-mode="image"] { border-color: var(--accent); color: var(--accent); }
.mode-tab.active[data-mode="video"] { border-color: var(--accent-2); color: var(--accent-2); }

.model-badge {
  display: inline-block;
  font-size: 0.75rem;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 99px;
  padding: 3px 10px;
  margin-bottom: 14px;
  opacity: 0.9;
}
.model-badge.horse { color: var(--accent-2); border-color: var(--accent-2); }

.mode-form { display: flex; flex-direction: column; }
.mode-form[hidden] { display: none; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; flex: 1; }
.field > span { font-size: 0.85rem; color: var(--muted); }
.field small { opacity: 0.7; }

textarea, select, input[type="number"], input[type="password"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 9px 10px;
  font-size: 0.92rem;
  font-family: inherit;
  width: 100%;
  resize: vertical;
}
textarea:focus, select:focus, input:focus { outline: 1px solid var(--accent); }

.row { display: flex; gap: 10px; }

/* reference image grid */
.ref-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.ref-thumb {
  position: relative;
  width: 64px; height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.ref-thumb img { width: 100%; height: 100%; object-fit: cover; }
.ref-thumb button {
  position: absolute; top: 2px; right: 2px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px; height: 18px;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

.upload-tile {
  display: block;
  text-align: center;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: border-color 0.15s;
}
.upload-tile:hover { border-color: var(--accent); color: var(--text); }
.upload-tile.big { padding: 28px 10px; }

.source-preview { position: relative; }
.source-preview img {
  width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  display: block;
}
.source-preview .remove-source {
  position: absolute; top: 6px; right: 6px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
  font-size: 0.8rem;
}

.generate-btn {
  margin-top: 4px;
  padding: 12px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #1a1605;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: filter 0.15s;
}
.generate-btn.horse { background: var(--accent-2); color: #2b1304; }
.generate-btn:hover { filter: brightness(1.1); }
.generate-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.panel-footer { margin-top: auto; }
.panel-footer summary { color: var(--muted); font-size: 0.85rem; cursor: pointer; }
.key-row { display: flex; gap: 8px; margin-top: 8px; }
.key-row button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0 14px;
  cursor: pointer;
}
#key-status { color: var(--muted); display: block; margin-top: 6px; }

/* ============ right gallery ============ */
.gallery-pane {
  grid-area: gallery;
  background: #000000;
  overflow-y: auto;
  overflow-x: hidden;
  min-width: 0;
  padding: 20px 24px;
}
.gallery-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.gallery-tabs { display: flex; gap: 8px; }
.gallery-tab {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 6px 16px;
  cursor: pointer;
  font-size: 0.9rem;
}
.gallery-tab.active {
  border-color: var(--accent);
  color: var(--accent);
}
#clear-gallery {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 5px 12px;
  cursor: pointer;
  font-size: 0.8rem;
}
#clear-gallery:hover { color: #ff6b6b; border-color: #ff6b6b; }

/* row-major flow: newest top-left, reading order left-to-right then down */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  align-items: start;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
/* media keeps its native aspect ratio: width fills the column, height follows */
.card img, .card video {
  width: 100%;
  height: auto;
  display: block;
}

.card .meta {
  padding: 8px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.card .meta .prompt {
  font-size: 0.78rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.card .actions { display: flex; gap: 6px; flex-shrink: 0; }
.card .actions a, .card .actions button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 0.8rem;
  cursor: pointer;
  text-decoration: none;
}
.card .actions a:hover, .card .actions button:hover { border-color: var(--accent); }
.card .actions .delete:hover { border-color: #ff6b6b; color: #ff6b6b; }

/* pending card */
.card.pending .placeholder {
  width: 100%;
  background:
    linear-gradient(110deg, var(--panel-2) 40%, #303030 50%, var(--panel-2) 60%);
  background-size: 200% 100%;
  animation: shimmer 1.6s infinite linear;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  padding: 8px;
}
@keyframes shimmer {
  to { background-position: -200% 0; }
}
.card.error .placeholder {
  animation: none;
  background: #2a1518;
  color: #ff8a8a;
}

.gallery-empty {
  color: var(--muted);
  text-align: center;
  margin-top: 15vh;
}

/* ============ dropzones ============ */
.dropzone { border-radius: var(--radius); }
.dropzone.drag-over .upload-tile,
.source-preview.drag-over .upload-tile {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(245, 197, 24, 0.08);
}

.card.draggable { cursor: grab; }
.card.dragging { opacity: 0.4; }

/* badge on gallery cards whose image is used in the story */
.card .in-story {
  position: absolute;
  top: 8px; left: 8px;
  background: rgba(0,0,0,0.75);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 6px;
  font-size: 0.72rem;
  padding: 2px 7px;
  pointer-events: none;
}
.card { position: relative; }

/* ============ right story panel ============ */
.story {
  grid-area: story;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-left: 1px solid var(--border);
  background: var(--panel);
  padding: 12px;
  min-height: 0;
  overflow: hidden;
}

.story-bar {
  display: flex;
  align-items: center;
  gap: 10px;
}
.story-title {
  color: var(--muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}
.story-count { color: var(--muted); font-size: 0.78rem; }
#story-clear {
  margin-left: auto;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 8px;
  padding: 3px 12px;
  cursor: pointer;
  font-size: 0.75rem;
}
#story-clear:hover { color: #ff6b6b; border-color: #ff6b6b; }

/* vertical shot list: scrolls down, never sideways */
.story-strip {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  overflow-x: hidden;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 10px;
  min-height: 0;
}
.story-strip.drag-over { border-color: var(--accent); background: rgba(245, 197, 24, 0.06); }
.story-hint {
  color: var(--muted);
  font-size: 0.85rem;
  align-self: center;
  margin: 0 auto;
  pointer-events: none;
}

/* shots keep their image's native aspect ratio: width fills the column, height follows */
.shot {
  position: relative;
  flex: 0 0 auto;
  width: 100%;
  border: 2px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  cursor: grab;
  background: #000;
}
.shot img {
  width: 100%;
  height: auto;
  display: block;
  pointer-events: none;
}
.shot.dragging { opacity: 0.4; }
.shot.drop-before { box-shadow: 0 -5px 0 0 var(--accent); }
.shot.drop-after { box-shadow: 0 5px 0 0 var(--accent); }
.shot .shot-number {
  position: absolute;
  top: 4px; left: 4px;
  background: var(--accent);
  color: #1a1605;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 1px 6px;
}
.shot .shot-remove {
  position: absolute;
  top: 4px; right: 4px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px; height: 18px;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

@media (max-width: 900px) {
  body { overflow: auto; overflow-x: hidden; }
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas: "panel" "gallery" "story";
    height: auto;
  }
  .panel { border-right: none; border-bottom: 1px solid var(--border); }
  .story { border-left: none; border-top: 1px solid var(--border); max-height: 50vh; }
}
