/* ── Reset & Base ─────────────────────────────────────────── */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-hover: #f0f1f5;
  --border: #d9dce6;
  --border-light: #e8eaf0;
  --text: #1a1d2e;
  --text-muted: #6b7185;
  --primary: #5b4cdb;
  --primary-hover: #4a3bc9;
  --primary-light: rgba(91, 76, 219, 0.08);
  --accent: #0891b2;
  --accent-hover: #0e7490;
  --accent-light: rgba(8, 145, 178, 0.08);
  --danger: #dc2626;
  --danger-light: rgba(220, 38, 38, 0.06);
  --success: #059669;
  --success-light: rgba(5, 150, 105, 0.08);
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow: 0 1px 3px rgba(0,0,0,0.07), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.06), 0 2px 4px rgba(0,0,0,0.04);
  --radius: 10px;
  --radius-sm: 6px;
  --font: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  --checker-a: #e8e8e8;
  --checker-b: #f8f8f8;
}

html {
  font-size: 15px;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
}

/* ── Layout ──────────────────────────────────────────────── */
header {
  text-align: center;
  padding: 1.75rem 1rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

header h1 {
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-top: 0.2rem;
}

main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

footer {
  text-align: center;
  padding: 1.25rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border-light);
}

/* ── Card ────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.card h2 {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 1rem;
  color: var(--text);
}

.section-hint {
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

/* ── Upload ──────────────────────────────────────────────── */
.upload-area {
  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;
}

.upload-area:hover,
.upload-area.dragover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.upload-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  color: var(--text-muted);
}

.upload-icon {
  font-size: 2.5rem;
}

.upload-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  opacity: 0.7;
}

.image-info {
  display: flex;
  gap: 1.5rem;
  margin-top: 0.75rem;
  font-family: var(--mono);
  font-size: 0.85rem;
  color: var(--accent);
}

.upload-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

/* ── Grid Controls ───────────────────────────────────────── */
.grid-controls,
.export-controls,
.anim-controls {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.input-group label {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 500;
}

input[type="number"],
input[type="text"],
input[type="range"] {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem;
  color: var(--text);
  font-family: var(--mono);
  font-size: 0.9rem;
  width: 100px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input[type="text"] {
  width: 180px;
}

input[type="range"] {
  padding: 0;
  border: none;
  background: transparent;
  width: 120px;
}

input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

/* ── Buttons ─────────────────────────────────────────────── */
.btn {
  padding: 0.5rem 1.15rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: var(--shadow-sm);
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover {
  background: var(--accent-hover);
  box-shadow: var(--shadow-sm);
}

.btn-outline {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-outline:hover {
  background: var(--surface-hover);
  border-color: var(--text-muted);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: #b91c1c;
}

.btn-sm {
  padding: 0.3rem 0.6rem;
  font-size: 0.78rem;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Canvas ──────────────────────────────────────────────── */
.canvas-wrapper {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 20px 20px;
  display: flex;
  justify-content: center;
  padding: 1rem;
  position: relative;
}

#main-canvas {
  max-width: 100%;
  height: auto;
  cursor: default;
}

.canvas-wrapper.grid-interactive #main-canvas {
  cursor: crosshair;
}

/* ── Preview Grid ────────────────────────────────────────── */
.preview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.75rem;
}

.preview-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}

.preview-item canvas {
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 10px 10px;
  border-radius: 4px;
  max-width: 100%;
  cursor: grab;
  border: 1px solid var(--border-light);
}

.preview-item canvas:active {
  cursor: grabbing;
}

.preview-item .frame-label {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--text-muted);
}

.preview-item .preview-toolbar {
  display: flex;
  gap: 0.3rem;
  width: 100%;
  justify-content: center;
}

.preview-item .offset-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  width: 100%;
}

.preview-item .offset-controls .input-group {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.35rem;
}

.preview-item .offset-controls .input-group label {
  min-width: 14px;
  text-align: right;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  flex-shrink: 0;
}

.preview-item .offset-controls input {
  width: 100%;
  min-width: 0;
  padding: 0.35rem 0.5rem;
  font-size: 0.82rem;
}

/* ── Animation Section ───────────────────────────────────── */
.anim-canvas-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 16px 16px;
  padding: 1rem;
  margin-bottom: 1rem;
}

#anim-canvas {
  border-radius: 4px;
}

.anim-frame-strip {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  padding: 0.5rem 0;
  margin-bottom: 1rem;
}

.anim-frame-thumb {
  width: 56px;
  height: 56px;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  object-fit: contain;
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 8px 8px;
  flex-shrink: 0;
  transition: border-color 0.15s;
}

.anim-frame-thumb.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--primary-light);
}

.anim-frame-thumb:hover {
  border-color: var(--primary);
}

/* ── Export Status ────────────────────────────────────────── */
.export-status {
  margin-top: 0.75rem;
  padding: 0.6rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-family: var(--mono);
}

.export-status.success {
  background: var(--success-light);
  color: var(--success);
  border: 1px solid rgba(5, 150, 105, 0.2);
}

.export-status.error {
  background: var(--danger-light);
  color: var(--danger);
  border: 1px solid rgba(220, 38, 38, 0.2);
}

/* ── Utilities ───────────────────────────────────────────── */
.hidden {
  display: none !important;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 600px) {
  .grid-controls,
  .export-controls,
  .anim-controls {
    flex-direction: column;
    align-items: stretch;
  }

  input[type="number"],
  input[type="text"] {
    width: 100%;
  }

  .preview-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  }
}

/* ── Tab Bar ─────────────────────────────────────────────── */
.tab-bar {
  display: flex;
  justify-content: center;
  gap: 0.35rem;
  margin-top: 1rem;
  padding-bottom: 0;
}

.tab-btn {
  padding: 0.45rem 1.4rem;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  background: var(--bg);
  color: var(--text-muted);
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.tab-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.tab-btn.active {
  background: var(--surface);
  color: var(--primary);
  border-color: var(--border);
  box-shadow: 0 -2px 0 0 var(--primary) inset;
}

.tab-panel {
  border-top: 1px solid var(--border);
  background: var(--bg);
}

/* ── Optimize / Resize Controls ─────────────────────────── */
.optimize-controls {
  display: flex;
  align-items: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.radio-group {
  display: flex;
  gap: 1rem;
  padding-top: 0.25rem;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.88rem;
  color: var(--text);
  cursor: pointer;
}

.badge-optional {
  font-size: 0.7rem;
  font-weight: 600;
  background: var(--accent-light);
  color: var(--accent);
  border: 1px solid rgba(8, 145, 178, 0.25);
  border-radius: 9999px;
  padding: 0.1rem 0.55rem;
  vertical-align: middle;
  margin-left: 0.4rem;
}

/* ── Frame Names Grid ────────────────────────────────────── */
.frame-names-title {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 1rem 0 0.4rem;
  color: var(--text);
}

.frame-names-grid {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.5rem;
  max-height: 320px;
  overflow-y: auto;
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 0.5rem;
  background: var(--bg);
}

.frame-name-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.3rem 0.4rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
  border: 1px solid var(--border-light);
}

.frame-name-thumb {
  border-radius: 3px;
  border: 1px solid var(--border-light);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 8px 8px;
  flex-shrink: 0;
}

.frame-name-label {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 28px;
  flex-shrink: 0;
}

.frame-name-input {
  flex: 1;
  width: auto !important;
  min-width: 0;
  padding: 0.35rem 0.6rem;
  font-size: 0.85rem;
  font-family: var(--mono);
}

/* ── Resize Preview Pair ─────────────────────────────────── */
.resize-preview-pair {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 0.5rem;
}

.resize-preview-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.resize-preview-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.resize-preview-arrow {
  font-size: 1.8rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

#resize-before-canvas,
#resize-after-canvas {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: repeating-conic-gradient(var(--checker-a) 0% 25%, var(--checker-b) 0% 50%) 0 0 / 12px 12px;
  max-width: 100%;
}

.resize-dim-info {
  font-family: var(--mono);
  font-size: 0.78rem;
  color: var(--accent);
}
