:root {
  --bg: #070b16;
  --panel: #111a2e;
  --panel-border: #1e2d4a;
  --panel-header: #0d1528;
  --gold: #d4af37;
  --gold-light: #f2cf6b;
  --gold-dim: rgba(212, 175, 55, 0.15);
  --ink: #e8ecf5;
  --ink-muted: #8b95a8;
  --ink-dim: #5a6478;
  --accent-blue: #3b82f6;
  --success: #34d399;
  --error: #f87171;
  --input-bg: #0a1020;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
  --font-sans: 'Noto Sans TC', system-ui, sans-serif;
  --font-serif: 'Noto Serif TC', Georgia, serif;
  --font-mono: 'JetBrains Mono', 'Consolas', monospace;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  min-height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 18px 24px 28px;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(212, 175, 55, 0.06), transparent),
    var(--bg);
}

/* Header */
.app-header {
  text-align: center;
  margin-bottom: 14px;
  flex-shrink: 0;
}

.app-title {
  font-family: var(--font-serif);
  font-size: 1.85rem;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.06em;
  text-shadow: 0 2px 16px rgba(212, 175, 55, 0.3);
}

.app-subtitle {
  margin-top: 6px;
  color: var(--ink-muted);
  font-size: 1rem;
}

.server-banner {
  margin: 0 0 14px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: rgba(248, 113, 113, 0.12);
  border: 1px solid rgba(248, 113, 113, 0.45);
  color: #fca5a5;
  font-size: 0.95rem;
  line-height: 1.5;
}

.server-banner.ok {
  background: rgba(52, 211, 153, 0.1);
  border-color: rgba(52, 211, 153, 0.4);
  color: var(--success);
}

/* Main layout */
.app-main {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(440px, 48%) 1fr;
  gap: 18px;
  align-items: start;
}

.left-column {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Panel */
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: visible;
  flex-shrink: 0;
}

.panel-grow { min-height: auto; }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--panel-header);
  border-bottom: 1px solid var(--panel-border);
  gap: 10px;
}

.panel-collapsible.is-collapsed .panel-header {
  border-bottom: none;
}

.panel-header-toggle {
  width: 100%;
  border: none;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
  transition: background 0.15s;
}

.panel-header-toggle:hover {
  background: rgba(212, 175, 55, 0.06);
}

.panel-header-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.panel-connect-summary {
  font-size: 0.82rem;
  color: var(--ink-dim);
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.panel-connect-summary.is-ready {
  color: var(--success);
}

.panel-chevron {
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--ink-muted);
  border-bottom: 2px solid var(--ink-muted);
  transform: rotate(45deg);
  transition: transform 0.2s;
  flex-shrink: 0;
}

.panel-collapsible.is-collapsed .panel-chevron {
  transform: rotate(-45deg);
}

.panel-collapsible.is-collapsed .panel-body {
  display: none;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 1.08rem;
}

.section-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--gold-dim);
  color: var(--gold);
  border-radius: 5px;
  font-size: 0.88rem;
  font-weight: 700;
}

.section-icon { font-size: 1.15rem; }

.badge {
  font-size: 0.82rem;
  padding: 3px 10px;
  border-radius: 20px;
  font-weight: 500;
}

.badge-gold {
  background: var(--gold-dim);
  color: var(--gold);
  border: 1px solid rgba(212, 175, 55, 0.3);
}

.panel-body {
  padding: 16px 18px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.panel-body-grow { min-height: auto; }
.panel-body-result {
  min-height: calc(100vh - 160px);
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
}

/* Fields */
.field-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field-group.flex-grow { flex: none; }

.field-row {
  display: flex;
  gap: 12px;
}

.flex-1 { flex: 1; min-width: 0; }

label {
  font-size: 0.95rem;
  color: var(--ink-muted);
  font-weight: 500;
}

.hint {
  color: var(--ink-dim);
  font-weight: 400;
  font-size: 0.88rem;
}

.value-badge {
  float: right;
  color: var(--gold);
  font-family: var(--font-mono);
  font-size: 0.92rem;
}

.input {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  color: var(--ink);
  padding: 10px 12px;
  font-family: var(--font-sans);
  font-size: 0.95rem;
  transition: border-color 0.15s, box-shadow 0.15s;
  outline: none;
}

.input:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(212, 175, 55, 0.15);
}

.input::placeholder { color: var(--ink-dim); }

.select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238b95a8' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

.textarea-key {
  font-family: var(--font-mono);
  font-size: 0.92rem;
  resize: vertical;
  min-height: 88px;
}

.textarea-benchmark {
  min-height: 72px;
  resize: vertical;
  line-height: 1.55;
}

.textarea-prompt {
  min-height: 200px;
  resize: vertical;
  line-height: 1.65;
}

.slider {
  width: 100%;
  accent-color: var(--gold);
  cursor: pointer;
}

/* Checkboxes */
.checkbox-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 0.94rem;
  color: var(--ink-muted);
}

.checkbox-label input[type="checkbox"] {
  accent-color: var(--gold);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* Buttons */
.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-family: var(--font-sans);
  font-size: 0.94rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-sm { padding: 7px 14px; font-size: 0.9rem; }

.btn-ghost {
  background: transparent;
  border-color: var(--panel-border);
  color: var(--ink-muted);
}

.btn-ghost:hover { border-color: var(--ink-dim); color: var(--ink); }

.btn-outline {
  background: transparent;
  border-color: rgba(212, 175, 55, 0.35);
  color: var(--gold);
}

.btn-outline:hover {
  background: var(--gold-dim);
  border-color: var(--gold);
}

.btn-primary,
.btn-generate {
  background: linear-gradient(135deg, var(--gold) 0%, #b8942e 100%);
  color: #1a1400;
  font-weight: 700;
  font-size: 1.05rem;
  padding: 14px 22px;
  border: none;
  box-shadow: 0 2px 12px rgba(212, 175, 55, 0.3);
}

.btn-primary:hover,
.btn-generate:hover {
  background: linear-gradient(135deg, var(--gold-light) 0%, var(--gold) 100%);
  box-shadow: 0 4px 16px rgba(212, 175, 55, 0.4);
}

.btn-generate { width: 100%; margin-top: 4px; }

.btn-success {
  background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
  color: #fff;
  border: none;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(34, 197, 94, 0.25);
}

.btn-success:hover {
  background: linear-gradient(135deg, #4ade80 0%, #22c55e 100%);
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.35);
}

/* Keyword tags */
.keyword-panel {
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.keyword-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 28px;
}

.keyword-tags:empty::before {
  content: "尚未新增關鍵字";
  color: var(--ink-dim);
  font-size: 0.88rem;
}

.keyword-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px 5px 12px;
  background: rgba(59, 130, 246, 0.18);
  border: 1px solid rgba(59, 130, 246, 0.35);
  border-radius: 999px;
  color: #93c5fd;
  font-size: 0.88rem;
  line-height: 1.2;
}

.keyword-tag-text {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.keyword-tag-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: #f87171;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.keyword-tag-remove:hover {
  background: rgba(248, 113, 113, 0.15);
}

.keyword-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.keyword-input-row .input {
  flex: 1;
  min-width: 0;
}

.keyword-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.keyword-status {
  font-size: 0.82rem;
  color: var(--success);
  min-height: 1.2em;
}

.keyword-status.is-warn {
  color: #fbbf24;
}

/* Tabs */
.tab-bar {
  display: flex;
  gap: 4px;
  background: var(--input-bg);
  border-radius: var(--radius-sm);
  padding: 3px;
  border: 1px solid var(--panel-border);
}

.tab-bar-sm { width: fit-content; }

.tab-btn {
  flex: 1;
  padding: 9px 14px;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--ink-muted);
  font-family: var(--font-sans);
  font-size: 0.94rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

.tab-bar-sm .tab-btn { flex: unset; padding: 7px 16px; font-size: 0.92rem; }

.tab-btn:hover { color: var(--ink); }

.tab-btn.active {
  background: var(--gold);
  color: #1a1400;
  font-weight: 600;
}

/* Upload */
.upload-area {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.upload-preview {
  width: 100%;
  margin-top: 4px;
}

.upload-preview img {
  max-width: 100%;
  max-height: 120px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--panel-border);
}

/* Status */
.status-msg {
  font-size: 0.9rem;
  color: var(--ink-dim);
  min-height: 1.4em;
  line-height: 1.5;
  word-break: break-word;
}

.gen-status { margin-top: 4px; }

/* API Key blocks */
.api-block {
  padding: 14px;
  background: var(--input-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.api-block + .api-block { margin-top: 0; }

.api-block-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.api-block-status {
  font-size: 0.8rem;
  color: #f87171;
  min-height: 1.5em;
  line-height: 1.45;
  word-break: break-word;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  background: transparent;
  border: none;
}

.api-block-status:empty {
  display: none;
}

.api-block-status.success,
.api-block-status.error,
.api-block-status.info {
  color: #f87171;
  background: transparent;
  border: none;
}

.btn.is-loading {
  opacity: 0.75;
  pointer-events: none;
}

/* Result panel */
.panel-result {
  position: sticky;
  top: 18px;
  min-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
}

.result-model-label {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--ink-dim);
  max-width: 360px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.result-actions {
  display: flex;
  gap: 4px;
}

.result-view {
  flex: 1;
  min-height: 480px;
  display: none;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--panel-border);
}

.result-view-active { display: flex; flex-direction: column; }

#preview-frame {
  flex: 1;
  width: 100%;
  min-height: 480px;
  border: none;
  background: #fff;
}

.code-editor {
  flex: 1;
  min-height: 480px;
  overflow: auto;
  background: var(--input-bg);
  padding: 16px 18px;
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.88rem;
  line-height: 1.7;
  color: #a8d4a8;
  tab-size: 2;
}

.code-editor code {
  white-space: pre-wrap;
  word-break: break-word;
}

/* Syntax highlight tokens */
.tok-tag { color: #7ec8e3; }
.tok-attr { color: #f2cf6b; }
.tok-str { color: #98c379; }
.tok-comment { color: #5a6478; font-style: italic; }
.tok-doctype { color: #c678dd; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--panel-border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--ink-dim); }

.status-msg.success { color: var(--success); }
.status-msg.error { color: var(--error); }
.status-msg.info { color: var(--accent-blue); }

/* Responsive */
@media (max-width: 960px) {
  .app-main {
    grid-template-columns: 1fr;
  }

  .panel-result {
    position: static;
    min-height: 560px;
  }
}

@media (max-width: 520px) {
  .app { padding: 14px; }
  .app-title { font-size: 1.55rem; }
  .field-row { flex-direction: column; }
  .tab-btn { font-size: 0.85rem; padding: 8px 10px; }
}
