:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #667085;
  --line: #e1e4ea;
  --soft: #eef0f4;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --danger: #d92d20;
  --warn: #b54708;
  --warn-bg: #fff4e5;
  --radius: 10px;
  --font: system-ui, -apple-system, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131519;
    --panel: #1b1e25;
    --text: #e8eaed;
    --muted: #98a2b3;
    --line: #2b303a;
    --soft: #262a33;
    --accent: #7d93ff;
    --accent-text: #10131a;
    --danger: #ff8077;
    --warn: #f5b65c;
    --warn-bg: #3a2a12;
  }
}

* { box-sizing: border-box; }
html { background: var(--bg); }
body {
  margin: 0;
  font: 14px/1.5 var(--font);
  color: var(--text);
  background: var(--bg);
}
h1, h2 { margin: 0; font-weight: 650; }
h1 { font-size: 17px; }
h2 { font-size: 15px; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 16px; height: 52px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.tabs { display: flex; gap: 4px; height: 100%; flex: 1; }
.tabs button {
  border: 0; background: none; color: var(--muted); font: inherit; font-weight: 600;
  padding: 0 14px; cursor: pointer; border-bottom: 2px solid transparent;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

main { max-width: 1280px; margin: 0 auto; padding: 16px; }
.create-layout { display: grid; grid-template-columns: minmax(340px, 460px) 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) { .create-layout { grid-template-columns: 1fr; } }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
#gen-form { display: grid; gap: 16px; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
legend, .field > label { font-weight: 600; padding: 0; margin-bottom: 6px; display: block; }
.legend-sub { font-weight: 400; color: var(--muted); font-size: 12px; margin-left: 6px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.hint.right { text-align: right; }
.error { color: var(--danger); font-size: 13px; margin: 8px 0 0; min-height: 1em; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; margin: 0; }

textarea, input[type="text"], input[type="number"], input[type="password"] {
  width: 100%; font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
}
textarea { resize: vertical; min-height: 120px; }

.btn {
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; text-align: center;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 14px;
  background: var(--panel); color: var(--text);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.block { display: flex; width: 100%; }
.btn:disabled, .btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; }

/* 모델 선택 */
.model-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.model-card {
  position: relative; display: grid; gap: 2px; cursor: pointer;
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
.model-card input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.model-card:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.model-card:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.model-label { font-weight: 650; }
.model-name { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.usage { font-size: 12px; color: var(--muted); }
.usage:empty { display: none; }
.usage.out { color: var(--danger); font-weight: 600; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 99px; background: var(--soft); color: var(--muted); }
.badge.warn { background: var(--warn-bg); color: var(--warn); justify-self: start; }

/* 비율 */
.ratio-grid { display: grid; grid-template-columns: 74px repeat(3, minmax(0, 1fr)); gap: 6px; align-items: center; }
@media (max-width: 480px) {
  main { padding: 12px; }
  .panel { padding: 12px; }
  .ratio-grid { grid-template-columns: 58px repeat(3, minmax(0, 1fr)); }
  .ratio-btn .shape { display: none; }
  .model-cards { grid-template-columns: 1fr; }
}
.ratio-head { font-size: 12px; font-weight: 600; color: var(--muted); }
.ratio-head.col { text-align: center; }
.ratio-btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 12px; cursor: pointer; color: var(--text); min-width: 0; white-space: nowrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 7px 4px;
}
.ratio-btn:hover { background: var(--soft); }
.ratio-btn[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); background: var(--soft); font-weight: 650; }
.ratio-btn .shape { display: inline-block; border: 1.5px solid currentColor; border-radius: 2px; opacity: 0.6; flex: none; }
.custom-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.custom-row .ratio-btn { padding: 7px 14px; }
.custom-row label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.custom-row input { width: 84px; padding: 6px 8px; }
.plan-info { margin-top: 10px; font-size: 13px; }
.plan-info .main { font-weight: 600; }
.plan-info .note { color: var(--warn); background: var(--warn-bg); border-radius: 6px; padding: 5px 8px; margin-top: 6px; font-size: 12px; }
.plan-info .bad { color: var(--danger); font-weight: 600; }

/* 화질 */
.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg { position: relative; display: block; text-align: center; padding: 8px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.seg small { color: var(--muted); margin-left: 4px; }
.seg:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); font-weight: 650; }
.seg:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.seg.disabled { opacity: 0.45; cursor: not-allowed; background: var(--soft); text-decoration: line-through; }
.seg.disabled input { cursor: not-allowed; }
.seg.disabled:has(input:checked) { box-shadow: none; border-color: var(--line); font-weight: inherit; }

/* 히스토리 */
.history-list { display: grid; gap: 10px; }
.card { display: grid; grid-template-columns: 112px 1fr; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
.card-body { min-width: 0; display: grid; gap: 4px; align-content: start; }
.thumb {
  width: 112px; height: 112px; padding: 0; border: 0; border-radius: 6px; overflow: hidden;
  background: var(--soft); display: grid; place-items: center; color: var(--muted); font-size: 12px; cursor: zoom-in;
}
.thumb[disabled] { cursor: default; }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }
.name {
  font: inherit; font-weight: 650; text-align: left; padding: 0; border: 0; background: none; color: var(--text);
  cursor: text; overflow-wrap: anywhere; border-bottom: 1px dashed transparent;
}
.name:hover { border-bottom-color: var(--muted); }
.name-edit { display: flex; align-items: center; gap: 4px; }
.name-edit .ext { color: var(--muted); }
.name-input { padding: 3px 6px; font-weight: 650; }
.meta { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; }
.prompt { margin: 0; color: var(--muted); font-size: 13px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.card .error { margin: 0; }
.actions { display: flex; gap: 6px; margin-top: 2px; }

/* 결과 모음 */
.gallery-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  position: sticky; top: 52px; z-index: 4; padding: 10px 12px; margin-bottom: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
}
.check-all { display: flex; align-items: center; gap: 6px; font-weight: 600; cursor: pointer; }
.spacer { flex: 1; }
.gallery-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; margin-bottom: 12px; }
.gcard { position: relative; display: grid; gap: 6px; padding: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); align-content: start; }
.gcard.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.gcard .thumb { width: 100%; height: auto; aspect-ratio: 1; }
.gcard .pick {
  position: absolute; top: 14px; left: 14px; z-index: 1; width: 22px; height: 22px; margin: 0; cursor: pointer;
  accent-color: var(--accent); border-radius: 4px; outline: 2px solid #fff; box-shadow: 0 0 0 3px rgb(0 0 0 / 0.35);
}
.gcard .meta { font-size: 12px; color: var(--muted); }

input[type="checkbox"] { accent-color: var(--accent); }

/* 다이얼로그 */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 0; max-width: min(96vw, 1100px); }
dialog::backdrop { background: rgb(0 0 0 / 0.55); }
#lightbox { width: min(96vw, 1100px); }
#lightbox .close { position: absolute; top: 8px; right: 8px; background: var(--panel); }
#lb-img { display: block; max-width: 100%; max-height: 72vh; margin: 0 auto; object-fit: contain; background: var(--soft); width: 100%; }
.lb-info { padding: 12px 16px 16px; display: grid; gap: 4px; justify-items: start; }
.lb-prompt { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; max-height: 7em; overflow: auto; }
#login { width: min(92vw, 360px); padding: 20px; }
#login form { display: grid; gap: 10px; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 20; width: min(92vw, 460px); }
.toast { background: #22262e; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / 0.3); font-size: 13px; }
.toast.error { background: #b42318; }
