:root {
  --bg: #0f1220;
  --panel: #181c2f;
  --panel-2: #202646;
  --text: #e8ebff;
  --muted: #adb7e8;
  --accent: #5f8bff;
  --accent-2: #88a8ff;
  --danger: #ff6b6b;
  --border: #303960;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, Segoe UI, Roboto, Arial, sans-serif;
  background: radial-gradient(circle at top, #1b2140, var(--bg) 40%);
  color: var(--text);
}

.container {
  width: min(1100px, 92%);
  margin: 2rem auto;
  display: grid;
  gap: 1rem;
}

.hero h1 {
  margin: 0;
  font-size: 2rem;
}

.hero p {
  margin: 0.5rem 0 0;
  color: var(--muted);
}

.panel {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: end;
}

.field {
  min-width: 230px;
  display: grid;
  gap: 0.35rem;
}

label {
  font-size: 0.9rem;
  color: var(--muted);
}

input,
select,
button {
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 0.7rem 0.8rem;
  font-size: 0.95rem;
}

input,
select {
  background: #10152c;
  color: var(--text);
}

button {
  cursor: pointer;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  transition: 0.15s ease;
}

button:hover {
  background: var(--accent-2);
}

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

button.ghost {
  background: transparent;
  border-color: var(--accent);
}

button.ghost:hover {
  background: rgba(95, 139, 255, 0.12);
}

.recommendations h2,
.output h2 {
  margin-top: 0;
}

.recommendations ul {
  margin: 0;
  padding-left: 1.1rem;
  display: grid;
  gap: 0.35rem;
}

.status {
  margin: 0 0 0.8rem;
  color: var(--muted);
}

.status.error {
  color: var(--danger);
}

.image-preview-layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 0.8rem;
}

.grid-sidebar {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d1228;
  padding: 0.75rem;
}

.grid-sidebar h3 {
  margin: 0 0 0.65rem;
  font-size: 1rem;
}

.field.compact {
  min-width: auto;
  margin-bottom: 0.55rem;
}

.hint {
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.image-preview-wrap {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d1228;
  min-height: 280px;
  display: grid;
  place-items: center;
  overflow: hidden;
}

.preview-stage {
  position: relative;
  display: inline-block;
  max-width: 100%;
  max-height: 520px;
  line-height: 0;
  outline: none;
}

.preview-stage:focus-visible {
  box-shadow: 0 0 0 2px rgba(136, 168, 255, 0.6);
  border-radius: 4px;
}

#imagePreview {
  width: auto;
  max-width: 100%;
  max-height: 520px;
  object-fit: contain;
  display: none;
}

#imagePreview.visible {
  display: block;
}

.grid-box {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  display: none;
  cursor: move;
  border: 1px solid var(--grid-color, #ffffff);
}

.grid-box.visible {
  display: block;
}

.grid-overlay {
  width: 100%;
  height: 100%;
  background-image:
    repeating-linear-gradient(
      to right,
      var(--grid-color, #ffffff) 0,
      var(--grid-color, #ffffff) 1px,
      transparent 1px,
      transparent calc(100% / var(--cols, 3))
    ),
    repeating-linear-gradient(
      to bottom,
      var(--grid-color, #ffffff) 0,
      var(--grid-color, #ffffff) 1px,
      transparent 1px,
      transparent calc(100% / var(--rows, 1))
    );
}

.resize-handle {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--grid-color, #ffffff);
  border: 1px solid #0d1228;
}

.resize-handle.nw { left: -5px; top: -5px; cursor: nwse-resize; }
.resize-handle.n { left: calc(50% - 5px); top: -5px; cursor: ns-resize; }
.resize-handle.ne { right: -5px; top: -5px; cursor: nesw-resize; }
.resize-handle.e { right: -5px; top: calc(50% - 5px); cursor: ew-resize; }
.resize-handle.se { right: -5px; bottom: -5px; cursor: nwse-resize; }
.resize-handle.s { left: calc(50% - 5px); bottom: -5px; cursor: ns-resize; }
.resize-handle.sw { left: -5px; bottom: -5px; cursor: nesw-resize; }
.resize-handle.w { left: -5px; top: calc(50% - 5px); cursor: ew-resize; }

.grid-box.hidden-handles .resize-handle {
  display: none;
}

@media (max-width: 900px) {
  .image-preview-layout {
    grid-template-columns: 1fr;
  }
}

.frames-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(170px, 1fr));
  gap: 0.85rem;
}

.frame-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #0d1228;
  overflow: hidden;
}

.frame-card img {
  width: 100%;
  display: block;
  aspect-ratio: 1 / 1;
  object-fit: cover;
}

.frame-card footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.45rem 0.55rem;
  gap: 0.45rem;
}

.frame-card small {
  color: var(--muted);
}

.frame-card a {
  text-decoration: none;
  color: #fff;
  font-size: 0.82rem;
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 0.25rem 0.45rem;
}
