:root {
  --ink: #0a0b12;
  --ink-soft: #2b2f3a;
  --canvas: #f7f3ee;
  --accent: #f35b4f;
  --accent-dark: #d64337;
  --mint: #3ccfcf;
  --card: rgba(255, 255, 255, 0.92);
  --shadow: 0 20px 40px rgba(10, 11, 18, 0.15);
  --radius: 22px;
  --radius-sm: 12px;
}

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

body {
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  background: radial-gradient(circle at 20% 20%, #fff2db 0%, transparent 55%),
    radial-gradient(circle at 80% 10%, #dff7ff 0%, transparent 40%),
    linear-gradient(140deg, #fef8f2 0%, #eaf6ff 55%, #fdf5f0 100%);
  color: var(--ink);
  min-height: 100vh;
}

.background-orbit {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(circle at 15% 65%, rgba(243, 91, 79, 0.12), transparent 55%),
    radial-gradient(circle at 80% 55%, rgba(60, 207, 207, 0.18), transparent 50%);
  z-index: 0;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 1200px;
  margin: 0 auto;
  padding: 48px 24px 72px;
  display: grid;
  gap: 28px;
}

.hero {
  display: flex;
  flex-wrap: wrap;
  gap: 24px;
  align-items: center;
  justify-content: space-between;
}

.eyebrow {
  font-size: 0.85rem;
  letter-spacing: 0.2rem;
  text-transform: uppercase;
  color: var(--accent-dark);
  font-weight: 600;
}

h1 {
  font-family: "Fraunces", "Times New Roman", serif;
  font-size: clamp(2.4rem, 3.5vw, 3.5rem);
  margin: 12px 0 10px;
}

.subtitle {
  max-width: 520px;
  color: var(--ink-soft);
  font-size: 1.05rem;
}

.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 22px 26px;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  animation: float-in 0.6s ease both;
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  align-items: end;
}

.control-group {
  display: grid;
  gap: 8px;
}

label {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.12rem;
  color: var(--ink-soft);
}

input,
select {
  border-radius: var(--radius-sm);
  border: 1px solid rgba(10, 11, 18, 0.1);
  padding: 10px 12px;
  font-size: 0.95rem;
  background: #fff;
  font-family: inherit;
}

input:focus,
select:focus {
  outline: 2px solid rgba(60, 207, 207, 0.4);
  border-color: rgba(60, 207, 207, 0.8);
}

button {
  border: none;
  border-radius: 999px;
  padding: 10px 18px;
  font-weight: 600;
  font-size: 0.95rem;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

button.primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 12px 24px rgba(243, 91, 79, 0.3);
}

button.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 16px 30px rgba(243, 91, 79, 0.35);
}

button.ghost {
  background: transparent;
  border: 1px solid rgba(10, 11, 18, 0.15);
  color: var(--ink-soft);
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  color: var(--ink-soft);
}

.panel-head h2 {
  font-size: 1.4rem;
  font-family: "Fraunces", serif;
  color: var(--ink);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 14px 18px;
}

.form-grid label {
  text-transform: none;
  letter-spacing: 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.form-grid span {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
}

.form-actions {
  margin-top: 18px;
  display: flex;
  gap: 12px;
}

.table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(10, 11, 18, 0.08);
}

.table-panel table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 14px 12px;
  text-align: left;
  font-size: 0.95rem;
}

/* Rating+YouTube combined cell */
.rating-yt-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

th {
  background: rgba(10, 11, 18, 0.04);
  text-transform: uppercase;
  letter-spacing: 0.1rem;
  font-size: 0.75rem;
}

.sort-btn {
  width: 100%;
  display: inline-flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  text-transform: inherit;
  letter-spacing: inherit;
  padding: 0;
  cursor: pointer;
}

.sort-btn:focus-visible {
  outline: 2px solid rgba(60, 207, 207, 0.6);
  border-radius: 999px;
}

.sort-indicator {
  font-size: 0.7rem;
  opacity: 0.7;
}

.sort-indicator::before {
  content: "";
}

.sort-btn.is-active[data-order="asc"] .sort-indicator::before {
  content: "▲";
}

.sort-btn.is-active[data-order="desc"] .sort-indicator::before {
  content: "▼";
}

tr {
  border-bottom: 1px solid rgba(10, 11, 18, 0.08);
  transition: background 0.2s ease;
  cursor: pointer;
}

tr:hover {
  background: rgba(60, 207, 207, 0.08);
}

th tr {
  cursor: default;
}

.cover {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  object-fit: cover;
  box-shadow: 0 8px 16px rgba(10, 11, 18, 0.12);
}

.actions {
  display: flex;
  gap: 8px;
}

.muted {
  color: var(--ink-soft);
  font-size: 0.85rem;
}

.error-msg {
  font-weight: 700;
  color: #c0392b;
  font-size: 0.95rem;
}

.actions button {
  padding: 8px 12px;
  font-size: 0.85rem;
}

.empty {
  margin-top: 16px;
  padding: 16px;
  border-radius: var(--radius-sm);
  background: rgba(243, 91, 79, 0.08);
  color: var(--accent-dark);
  text-align: center;
}

.pagination {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  flex-wrap: wrap;
}

.pagination button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

@keyframes float-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 700px) {
  .app {
    padding: 24px 10px 48px;
  }

  .panel {
    padding: 16px 12px;
  }

  .form-actions {
    flex-direction: column;
    align-items: stretch;
  }

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

  /* New Mobile Form Layout */
  .form-grid {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .form-grid label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
  }

  .form-grid label span {
    margin-bottom: 0;
    width: auto;
    min-width: 80px; /* Consistently sized label width */
    max-width: 120px;
    flex-shrink: 0;
  }

  .form-grid input,
  .form-grid select {
    flex: 1;
    width: 100%; /* Ensure it takes available space */
    min-width: 0;
  }

  .file-input-group {
    flex: 1;
    width: 100%;
  }

  /* Library Mobile Layout */
  .table-panel table,
  .table-panel tbody,
  .table-panel tr,
  .table-panel td {
    display: block;
    width: 100%;
  }

  .table-panel thead {
    display: none;
  }

  /* Make row a grid container */
  .table-panel tr {
    display: grid;
    /*
      Col 1: Cover (fixed 50px)
      Col 2: Name (takes remaining space)
      Col 3: YouTube icon (auto, collapses when empty)
      Col 4: Year / Bin (auto)
      Col 5: Players / Actions (auto)
    */
    grid-template-columns: 50px 1fr auto auto;

    /* Rows defined implicitly, but we can be explicit */
    /* Row 1: Main Info */
    /* Row 2: Secondary Info + Actions */
    grid-template-rows: auto auto;

    gap: 6px 8px;
    padding: 10px 0;
    align-items: center;
    border-bottom: 1px solid rgba(10, 11, 18, 0.08);
  }

  /* Target specific cells by order */
  /*
    DOM Order:
    1: Cover
    2: Name
    3: Year
    4: Players
    5: Rating+YouTube
    6: Bin
    7: Actions
  */

  /* --- Row 1 --- */
  .table-panel td:nth-child(1) { /* Cover */
    grid-column: 1;
    grid-row: 1;
    padding: 0;
  }

  .table-panel td:nth-child(2) { /* Name */
    grid-column: 2;
    grid-row: 1;
    padding: 0;
    align-self: center;
    min-width: 0;
  }

  .table-panel td:nth-child(3) { /* Year */
    grid-column: 3;
    grid-row: 1;
    padding: 0;
    align-self: center;
    font-size: 0.8rem;
    color: var(--ink-soft);
    white-space: nowrap;
    text-align: right;
  }

  .table-panel td:nth-child(3):empty {
    display: none;
  }

  .table-panel td:nth-child(4) { /* Players */
    grid-column: 4;
    grid-row: 1;
    padding: 0;
    align-self: center;
    font-size: 0.85rem;
    white-space: nowrap;
    justify-self: end;
    text-align: right;
  }

  /* Append " Players" text if not empty */
  .table-panel td:nth-child(4):empty {
    display: none;
  }

  .table-panel td:nth-child(4):not(:empty)::after {
    content: " p";
    color: var(--ink-soft);
  }

  /* --- Row 2 --- */
  .table-panel td:nth-child(5) { /* Rating+YouTube */
    grid-column: 2;
    grid-row: 2;
    padding: 0;
    align-self: center;
  }

  .table-panel td:nth-child(6) { /* Bin */
    grid-column: 3;
    grid-row: 2;
    padding: 0;
    align-self: center;
    font-size: 0.85rem;
    white-space: nowrap;
    display: flex;
    align-items: center;
  }

  .table-panel td:nth-child(6):empty {
    display: none;
  }

  .table-panel td:nth-child(6):not(:empty)::before {
    content: "Bin ";
    color: var(--ink-soft);
    margin-right: 4px;
    font-size: 0.75rem;
  }

  .table-panel td:nth-child(7) { /* Actions */
    grid-column: 4;
    grid-row: 2;
    justify-self: end;
    padding: 0;
  }

  .cover {
    width: 44px;
    height: 44px;
    border-radius: 10px;
  }

  .actions button {
    padding: 6px 10px;
    font-size: 0.75rem;
  }
}

.form-grid label:has(.file-input-group) {
  /* No special handling on desktop, it will follow grid auto-placement unless we want it on its own row */
  grid-column: 1 / -1;
  display: block; /* Stack vertical */
}

/* Ensure consistent spacing for the span inside since we reset display */
.form-grid label:has(.file-input-group) > span {
  display: block;
  margin-bottom: 6px;
}

.file-input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Ensure the file input takes available space */
.file-input-group input[type="file"] {
  width: 100%;
}

.preview-container {
  margin-top: 0;
  position: relative;
  width: fit-content;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

#coverPreview {
  max-width: 100px;
  max-height: 100px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  display: block;
}

#coverPreview[hidden] {
  display: none !important;
}

#clearCoverBtn {
  font-size: 0.75rem;
  padding: 4px 8px;
}

/* ── Game Card ─────────────────────────────────────────── */

.game-card-section {
  animation: float-in 0.35s ease both;
}

.back-btn {
  margin-bottom: 24px;
}

.gc-layout {
  display: flex;
  gap: 36px;
  align-items: flex-start;
}

.gc-cover-wrap {
  flex-shrink: 0;
}

.gc-cover {
  width: 200px;
  height: 200px;
  object-fit: cover;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: block;
}

.gc-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-width: 0;
}

.gc-name {
  font-family: "Fraunces", serif;
  font-size: clamp(1.4rem, 2.5vw, 2rem);
  line-height: 1.2;
}

.gc-alt-name {
  margin-top: 4px;
  font-size: 0.95rem;
}

.gc-meta {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 20px;
  align-items: baseline;
}

.gc-meta dt {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12rem;
  color: var(--ink-soft);
  white-space: nowrap;
}

.gc-meta dd {
  font-size: 0.95rem;
  color: var(--ink);
}

.gc-footer {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.rating-badge {
  display: inline-block;
  font-weight: 700;
  font-size: 0.8rem;
  padding: 3px 10px;
  border-radius: 999px;
  color: #fff;
  background: linear-gradient(135deg, #e74c3c, #c0392b);
  white-space: nowrap;
  text-decoration: none;
}

.rating-badge.medium {
  background: linear-gradient(135deg, #f39c12, #e67e22);
}

.rating-badge.high {
  background: linear-gradient(135deg, #27ae60, #229954);
}

.gc-rating {
  font-weight: 700;
  font-size: 1rem;
  padding: 6px 16px;
  border-radius: 999px;
  color: #fff;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  background: linear-gradient(135deg, #e74c3c, #c0392b); /* default/low */
}

.gc-rating.medium {
  background: linear-gradient(135deg, #f39c12, #e67e22);
  box-shadow: 0 8px 20px rgba(243, 156, 18, 0.35);
}

.gc-rating.high {
  background: linear-gradient(135deg, #27ae60, #229954);
  box-shadow: 0 8px 20px rgba(39, 174, 96, 0.35);
}

.gc-rating:not([href]) {
  cursor: default;
}

a.gc-rating:hover {
  transform: translateY(-2px);
}

.gc-bins {
  display: flex;
  gap: 8px;
  margin-left: auto;
}

.gc-bin-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 5px 14px;
  border-radius: var(--radius-sm);
  background: rgba(60, 207, 207, 0.12);
  border: 1px solid rgba(60, 207, 207, 0.3);
  text-decoration: none;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.gc-bin-badge:hover {
  background: rgba(60, 207, 207, 0.22);
  border-color: rgba(60, 207, 207, 0.55);
}

.gc-bin-label {
  font-size: 0.6rem;
  text-transform: uppercase;
  letter-spacing: 0.15rem;
  color: var(--ink-soft);
}

.gc-bin-number {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--mint);
  line-height: 1.2;
}

.gc-external-links {
  display: flex;
  align-items: center;
  gap: 12px;
}

.gc-yt-btn[hidden] {
  display: none;
}

.yt-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #ff0000;
  text-decoration: none;
  opacity: 0.85;
  transition: opacity 0.15s ease;
}

.yt-icon:hover {
  opacity: 1;
}

.yt-icon svg {
  width: 40px;
  height: 40px;
  display: block;
}

.gc-yt-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #ff0000;
  color: #fff;
  text-decoration: none;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  box-shadow: 0 8px 20px rgba(255, 0, 0, 0.28);
  flex-shrink: 0;
}

.gc-yt-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 14px 28px rgba(255, 0, 0, 0.38);
}

.gc-yt-btn svg {
  width: 22px;
  height: 22px;
}

@media (max-width: 600px) {
  .gc-layout {
    flex-direction: column;
  }

  .gc-cover {
    width: 100%;
    height: 220px;
  }

  .gc-bins {
    margin-left: auto;
  }
}
