:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --border: #e4e7ef;
  --text: #1a1d28;
  --muted: #6b7185;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #eaf0ff;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --warning: #b54708;
  --warning-soft: #fffaeb;
  --success: #027a48;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow: 0 4px 16px rgba(16, 24, 40, 0.06);
  --font-th: 'IBM Plex Sans Thai', 'Inter', system-ui, -apple-system, Segoe UI, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1117;
    --surface: #161b22;
    --surface-2: #1c222b;
    --border: #2a313c;
    --text: #e6e8ee;
    --muted: #98a0b3;
    --primary: #60a5fa;
    --primary-hover: #93c5fd;
    --primary-soft: #1c2942;
    --danger: #f97066;
    --danger-soft: #2a1715;
    --warning: #fdb022;
    --warning-soft: #2a1f0e;
    --success: #6cdba5;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-th);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  backdrop-filter: saturate(180%) blur(6px);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand .logo {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 10px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 16px; }
.brand-text .muted { font-size: 12px; }

.topbar-actions { display: flex; gap: 12px; align-items: center; }
.lang-toggle {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px;
  border: 1px solid var(--border);
}
.lang-toggle button {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 6px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.lang-toggle button.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 24px 60px;
}

.hero h1 {
  font-size: 28px;
  margin: 0 0 8px;
  letter-spacing: -0.01em;
}
.hero p { margin: 0; max-width: 760px; }

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

.disclaimer {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--warning-soft);
  color: var(--warning);
  border: 1px solid color-mix(in srgb, var(--warning) 25%, transparent);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  margin: 20px 0 24px;
}
.disclaimer strong { display: block; margin-bottom: 2px; }
.disclaimer p { margin: 0; color: var(--text); }

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}
@media (max-width: 900px) { .grid { grid-template-columns: 1fr; } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow-sm);
}
.card h2 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 12px;
}
.card h2:not(:first-of-type) { margin-top: 22px; }

.key-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 8px;
  border-radius: 999px;
  margin-left: 8px;
  vertical-align: 1px;
}
.key-badge.unset { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.key-badge.set { background: color-mix(in srgb, var(--success) 14%, transparent); color: var(--success); border: 1px solid color-mix(in srgb, var(--success) 30%, transparent); }
.key-badge.saved { background: var(--primary-soft); color: var(--primary); border: 1px solid color-mix(in srgb, var(--primary) 30%, transparent); }

.key-row { display: flex; gap: 8px; align-items: stretch; margin-bottom: 8px; }
.key-input-wrap { position: relative; flex: 1; }
.key-input-wrap input {
  width: 100%;
  font: inherit; font-size: 14px;
  padding: 10px 38px 10px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: 'SF Mono', 'Consolas', monospace;
  letter-spacing: 0.02em;
}
.key-input-wrap input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.icon-btn {
  position: absolute; top: 50%; right: 6px;
  transform: translateY(-50%);
  width: 30px; height: 30px;
  display: grid; place-items: center;
  background: transparent;
  color: var(--muted);
  border: 0;
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); background: var(--surface-2); }
.checkbox {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.checkbox input { accent-color: var(--primary); }
.key-help { margin: 6px 0 0; }
.key-help a { color: var(--primary); text-decoration: none; }
.key-help a:hover { text-decoration: underline; }

.dropzone {
  border: 1.5px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 28px 18px;
  text-align: center;
  cursor: pointer;
  color: var(--muted);
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.drag {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  outline: none;
}
.dropzone p { margin: 8px 0 0; }

.thumbs {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
}
.thumbs li {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.thumbs img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumbs .rm {
  position: absolute; top: 4px; right: 4px;
  width: 22px; height: 22px;
  border-radius: 50%;
  border: 0; cursor: pointer;
  background: rgba(0,0,0,0.65); color: #fff;
  display: grid; place-items: center;
  font-size: 14px; line-height: 1;
}
.thumbs .nm {
  position: absolute; left: 0; right: 0; bottom: 0;
  font-size: 10px; padding: 3px 6px;
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
  color: #fff;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.format-options { display: grid; gap: 8px; }
.opt {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.opt:hover { border-color: color-mix(in srgb, var(--primary) 50%, var(--border)); }
.opt input { margin-top: 3px; accent-color: var(--primary); }
.opt:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}
.opt div { display: flex; flex-direction: column; gap: 2px; }
.opt strong { font-weight: 600; font-size: 14px; }

.seg {
  display: inline-flex;
  background: var(--surface-2);
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: inline-block;
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.seg input:checked + span {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.advanced { margin-top: 18px; }
.advanced summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  user-select: none;
  padding: 6px 0;
}
.advanced[open] summary { color: var(--text); }
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; font-size: 13px; }
.field span { color: var(--muted); }
.field textarea, .field select, .field input[type=text] {
  font: inherit;
  font-size: 14px;
  padding: 10px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  resize: vertical;
}
.field textarea:focus, .field select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

button.primary {
  margin-top: 22px;
  width: 100%;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--primary);
  color: #fff;
  border: 0;
  font: inherit;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, transform 0.05s;
}
button.primary:hover:not(:disabled) { background: var(--primary-hover); }
button.primary:active:not(:disabled) { transform: translateY(1px); }
button.primary:disabled { opacity: 0.55; cursor: not-allowed; }

button.ghost {
  display: inline-flex; align-items: center; gap: 6px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  font: inherit; font-size: 13px;
  padding: 7px 11px;
  border-radius: 6px;
  cursor: pointer;
}
button.ghost:hover:not(:disabled) { color: var(--text); border-color: var(--primary); }
button.ghost:disabled { opacity: 0.55; cursor: not-allowed; }

.result-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 12px;
}
.result-head h2 { margin: 0; }
.result-actions { display: inline-flex; gap: 8px; }

.status {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 12px;
}
.status.idle { background: var(--surface-2); color: var(--muted); }
.status.loading { background: var(--primary-soft); color: var(--primary); }
.status.error { background: var(--danger-soft); color: var(--danger); }
.status.success { background: color-mix(in srgb, var(--success) 12%, transparent); color: var(--success); }

.output {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 18px 20px;
  min-height: 220px;
  font-size: 14px;
  line-height: 1.7;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.output:empty::before {
  content: attr(data-placeholder);
  color: var(--muted);
}
.output h1, .output h2, .output h3, .output h4 {
  margin: 1.1em 0 0.4em;
  line-height: 1.3;
}
.output h1 { font-size: 18px; }
.output h2 { font-size: 16px; text-transform: none; letter-spacing: 0; color: var(--text); }
.output h3 { font-size: 14px; color: var(--primary); }
.output h2:first-child, .output h1:first-child { margin-top: 0; }
.output ul, .output ol { padding-left: 22px; margin: 0.4em 0; }
.output li { margin: 0.15em 0; }
.output strong { color: var(--text); }
.output code {
  background: var(--surface);
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  border: 1px solid var(--border);
}
.output hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }

.spin {
  width: 14px; height: 14px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  display: inline-block;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.footer {
  margin-top: 32px;
  text-align: center;
}
