/* Coding — vibe coding on Cloudflare */
:root {
  --bg: #0b0d12;
  --bg-2: #11141b;
  --bg-3: #171b24;
  --line: #232936;
  --fg: #e8edf4;
  --fg-dim: #8b94a7;
  --accent: #f6821f;          /* cloudflare orange */
  --accent-soft: #f6821f22;
  --good: #4ade80;
  --bad: #f87171;
  --radius: 12px;
  --font: "Avenir Next", "Noto Sans TC", ui-sans-serif, system-ui, -apple-system, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font-family: var(--font); font-size: 15px; line-height: 1.55;
}
button { font-family: inherit; }
.hidden { display: none !important; }
.view { min-height: 100vh; }

/* ---------- buttons ---------- */
.btn-primary {
  background: var(--accent); color: #1a1004; border: none; cursor: pointer;
  padding: .65rem 1.2rem; border-radius: 10px; font-weight: 700; font-size: .95rem;
  transition: transform .15s, box-shadow .15s, opacity .15s;
}
.btn-primary:hover { transform: translateY(-1px); box-shadow: 0 6px 18px var(--accent-soft); }
.btn-primary:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn-primary.small { padding: .45rem .9rem; font-size: .85rem; }
.btn-ghost {
  background: transparent; color: var(--fg-dim); border: 1px solid var(--line);
  border-radius: 9px; cursor: pointer; padding: .4rem .7rem; font-size: .95rem;
  text-decoration: none; transition: color .15s, border-color .15s;
}
.btn-ghost:hover { color: var(--fg); border-color: var(--fg-dim); }

/* ---------- home ---------- */
.home-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.1rem 2rem; border-bottom: 1px solid var(--line);
}
.logo { font-weight: 800; font-size: 1.15rem; letter-spacing: .02em; }
.badge {
  font-size: .75rem; color: var(--fg-dim); border: 1px solid var(--line);
  padding: .25rem .65rem; border-radius: 99px; font-family: var(--mono);
}
.hero { max-width: 760px; margin: 0 auto; padding: 4.5rem 1.5rem 2.5rem; text-align: center; }
.hero h1 { font-size: clamp(1.8rem, 4.5vw, 2.8rem); margin: 0 0 .6rem; font-weight: 800; }
.accent { color: var(--accent); }
.sub { color: var(--fg-dim); margin: 0 0 2rem; }
.new-form { display: flex; flex-direction: column; gap: .8rem; }
.new-form textarea {
  width: 100%; resize: vertical; background: var(--bg-2); color: var(--fg);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem;
  font-family: inherit; font-size: 1rem; transition: border-color .15s;
}
.new-form textarea:focus { outline: none; border-color: var(--accent); }
.new-form-row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.fw-label { color: var(--fg-dim); font-size: .85rem; display: flex; align-items: center; gap: .5rem; }
.fw-label select {
  background: var(--bg-2); color: var(--fg); border: 1px solid var(--line);
  border-radius: 8px; padding: .45rem .6rem; font-family: inherit; font-size: .85rem;
}
.fw-label select:focus { outline: none; border-color: var(--accent); }

/* templates */
.templates { max-width: 980px; margin: 0 auto; padding: 0 1.5rem 1rem; }
.templates h2, .projects h2 { font-size: 1rem; color: var(--fg-dim); font-weight: 600; letter-spacing: .04em; }
.tmpl-grid { grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
.tmpl-card {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .9rem 1rem; cursor: pointer; transition: border-color .15s, transform .15s;
}
.tmpl-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.tmpl-card .t-icon { font-size: 1.4rem; }
.tmpl-card .t-name { font-weight: 700; margin: .3rem 0 .15rem; font-size: .92rem; }
.tmpl-card .t-desc { color: var(--fg-dim); font-size: .78rem; line-height: 1.45; }
.tmpl-card .t-fw {
  display: inline-block; margin-top: .5rem; font-family: var(--mono); font-size: .68rem;
  color: var(--accent); border: 1px solid #f6821f44; padding: .1rem .45rem; border-radius: 6px;
}

.projects { max-width: 980px; margin: 0 auto; padding: 1rem 1.5rem 4rem; }
.projects h2 { font-size: 1rem; color: var(--fg-dim); font-weight: 600; letter-spacing: .04em; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: .9rem; }
.card {
  background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem 1.1rem; cursor: pointer; position: relative;
  transition: border-color .15s, transform .15s;
}
.card:hover { border-color: var(--accent); transform: translateY(-2px); }
.card .name { font-weight: 700; margin-bottom: .35rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta { font-size: .78rem; color: var(--fg-dim); font-family: var(--mono); }
.card .pub-dot { color: var(--good); }
.card .del {
  position: absolute; top: .55rem; right: .6rem; background: none; border: none;
  color: var(--fg-dim); cursor: pointer; font-size: .9rem; opacity: 0; transition: opacity .15s, color .15s;
}
.card:hover .del { opacity: 1; }
.card .del:hover { color: var(--bad); }
.empty-hint { color: var(--fg-dim); font-size: .9rem; padding: 1rem 0; }

/* ---------- project view ---------- */
.topbar {
  display: flex; align-items: center; gap: .9rem;
  padding: .7rem 1.1rem; border-bottom: 1px solid var(--line); background: var(--bg-2);
}
.proj-name { font-weight: 700; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: .7rem; }
.pub-link { color: var(--good); font-size: .85rem; text-decoration: none; }
.pub-link:hover { text-decoration: underline; }

.workspace {
  display: grid; grid-template-columns: minmax(340px, 30%) 1fr;
  height: calc(100vh - 57px);
}
@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: 45% 55%; }
}

/* chat */
.chat-pane { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-width: 0; }
.chat-log { flex: 1; overflow-y: auto; padding: 1.1rem; display: flex; flex-direction: column; gap: .8rem; }
.msg { max-width: 92%; padding: .7rem .95rem; border-radius: var(--radius); font-size: .92rem; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--accent-soft); border: 1px solid #f6821f44; }
.msg.assistant { align-self: flex-start; background: var(--bg-3); border: 1px solid var(--line); }
.msg.error { align-self: stretch; background: #f8717115; border: 1px solid #f8717155; color: var(--bad); }
.file-chips { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .5rem; }
.fchip {
  font-family: var(--mono); font-size: .72rem; padding: .2rem .55rem; border-radius: 6px;
  background: var(--bg-2); border: 1px solid var(--line); color: var(--fg-dim);
}
.fchip.writing { border-color: var(--accent); color: var(--accent); animation: pulse 1.2s infinite; }
.fchip.done { border-color: #4ade8055; color: var(--good); }
.fchip.fail { border-color: #f8717155; color: var(--bad); }
@keyframes pulse { 50% { opacity: .5; } }
.typing { color: var(--fg-dim); font-size: .85rem; padding-left: .3rem; }
.typing::after { content: "▍"; animation: pulse 1s infinite; }

.chat-form { display: flex; gap: .6rem; padding: .8rem; border-top: 1px solid var(--line); background: var(--bg-2); }
.chat-form textarea {
  flex: 1; resize: none; background: var(--bg); color: var(--fg);
  border: 1px solid var(--line); border-radius: 10px; padding: .6rem .8rem;
  font-family: inherit; font-size: .92rem;
}
.chat-form textarea:focus { outline: none; border-color: var(--accent); }

/* right pane */
.right-pane { display: flex; flex-direction: column; min-width: 0; }
.tabs { display: flex; align-items: center; gap: .4rem; padding: .55rem .9rem; border-bottom: 1px solid var(--line); background: var(--bg-2); }
.tab {
  background: none; border: none; color: var(--fg-dim); cursor: pointer;
  padding: .35rem .8rem; border-radius: 8px; font-size: .88rem; font-weight: 600;
  transition: color .15s, background .15s;
}
.tab:hover { color: var(--fg); }
.tab.active { color: var(--fg); background: var(--bg-3); }
.tabs .btn-ghost { margin-left: auto; }
.tabs .btn-ghost + .btn-ghost { margin-left: 0; }
.tab-body { flex: 1; display: flex; min-height: 0; background: #fff; }
#tab-code { background: var(--bg); }
#preview { width: 100%; height: 100%; border: none; background: #fff; }

.file-pane { width: 200px; border-right: 1px solid var(--line); display: flex; flex-direction: column; flex-shrink: 0; }
.file-pane-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: .55rem .9rem .35rem; color: var(--fg-dim); font-size: .75rem; font-weight: 700;
  letter-spacing: .06em; text-transform: uppercase;
}
.file-list { flex: 1; overflow-y: auto; padding: 0 0 .6rem; }
.file-item {
  display: flex; align-items: center; gap: .4rem; width: 100%; text-align: left; background: none; border: none;
  color: var(--fg-dim); font-family: var(--mono); font-size: .8rem; cursor: pointer;
  padding: .4rem .9rem; transition: color .15s, background .15s;
}
.file-item:hover { color: var(--fg); background: var(--bg-2); }
.file-item.active { color: var(--accent); background: var(--bg-3); }
.file-item.writing::after { content: "✎"; color: var(--accent); animation: pulse 1.2s infinite; margin-left: auto; }

.editor-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.editor-toolbar {
  display: flex; align-items: center; gap: .7rem; padding: .45rem .9rem;
  border-bottom: 1px solid var(--line); background: var(--bg-2);
}
.editor-filename { font-family: var(--mono); font-size: .8rem; color: var(--fg); }
.editor-meta { font-family: var(--mono); font-size: .72rem; color: var(--fg-dim); }
.editor-actions { margin-left: auto; display: flex; gap: .4rem; }
.btn-ghost.tiny, .btn-primary.tiny { padding: .25rem .6rem; font-size: .75rem; }
.btn-ghost.danger:hover { color: var(--bad); border-color: var(--bad); }
.file-content {
  flex: 1; margin: 0; overflow: auto; padding: 1rem 1.2rem;
  font-family: var(--mono); font-size: .8rem; line-height: 1.6; color: #c9d4e3;
  tab-size: 2; background: transparent;
}
.file-content code { background: transparent !important; padding: 0 !important; font-family: inherit; }
.file-editor {
  flex: 1; resize: none; border: none; outline: none; background: #0d1117; color: #c9d4e3;
  font-family: var(--mono); font-size: .8rem; line-height: 1.6; padding: 1rem 1.2rem; tab-size: 2;
}

/* streaming indicator */
.stream-indicator { color: var(--accent); font-size: .78rem; font-family: var(--mono); animation: pulse 1.2s infinite; margin-left: .3rem; }

/* topbar extras */
.fw-badge { font-family: var(--mono); font-size: .7rem; color: var(--fg-dim); border: 1px solid var(--line); padding: .15rem .5rem; border-radius: 6px; }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50; min-width: 280px; max-height: 320px; overflow-y: auto;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.5); padding: .35rem;
}
.menu-item {
  display: block; width: 100%; text-align: left; background: none; border: none; cursor: pointer;
  color: var(--fg); padding: .5rem .7rem; border-radius: 7px; font-size: .82rem;
}
.menu-item:hover { background: var(--bg-3); }
.menu-item .m-time { color: var(--fg-dim); font-family: var(--mono); font-size: .7rem; display: block; }
.menu-empty { color: var(--fg-dim); font-size: .8rem; padding: .6rem .7rem; }

/* error bar (preview runtime errors) */
.error-bar {
  display: flex; align-items: center; gap: .8rem; padding: .5rem 1.1rem;
  background: #f8717118; border-bottom: 1px solid #f8717155; color: var(--bad); font-size: .85rem;
}
.error-bar span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--mono); font-size: .78rem; }
.btn-fix {
  background: var(--bad); color: #2a0a0a; border: none; border-radius: 8px; cursor: pointer;
  padding: .35rem .8rem; font-weight: 700; font-size: .8rem;
}
.small-x { padding: .2rem .5rem; }

/* logs tab */
#tab-logs { background: var(--bg); }
.logs-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.logs-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: .6rem 1rem; border-bottom: 1px solid var(--line); color: var(--fg-dim); font-size: .82rem;
}
.logs-list { flex: 1; overflow-y: auto; font-family: var(--mono); font-size: .76rem; }
.log-row {
  display: grid; grid-template-columns: 150px 52px 1fr 44px; gap: .8rem;
  padding: .35rem 1rem; border-bottom: 1px solid #1a1f29; color: var(--fg-dim);
}
.log-row .l-path { color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-row .l-status { text-align: right; }
.log-row.err { background: #f8717110; }
.log-row.err .l-status { color: var(--bad); }
.log-row .l-note { grid-column: 1 / -1; color: var(--bad); padding-left: .5rem; }
.logs-empty { padding: 1.2rem; color: var(--fg-dim); font-size: .85rem; font-family: var(--font); }

/* select & edit */
#select-btn.active { color: var(--accent); border-color: var(--accent); animation: pulse 1.2s infinite; }
.selected-chip {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  margin: 0 .8rem; padding: .4rem .7rem; background: var(--accent-soft);
  border: 1px solid #f6821f44; border-radius: 9px; font-size: .8rem;
}
.selected-chip code { font-family: var(--mono); color: var(--accent); font-size: .75rem; }

/* toasts */
.toast-wrap { position: fixed; bottom: 1.2rem; right: 1.2rem; z-index: 100; display: flex; flex-direction: column; gap: .5rem; }
.toast {
  background: var(--bg-3); border: 1px solid var(--line); border-left: 3px solid var(--accent);
  color: var(--fg); padding: .7rem 1rem; border-radius: 10px; font-size: .85rem; max-width: 360px;
  box-shadow: 0 8px 24px rgba(0,0,0,.4); animation: slideIn .2s ease-out; word-break: break-all;
}
.toast.good { border-left-color: var(--good); }
.toast.bad { border-left-color: var(--bad); }
.toast a { color: var(--accent); }
@keyframes slideIn { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
