:root {
  --bg: #f7f6f2;
  --panel: #fffdf8;
  --ink: #1f2933;
  --muted: #52606d;
  --line: #d9d6ce;
  --brand: #b45532;
  --brand-soft: #f5dfd6;
  --good: #176241;
  --warn: #9a6700;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Noto Sans KR", "Apple SD Gothic Neo", sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at 20% 10%, #fff9ed 0%, var(--bg) 60%);
}

.app-header {
  padding: 20px 16px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(90deg, #fffaf0 0%, #f8eee8 80%);
}

.app-header h1 {
  margin: 0;
  font-size: 24px;
}

.app-header p {
  margin: 6px 0 0;
  color: var(--muted);
}

.save-status {
  margin-top: 8px;
  font-size: 12px;
  color: #1f6f47;
}

.container {
  padding: 16px;
  display: grid;
  gap: 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 18px;
}

.help-text {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 13px;
}

.onboarding {
  border-color: #edd9c9;
  background: linear-gradient(140deg, #fff9ef 0%, #fffdf8 75%);
}

.manual-panel {
  border-color: #ddcfb9;
  background: linear-gradient(150deg, #fffdf7 0%, #fff7ee 100%);
}

.manual-details {
  border: 1px solid #e6d8c4;
  border-radius: 10px;
  background: #fff;
}

.manual-details summary {
  cursor: pointer;
  padding: 10px 12px;
  font-weight: 700;
  color: #613322;
  background: #fff7f2;
  border-radius: 10px;
}

.manual-details[open] summary {
  border-bottom: 1px solid #efdfc9;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.manual-grid {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 10px;
}

.manual-card {
  border: 1px solid #efdfc9;
  border-radius: 10px;
  padding: 10px;
  background: #fffdf9;
}

.manual-card h3 {
  margin: 0 0 6px;
  font-size: 14px;
}

.manual-card p {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.45;
}

.step-list {
  margin: 0 0 12px 18px;
  padding: 0;
  display: grid;
  gap: 6px;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.panel-head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.compact-field {
  min-width: 92px;
}

.tool-icon-btn {
  width: 30px;
  min-width: 30px;
  height: 30px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  line-height: 1;
}

.view-mode-toggle {
  display: inline-flex;
  gap: 4px;
  align-items: center;
}

.view-toggle-btn {
  width: 30px;
  min-width: 30px;
  height: 30px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  line-height: 1;
}

.view-toggle-btn.is-active,
.view-toggle-btn[aria-pressed="true"] {
  border-color: var(--brand);
  background: var(--brand-soft);
  color: var(--brand);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
}

.stat-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fff;
}

.stat-card .label {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.stat-card .value {
  margin: 4px 0 0;
  font-size: 20px;
  font-weight: 700;
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.bulk-series-row {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr auto;
  gap: 8px;
  align-items: end;
}

.bulk-series-row button {
  white-space: nowrap;
}

.file-btn,
button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
}

button:hover,
.file-btn:hover {
  border-color: var(--brand);
}

button.ghost {
  background: transparent;
}

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

.primary-btn:hover {
  border-color: #8f3f24;
  background: #9e4b2b;
}

#importJsonInput {
  display: none;
}

.series-layout,
.brick-layout {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 12px;
  align-items: start;
}

.series-list-wrap,
.brick-list-wrap {
  min-width: 0;
  overflow-x: auto;
}

.brick-list-panel {
  min-width: 0;
  display: grid;
  width: 100%;
}

.brick-list-wrap table {
  width: 100%;
  min-width: 0;
  table-layout: fixed;
}

.brick-list-wrap thead th {
  white-space: nowrap;
}

.brick-list-wrap th,
.brick-list-wrap td {
  padding: 7px 7px;
}

.brick-list-wrap th:nth-child(1),
.brick-list-wrap td:nth-child(1) {
  width: 48px;
  min-width: 48px;
}

.brick-list-wrap th:nth-child(2),
.brick-list-wrap td:nth-child(2) {
  width: auto;
  max-width: none;
}

.brick-list-wrap td:nth-child(2) > div:first-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brick-list-wrap td:nth-child(2) .muted {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brick-list-wrap th:nth-child(3),
.brick-list-wrap td:nth-child(3) {
  width: 46px;
  min-width: 46px;
  white-space: nowrap;
}

.brick-list-wrap th:nth-child(4),
.brick-list-wrap td:nth-child(4) {
  width: 88px;
  min-width: 88px;
  white-space: nowrap;
}

.brick-list-wrap th:nth-child(5),
.brick-list-wrap td:nth-child(5) {
  width: 46px;
  min-width: 46px;
  white-space: nowrap;
}

.brick-list-wrap th:nth-child(6),
.brick-list-wrap td:nth-child(6) {
  width: 40px;
  min-width: 40px;
  text-align: center;
  white-space: nowrap;
}

.brick-list-wrap th:nth-child(7),
.brick-list-wrap td:nth-child(7) {
  width: 52px;
  min-width: 52px;
  text-align: center;
  white-space: nowrap;
}

.brick-list-wrap th:nth-child(8),
.brick-list-wrap td:nth-child(8) {
  width: 58px;
  min-width: 58px;
  white-space: nowrap;
}

.brick-list-wrap .image-thumb {
  width: 38px;
  height: 38px;
}

.brick-list-wrap td:nth-child(4),
.brick-list-wrap td:nth-child(5),
.brick-list-wrap td:nth-child(8) {
  overflow: hidden;
  text-overflow: ellipsis;
}

.brick-list-wrap .badge {
  padding: 1px 4px;
  font-size: 10px;
}

.brick-list-wrap .inline-btn {
  margin-right: 1px;
  margin-bottom: 0;
}

.brick-list-wrap .icon-btn {
  width: 18px;
  min-width: 18px;
  height: 18px;
  font-size: 9px;
}

.series-list-wrap {
  max-height: 260px;
  overflow-y: auto;
}

.brick-list-wrap {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
}

.brick-thumb-wrap {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  width: 100%;
  align-content: start;
  max-height: 70vh;
  overflow-y: auto;
}

.brick-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fff;
  display: grid;
  gap: 8px;
  cursor: grab;
}

.brick-entry-row {
  cursor: grab;
}

.series-entry-row {
  cursor: pointer;
}

.brick-card:active,
.brick-entry-row:active {
  cursor: grabbing;
}

.brick-card.is-dragging,
.brick-entry-row.is-dragging {
  opacity: 0.48;
}

.brick-card.drag-over-before,
.brick-entry-row.drag-over-before td {
  box-shadow: inset 0 3px 0 var(--brand);
}

.brick-card.drag-over-after,
.brick-entry-row.drag-over-after td {
  box-shadow: inset 0 -3px 0 var(--brand);
}

.brick-card-head {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 10px;
  align-items: start;
}

.brick-card-image {
  width: 72px;
  height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  object-fit: cover;
  object-position: center 22%;
  background: #fff;
}

.brick-card-image.empty {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 11px;
  background: #f6f4ef;
}

.brick-card-title {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
}

.brick-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.collapsible-box {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}

.collapsible-box summary {
  cursor: pointer;
  padding: 10px 12px;
  font-weight: 700;
  color: #613322;
  background: #fff7f2;
  border-radius: 10px;
}

.collapsible-box[open] summary {
  border-bottom: 1px solid var(--line);
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.collapsible-box .series-layout {
  padding: 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

th,
td {
  border-bottom: 1px solid var(--line);
  text-align: left;
  padding: 8px;
  vertical-align: top;
}

th {
  background: #fff7f2;
  font-weight: 700;
}

.series-list-wrap thead th,
.brick-list-wrap thead th {
  position: sticky;
  top: 0;
  z-index: 2;
}

tbody tr:hover td {
  background: #fffaf2;
}

.form-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fff;
  display: grid;
  gap: 8px;
  align-self: start;
}

.form-card h3 {
  margin: 0 0 4px;
  font-size: 15px;
}

label {
  display: grid;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}

.label-head {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: fit-content;
  color: var(--ink);
  font-weight: 700;
  line-height: 1.3;
}

.label-head-text {
  display: inline-block;
}

.help-tip-btn {
  width: 18px;
  height: 18px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid #cab797;
  background: #fffaf1;
  color: #704227;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: help;
}

.help-tip-btn::after {
  content: attr(data-tip);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translateX(-50%) translateY(4px);
  width: min(300px, 72vw);
  padding: 8px 10px;
  border: 1px solid #d9c9b0;
  border-radius: 8px;
  background: #fffdf8;
  color: #2f2a24;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.4;
  text-align: left;
  box-shadow: 0 8px 20px rgba(31, 41, 51, 0.16);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
  z-index: 50;
}

.help-tip-btn::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: calc(100% + 2px);
  transform: translateX(-50%);
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid #d9c9b0;
  opacity: 0;
  transition: opacity 0.16s ease;
  pointer-events: none;
  z-index: 51;
}

.help-tip-btn:hover::after,
.help-tip-btn:hover::before,
.help-tip-btn:focus-visible::after,
.help-tip-btn:focus-visible::before {
  opacity: 1;
}

.help-tip-btn:hover::after,
.help-tip-btn:focus-visible::after {
  transform: translateX(-50%) translateY(0);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 8px;
  font-size: 13px;
  color: var(--ink);
  background: #fff;
}

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

.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.grid-3 {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px;
}

.brick-filter-grid {
  display: grid;
  grid-template-columns: minmax(150px, 1.1fr) minmax(110px, 0.8fr) minmax(110px, 0.8fr) minmax(220px, 1.5fr) auto;
  align-items: end;
  margin-bottom: 10px;
}

.brick-filter-grid > label {
  min-width: 0;
}

.brick-filter-tools {
  display: flex;
  align-items: end;
  justify-self: end;
  gap: 20px;
}

.filter-actions {
  display: flex;
  align-items: end;
}

.missing-tools {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px;
  margin-bottom: 8px;
}

.image-url-actions {
  display: grid;
  gap: 8px;
  align-content: end;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--line);
}

.badge.owned {
  background: #e6f7ee;
  color: var(--good);
  border-color: #b8e5cd;
}

.badge.missing {
  background: #fff4e5;
  color: var(--warn);
  border-color: #f4d8a4;
}

.badge.secondary {
  background: #f2f4f7;
}

.image-thumb {
  width: 52px;
  height: 52px;
  border-radius: 6px;
  object-fit: cover;
  object-position: center 22%;
  border: 1px solid var(--line);
}

.image-preview {
  display: none;
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.link-btn {
  color: var(--brand);
  text-decoration: none;
  font-weight: 700;
}

.link-btn:hover {
  text-decoration: underline;
}

.icon-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  height: 24px;
  font-size: 16px;
  text-decoration: none;
}

.inline-btn {
  font-size: 12px;
  padding: 4px 8px;
  margin-right: 4px;
  margin-bottom: 4px;
}

.icon-btn {
  width: 26px;
  min-width: 26px;
  height: 26px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
}

.muted {
  color: var(--muted);
  font-size: 12px;
}

.is-hidden {
  display: none !important;
}

.missing-thumb-wrap {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 10px;
}

.missing-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fff;
  display: grid;
  gap: 10px;
}

.missing-card-head {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 10px;
  align-items: start;
}

.missing-card-image {
  width: 72px;
  height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  object-fit: cover;
  object-position: center 22%;
}

.missing-card-image.empty {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 11px;
  background: #f6f4ef;
}

.missing-card-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.missing-card-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.advanced-box {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  background: #fffaf6;
}

.advanced-box summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 700;
  color: #613322;
}

.advanced-box[open] summary {
  margin-bottom: 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%) translateY(16px);
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #1f2933;
  color: #fff;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 999;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.error {
  background: #8f2f1f;
}

.confirm-dialog {
  border: none;
  padding: 0;
  border-radius: 12px;
  width: min(92vw, 420px);
}

.confirm-dialog::backdrop {
  background: rgba(19, 28, 39, 0.45);
}

.confirm-body {
  margin: 0;
  padding: 16px;
  display: grid;
  gap: 12px;
}

.confirm-body p {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
}

@media (max-width: 1100px) {
  .series-layout,
  .brick-layout {
    grid-template-columns: 1fr;
  }

  .bulk-series-row,
  .filter-grid,
  .grid-2,
  .grid-3 {
    grid-template-columns: 1fr;
  }

  .brick-filter-grid {
    display: grid;
  }

  .brick-filter-grid > label {
    min-width: 0;
  }

  .brick-filter-tools {
    margin-left: 0;
    justify-content: space-between;
  }

  .filter-actions {
    align-items: stretch;
  }

  .panel-head {
    align-items: stretch;
    flex-direction: column;
  }

  .panel-head-actions {
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .brick-thumb-wrap {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}

@media (min-width: 1200px) {
  .container {
    width: min(1280px, calc(100vw - 32px));
    margin: 0 auto;
    padding: 16px 0 20px;
    grid-template-columns: repeat(12, minmax(0, 1fr));
    grid-template-areas:
      "manual manual manual manual manual manual manual dashboard dashboard dashboard dashboard dashboard"
      "actions actions actions actions actions actions actions actions actions actions actions actions"
      "series series series series series series series series series series series series"
      "brick brick brick brick brick brick brick brick brick brick brick brick"
      "missing missing missing missing missing missing missing missing missing missing missing missing";
    gap: 14px;
  }

  .manual-panel {
    grid-area: manual;
  }

  .dashboard {
    grid-area: dashboard;
  }

  .actions {
    grid-area: actions;
  }

  .series-panel {
    grid-area: series;
  }

  .brick-panel {
    grid-area: brick;
  }

  .missing-panel {
    grid-area: missing;
  }

  .series-layout {
    grid-template-columns: minmax(0, 1.55fr) minmax(320px, 0.95fr);
  }

  .brick-layout {
    grid-template-columns: minmax(0, 1.7fr) minmax(360px, 0.9fr);
    gap: 14px;
  }

  .brick-list-panel {
    position: sticky;
    top: 12px;
    height: calc(100vh - 40px);
  }

  .series-list-wrap {
    max-height: 340px;
  }

  .brick-list-wrap {
    height: 100%;
    max-height: 100%;
  }

  .brick-thumb-wrap {
    height: 100%;
    max-height: 100%;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .series-panel .form-card,
  .brick-panel .form-card {
    position: sticky;
    top: 12px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    overflow-x: hidden;
  }

  .missing-thumb-wrap {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  }
}
