:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel-2: #f8f9fb;
  --line: #e2e6ec;
  --text: #16191f;
  --muted: #687182;
  --accent: #1f5eff;
  --accent-ink: #ffffff;
  --accent-soft: #e8efff;
  --danger: #d23b3b;
  --ok: #188a55;
  --grid: rgba(31, 94, 255, .06);
  --canvas: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 2px 8px rgba(16, 24, 40, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --panel: #161a21;
    --panel-2: #1b2029;
    --line: #2a313c;
    --text: #e8ebf0;
    --muted: #8b95a5;
    --accent: #5b8bff;
    --accent-ink: #0b0f17;
    --accent-soft: #1d2a4a;
    --grid: rgba(91, 139, 255, .07);
    --canvas: #f7f7f5;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", "Malgun Gothic", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 38px; padding: 0 16px; border-radius: 9px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font: inherit; font-weight: 600;
  cursor: pointer; text-decoration: none; transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; }
.icon-btn {
  min-width: 32px; height: 30px; padding: 0 8px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--panel); color: var(--text); font: inherit; cursor: pointer;
}
.icon-btn:hover { border-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- brand / top bar ---------- */
.brand { display: flex; align-items: center; gap: 10px; }
.brand div { display: flex; flex-direction: column; line-height: 1.2; }
.brand strong { font-size: 15px; letter-spacing: -.01em; }
.brand span { color: var(--muted); font-size: 12px; }
.brand-lg strong { font-size: 18px; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.topbar-right { display: flex; align-items: center; gap: 12px; }

/* ---------- layout ---------- */
.layout {
  display: grid; gap: 16px; padding: 16px 20px 32px; max-width: 1480px; margin: 0 auto;
  grid-template-columns: 320px minmax(0, 1fr);
  grid-template-areas: "controls viewer" "history history";
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.controls { grid-area: controls; padding: 16px; align-self: start; }
.viewer-panel { grid-area: viewer; display: flex; flex-direction: column; min-height: 560px; overflow: hidden; }
.history { grid-area: history; padding: 16px; }
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; grid-template-areas: "controls" "viewer" "history"; padding: 12px 16px 24px; }
  .viewer-panel { min-height: 440px; }
  .topbar { padding: 10px 16px; }
  .brand span { display: none; }
}

/* ---------- dropzone ---------- */
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
  padding: 22px 12px; border: 1.5px dashed var(--line); border-radius: 10px; cursor: pointer;
  background: var(--panel-2); color: var(--muted); transition: border-color .15s, background .15s;
}
.dropzone strong { color: var(--text); font-size: 14px; word-break: break-all; }
.dropzone span { font-size: 12px; }
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dropzone.has-file { border-style: solid; border-color: var(--accent); }

/* ---------- options ---------- */
.opts fieldset { border: 0; margin: 16px 0 0; padding: 0; }
.opts legend { font-weight: 700; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 8px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 3px; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 6px 0; border-radius: 7px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg input:checked + span { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }
.hint { margin: 6px 0 0; font-size: 12px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 10px; }
.row input[type=range] { flex: 1; accent-color: var(--accent); }
.num { min-width: 34px; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.check { display: flex; align-items: center; gap: 6px; margin-top: 6px; cursor: pointer; }
.check input { accent-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; font-weight: 500; }
.field small { color: var(--muted); font-weight: 400; }
input[type=number], input:not([type]), input[type=password], select {
  height: 36px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font: inherit; width: 100%;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#convert { margin-top: 18px; height: 42px; }
.status { min-height: 20px; margin: 8px 0 0; font-size: 13px; color: var(--muted); }
.status.err { color: var(--danger); }
.status.ok { color: var(--ok); }

/* ---------- viewer ---------- */
.viewer-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--line); gap: 8px; }
.tabs { display: flex; gap: 4px; }
.tab { border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600; padding: 6px 12px; border-radius: 7px; cursor: pointer; }
.tab.active { background: var(--accent-soft); color: var(--accent); }
.zoom { display: flex; gap: 4px; }
.viewport {
  position: relative; flex: 1; overflow: hidden; cursor: grab; touch-action: none;
  background-color: var(--panel-2);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
}
.viewport.panning { cursor: grabbing; }
.viewport img { position: absolute; left: 0; top: 0; transform-origin: 0 0; user-select: none; background: var(--canvas); box-shadow: 0 2px 12px rgba(0,0,0,.12); max-width: none; }
.empty { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; color: var(--muted); padding: 16px; }
.busy { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; background: color-mix(in srgb, var(--panel) 75%, transparent); font-weight: 600; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.result { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; padding: 12px; border-top: 1px solid var(--line); }
.stats { display: flex; gap: 20px; margin: 0; }
.stats div { display: flex; flex-direction: column; }
.stats dt { font-size: 11px; color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.downloads { display: flex; flex-wrap: wrap; gap: 6px; }
.btn-dl { min-width: 64px; }
.btn-dl:not(.btn-ghost) { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-dl.disabled { opacity: .35; pointer-events: none; }

/* ---------- history ---------- */
.history-head { display: flex; align-items: baseline; justify-content: space-between; }
.history h2 { font-size: 15px; margin: 0 0 10px; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 8px; }
.history-list li { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; cursor: pointer; background: var(--panel-2); }
.history-list li:hover, .history-list li.current { border-color: var(--accent); }
.history-list .h-main { flex: 1; min-width: 0; }
.history-list .h-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-list .h-sub { font-size: 12px; color: var(--muted); }
.history-list .h-del { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; padding: 2px 6px; border-radius: 6px; }
.history-list .h-del:hover { color: var(--danger); background: var(--panel); }
.history-empty { color: var(--muted); }

/* ---------- AI mode ---------- */
.seg-3 { grid-template-columns: repeat(3, 1fr); }
.hint.warn { color: var(--danger); }
.opts legend small { text-transform: none; letter-spacing: 0; font-weight: 400; }
textarea {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text); font: inherit; resize: vertical; min-height: 64px;
}
textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.busy strong { font-size: 15px; }
.busy-log { list-style: none; margin: 4px 0 0; padding: 0; font-size: 12px; color: var(--muted); text-align: center; font-variant-numeric: tabular-nums; max-width: 90%; }
.busy-log li:last-child { color: var(--text); font-weight: 600; }
.summary { margin: 0; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--accent-soft); font-size: 13px; white-space: pre-wrap; }
.history-list li.failed .h-sub { color: var(--danger); }

/* ---------- login ---------- */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.login-form { display: flex; flex-direction: column; gap: 14px; margin-top: 24px; }
.login-form label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.login-form .btn { height: 42px; margin-top: 4px; }
.error { margin: 0; color: var(--danger); font-size: 13px; }
