:root {
  --bg: #0f172a;
  --bg-deep: #0b1120;
  --surface: #1e293b;
  --surface-2: #253349;
  --border: #334155;
  --border-soft: #2a3850;
  --text: #f8fafc;
  --text-dim: #94a3b8;
  --text-faint: #64748b;
  --accent: #f59e0b;
  --accent-soft: rgba(245, 158, 11, 0.12);
  --green: #22c55e;
  --danger: #f87171;
  --danger-soft: rgba(248, 113, 113, 0.12);
  --radius: 10px;
  --font-sans: 'IBM Plex Sans', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body { font-family: var(--font-sans); font-size: 14px; background: var(--bg); color: var(--text); overflow: hidden; }

.hidden { display: none !important; }
.mono { font-family: var(--font-mono); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }

/* LOGIN */
.login-view {
  height: 100vh; display: grid; place-items: center;
  background: radial-gradient(ellipse 60% 50% at 50% -10%, rgba(245, 158, 11, 0.10), transparent), var(--bg-deep);
}
.login-card {
  width: min(380px, 90vw); background: var(--surface);
  border: 1px solid var(--border); border-radius: 16px;
  padding: 40px 36px; text-align: center; box-shadow: var(--shadow);
}
.login-logo svg { width: 52px; height: 52px; color: var(--accent); margin-bottom: 16px; }
.login-card h1 { font-family: var(--font-mono); font-size: 22px; font-weight: 600; }
.login-sub { color: var(--text-dim); margin: 6px 0 28px; }
.login-card input {
  width: 100%; padding: 12px 14px; background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); font-size: 15px; margin-bottom: 14px;
}
.login-card input:focus { outline: none; border-color: var(--accent); }
.login-card button {
  width: 100%; padding: 12px; background: var(--accent); color: #451a03;
  font-weight: 600; font-size: 15px; border: none; border-radius: var(--radius);
  transition: background 0.2s;
}
.login-card button:hover { background: #fbbf24; }
.login-error { color: var(--danger); margin-top: 12px; font-size: 13px; }

/* LAYOUT */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100vh; }
.sidebar { background: var(--bg-deep); border-right: 1px solid var(--border-soft); display: flex; flex-direction: column; min-height: 0; }
.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 18px 16px; border-bottom: 1px solid var(--border-soft); }
.logo-icon { width: 26px; height: 26px; color: var(--accent); flex-shrink: 0; }
.logo-text { font-family: var(--font-mono); font-weight: 600; font-size: 16px; }
.sidebar-actions { padding: 12px; }

.site-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; min-height: 0; }
.site-item {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 8px 10px; background: none; border: none; border-radius: 8px;
  color: var(--text-dim); font-family: var(--font-mono); font-size: 12.5px;
  text-align: left; transition: background 0.15s, color 0.15s; overflow: hidden;
}
.site-item svg { width: 14px; height: 14px; flex-shrink: 0; opacity: 0.7; }
.site-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-item .no-tpl { margin-left: auto; font-size: 9px; color: var(--danger); border: 1px solid rgba(248,113,113,0.4); padding: 1px 4px; border-radius: 4px; }
.site-item:hover { background: var(--surface); color: var(--text); }
.site-item.active { background: var(--accent-soft); color: var(--accent); }

.sidebar-footer { display: flex; justify-content: space-between; padding: 12px 16px; border-top: 1px solid var(--border-soft); }

.ghost-btn { background: none; border: none; color: var(--text-dim); font-size: 13px; padding: 6px 10px; border-radius: 8px; transition: background 0.15s, color 0.15s; }
.ghost-btn:hover { background: var(--surface); color: var(--text); }

.secondary-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 14px; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 13px; font-weight: 600; transition: background 0.2s;
}
.secondary-btn:hover { background: var(--surface-2); }
.secondary-btn.wide { width: 100%; }

.primary-btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 10px 20px;
  background: var(--accent); color: #451a03; border: none; border-radius: var(--radius);
  font-size: 13.5px; font-weight: 600; transition: background 0.2s;
}
.primary-btn:hover { background: #fbbf24; }
.primary-btn:disabled { opacity: 0.6; cursor: wait; }
.primary-btn svg { width: 15px; height: 15px; }

/* MAIN */
.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.empty-state { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; color: var(--text-faint); }
.empty-state svg { width: 72px; height: 72px; opacity: 0.35; margin-bottom: 10px; }
.empty-state h2 { color: var(--text-dim); font-size: 18px; font-weight: 600; }

.browser { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 13px 24px; border-bottom: 1px solid var(--border-soft);
}
.site-label { font-size: 14px; font-weight: 600; color: var(--accent); }

.tabs { display: flex; gap: 4px; background: var(--bg-deep); border-radius: 9px; padding: 3px; }
.tab { padding: 6px 16px; background: none; border: none; border-radius: 7px; color: var(--text-dim); font-size: 13px; font-weight: 500; transition: background 0.15s, color 0.15s; }
.tab:hover { color: var(--text); }
.tab.active { background: var(--surface-2); color: var(--text); }

.view { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 18px 24px 24px; overflow-y: auto; }

/* FORM */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 18px; }
.form-grid label { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; color: var(--text-dim); font-weight: 500; }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid input, .form-grid textarea {
  padding: 10px 12px; background: var(--surface);
  border: 1px solid var(--border-soft); border-radius: var(--radius);
  color: var(--text); font-size: 13.5px; transition: border-color 0.2s;
}
.form-grid input:focus, .form-grid textarea:focus { outline: none; border-color: var(--accent); }
.form-grid textarea { min-height: 320px; resize: vertical; font-size: 13px; line-height: 1.6; }

.format-row { display: flex; gap: 16px; margin-bottom: 2px; }
.radio { display: flex; flex-direction: row !important; align-items: center; gap: 6px !important; font-weight: 400 !important; cursor: pointer; }
.radio input { accent-color: var(--accent); }

.publish-row { display: flex; align-items: center; justify-content: flex-end; gap: 14px; padding: 14px 0 0; }
.count-label { color: var(--text-faint); font-size: 12.5px; font-family: var(--font-mono); }

/* STEPS */
.steps { margin-top: 14px; background: var(--surface); border: 1px solid var(--border-soft); border-radius: 12px; padding: 14px 18px; }
.step-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; font-size: 13px; color: var(--text-dim); }
.step-row .s-ok { color: var(--green); }
.step-row .s-fail { color: var(--danger); }
.step-row .s-info { margin-left: auto; font-family: var(--font-mono); font-size: 11.5px; color: var(--text-faint); max-width: 50%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step-url { margin-top: 8px; font-family: var(--font-mono); font-size: 12.5px; }
.step-url a { color: var(--accent); }

/* TABLES */
.table-card { background: var(--surface); border: 1px solid var(--border-soft); border-radius: 14px; overflow: auto; }
.mc-table { width: 100%; border-collapse: collapse; }
.mc-table th {
  text-align: left; font-size: 10.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: 0.8px; color: var(--text-faint); padding: 10px 14px;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}
.mc-table td { padding: 8px 14px; border-bottom: 1px solid var(--border-soft); font-size: 12.5px; color: var(--text-dim); }
.mc-table tbody tr:last-child td { border-bottom: none; }
.mc-table tbody tr:hover { background: var(--surface-2); }
.mc-table td.num, .mc-table th.num { text-align: right; font-family: var(--font-mono); }
.mc-table .mono-cell { font-family: var(--font-mono); font-size: 12px; }
.mc-table a { color: var(--accent); text-decoration: none; }
.mc-table a:hover { text-decoration: underline; }

/* TEMPLATE */
.hint { font-size: 13px; color: var(--text-dim); line-height: 1.7; margin-bottom: 12px; }
.hint code { font-family: var(--font-mono); font-size: 11.5px; color: var(--accent); background: var(--accent-soft); padding: 1px 5px; border-radius: 4px; }
#template-editor {
  flex: 1; min-height: 380px;
  background: var(--bg-deep); border: 1px solid var(--border-soft); border-radius: var(--radius);
  color: var(--text); font-size: 12px; line-height: 1.55; padding: 14px 16px; resize: vertical;
}
#template-editor:focus { outline: none; border-color: var(--accent); }

/* MODAL */
.modal-backdrop { position: fixed; inset: 0; background: rgba(2, 6, 17, 0.7); backdrop-filter: blur(3px); display: grid; place-items: center; z-index: 50; }
.modal { width: min(460px, 92vw); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 24px; }
.modal-title { font-size: 16px; margin-bottom: 14px; }
.m-label { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; color: var(--text-dim); margin-bottom: 12px; }
.m-label input, .m-label select {
  padding: 9px 12px; background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text); font-size: 12.5px;
}
.m-label input:focus, .m-label select:focus { outline: none; border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.plain-btn { background: none; border: 1px solid var(--border); color: var(--text-dim); padding: 9px 16px; border-radius: var(--radius); font-size: 13px; }
.plain-btn:hover { background: var(--surface-2); color: var(--text); }

/* TOASTS */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 8px; z-index: 60; }
.toast { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); padding: 11px 18px; border-radius: var(--radius); font-size: 13px; box-shadow: var(--shadow); animation: toast-in 0.25s ease; max-width: 480px; }
.toast.success { border-color: var(--green); }
.toast.error { border-color: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

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

@media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { max-height: 40vh; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ═══ v2: Astro CMS ═══ */
.sidebar-search { padding: 0 12px 10px; }
.sidebar-search input {
  width: 100%; padding: 8px 12px;
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: var(--radius);
  color: var(--text); font-size: 12.5px;
}
.sidebar-search input:focus { outline: none; border-color: var(--accent); }

.site-item { justify-content: space-between; }
.site-item .s-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-item .s-meta { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.s-count { font-size: 10px; color: var(--text-faint); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; }
.s-count.dim { opacity: 0.5; }
.s-deploy { font-size: 10px; }

.site-link { font-size: 11.5px; color: var(--text-faint); text-decoration: none; }
.site-link:hover { color: var(--accent); }

.toolbar-row {
  display: flex; align-items: center; gap: 14px;
  padding-bottom: 12px;
}
.toolbar-row .editor-actions { margin-left: auto; display: flex; gap: 10px; }
.toolbar-row #files-count { flex: 1; }

.file-label { font-size: 12.5px; color: var(--accent); }

#editor {
  flex: 1; min-height: 420px; width: 100%;
  background: var(--bg-deep); border: 1px solid var(--border-soft); border-radius: var(--radius);
  color: var(--text); font-size: 13px; line-height: 1.65; padding: 16px 18px; resize: none;
}
#editor:focus { outline: none; border-color: var(--accent); }
.editor-hint { margin-top: 8px; font-size: 11.5px; color: var(--text-faint); }

.danger-btn {
  display: inline-flex; align-items: center; gap: 8px; padding: 9px 16px;
  background: var(--danger-soft); color: var(--danger);
  border: none; border-radius: var(--radius); font-size: 13px; font-weight: 600;
}
.danger-btn:hover { background: rgba(248, 113, 113, 0.22); }

.row-clickable { cursor: pointer; }
.row-clickable:hover td:first-child { color: var(--accent); }

.draft-badge {
  font-size: 9.5px; color: var(--accent); border: 1px solid rgba(245, 158, 11, 0.4);
  border-radius: 4px; padding: 1px 5px; font-family: var(--font-mono);
}

.deploy-panel {
  position: fixed; bottom: 20px; right: 20px; width: min(560px, 90vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); z-index: 40; overflow: hidden;
}
.deploy-panel header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 10px 12px 16px; border-bottom: 1px solid var(--border-soft);
  font-size: 13px; font-weight: 600;
}
#deploy-log {
  max-height: 300px; overflow-y: auto; padding: 12px 16px;
  font-size: 11.5px; line-height: 1.6; color: var(--text-dim); white-space: pre-wrap;
}
.modal-sub { font-size: 12.5px; color: var(--text-dim); margin: -6px 0 14px; line-height: 1.5; }
.icon-btn { background: none; border: none; color: var(--text-dim); width: 28px; height: 28px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ═══ AI bar ═══ */
.ai-bar {
  background: linear-gradient(135deg, rgba(245, 158, 11, 0.07), rgba(245, 158, 11, 0.02));
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 14px;
}
.ai-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.ai-row input[type="text"] {
  flex: 1; min-width: 220px; padding: 9px 12px;
  background: var(--bg-deep); border: 1px solid var(--border-soft); border-radius: var(--radius);
  color: var(--text); font-size: 13px;
}
.ai-row input[type="text"]:focus { outline: none; border-color: var(--accent); }
.ai-deploy-check { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); cursor: pointer; white-space: nowrap; }
.ai-deploy-check input { accent-color: var(--accent); cursor: pointer; }

.ai-proposals { margin-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.ai-prop-head { font-size: 12px; color: var(--text-dim); margin-bottom: 2px; }
.ai-prop {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border-soft); border-radius: 10px;
  padding: 10px 14px;
}
.ai-prop-body { flex: 1; min-width: 0; }
.ai-prop-title { font-size: 13.5px; font-weight: 600; color: var(--text); }
.ai-prop-src {
  font-size: 9.5px; font-family: var(--font-mono); color: var(--accent);
  border: 1px solid rgba(245, 158, 11, 0.4); border-radius: 4px; padding: 1px 5px; margin-left: 6px;
}
.ai-prop-why { font-size: 12px; color: var(--text-dim); margin-top: 3px; line-height: 1.5; }
.ai-prop-go { flex-shrink: 0; padding: 7px 16px; }

/* ═══ autoblog ═══ */
.ab-box { margin-top: 12px; border-top: 1px dashed rgba(245, 158, 11, 0.3); padding-top: 10px; }
.ab-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ab-select {
  padding: 7px 10px; background: var(--bg-deep); border: 1px solid var(--border-soft);
  border-radius: 8px; color: var(--text); font-size: 12px;
}
.ab-select:focus { outline: none; border-color: var(--accent); }
.ab-global { margin-left: auto; color: var(--text-faint); }
.q-status { flex-shrink: 0; width: 22px; text-align: center; font-size: 13px; }
.q-published { opacity: 0.65; }
.q-writing { border-color: rgba(245, 158, 11, 0.5); }
.ai-prop a { color: var(--accent); text-decoration: none; }
.ai-prop a:hover { text-decoration: underline; }
