:root {
  --bg: #10140f;
  --bg-2: #171d15;
  --panel: #1c2419;
  --ink: #e7eadc;
  --muted: #8f977a;
  --line: rgba(212, 240, 106, 0.12);
  --lime: #d4f06a;
  --lime-dim: #9bb84a;
  --amber: #e7a04a;
  --rose: #e07a6a;
  --user: #24301c;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
html { background: var(--bg); }

body {
  margin: 0;
  color: var(--ink);
  font-family: "IBM Plex Sans", "PingFang SC", "Noto Sans SC", "Source Han Sans SC", sans-serif;
  background:
    radial-gradient(1200px 600px at 80% -10%, rgba(212, 240, 106, 0.08), transparent 55%),
    radial-gradient(800px 500px at -10% 110%, rgba(231, 160, 74, 0.07), transparent 50%),
    var(--bg);
  letter-spacing: 0.01em;
}

body::before {
  content: "";
  pointer-events: none;
  position: fixed;
  inset: 0;
  opacity: 0.035;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='140' height='140'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='2'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  z-index: 0;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }

.hidden { display: none !important; }

#login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(24px + var(--safe-top)) 20px calc(24px + var(--safe-bottom));
  position: relative;
  z-index: 1;
}

.login-card {
  width: min(420px, 100%);
  max-width: 100%;
  background: linear-gradient(180deg, #1f281c 0%, #151b13 100%);
  border: 1px solid var(--line);
  border-radius: 28px;
  padding: 36px 28px 28px;
  box-shadow: var(--shadow);
}

.kicker {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--lime);
  margin-bottom: 14px;
}

.login-card h1, .brand {
  font-family: "Fraunces", "Iowan Old Style", "Songti SC", serif;
  font-weight: 600;
  font-size: 42px;
  line-height: 0.95;
  margin: 0 0 10px;
  letter-spacing: -0.03em;
}

.sub {
  color: var(--muted);
  margin: 0 0 28px;
  line-height: 1.6;
}

.login-card input {
  width: 100%;
  background: #0e130c;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  outline: none;
}

.login-card input:focus, textarea:focus, select:focus {
  border-color: var(--lime);
}

.primary {
  width: 100%;
  margin-top: 14px;
  border: 0;
  background: var(--lime);
  color: #15180f;
  font-weight: 650;
  border-radius: 14px;
  padding: 13px 16px;
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 12px;
  padding: 8px 12px;
}

.error {
  color: var(--rose);
  min-height: 1.2em;
  margin-top: 10px;
  font-size: 14px;
}

#app {
  position: relative;
  z-index: 1;
  height: 100dvh;
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "top top"
    "side chat"
    "side composer";
}

#topbar {
  grid-area: top;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(16, 20, 15, 0.88);
  backdrop-filter: blur(16px);
  position: relative;
  z-index: 25;
  overflow: visible;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  background: #d4f06a;
  color: #15180f;
  display: grid;
  place-items: center;
  font-family: "Fraunces", serif;
  font-weight: 700;
}

.brand-name {
  font-family: "Fraunces", "Songti SC", serif;
  font-size: 20px;
  letter-spacing: -0.03em;
}

.grow { flex: 1; }

.engine-switch, .mode-switch {
  display: flex;
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
}

.engine-switch button, .mode-switch button {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 13px;
}

.engine-switch button.active, .mode-switch button.active {
  background: var(--lime);
  color: #171d12;
  font-weight: 650;
}

#sidebar {
  grid-area: side;
  border-right: 1px solid var(--line);
  background: rgba(22, 28, 20, 0.9);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.side-head, .side-foot {
  padding: 12px;
  display: flex;
  gap: 8px;
}

.side-head .primary { margin: 0; }

#session-list {
  overflow: auto;
  padding: 0 8px 12px;
}

.session-item, .cwd-chip {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 12px;
  padding: 10px 10px;
  margin-bottom: 4px;
}

.session-item:hover, .session-item.active {
  background: var(--user);
}

.session-item .t {
  display: block;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .m {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-family: "IBM Plex Mono", monospace;
  margin-top: 3px;
}

#chat {
  grid-area: chat;
  overflow: auto;
  padding: 18px 16px 8px;
}

.msg {
  max-width: 760px;
  margin: 0 auto 16px;
}

.msg.user .bubble {
  background: var(--user);
  border: 1px solid var(--line);
  border-radius: 18px 18px 6px 18px;
  padding: 12px 14px;
  margin-left: 18%;
  white-space: pre-wrap;
}

.msg.assistant .bubble {
  padding: 4px 2px 8px;
  line-height: 1.7;
}

.msg.assistant .bubble p { margin: 0 0 0.8em; }
.msg.assistant .bubble p:last-child { margin-bottom: 0; }
.msg.assistant pre {
  background: #0c100b;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  overflow: auto;
  font-size: 12.5px;
}
.msg.assistant code {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.92em;
}
.msg.assistant a { color: var(--lime); }

.thought, .tools {
  max-width: 760px;
  margin: 0 auto 10px;
  background: #141a12;
  border: 1px dashed var(--line);
  border-radius: 12px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 12px;
}

.thought summary, .tools summary {
  cursor: pointer;
  color: var(--lime-dim);
  font-family: "IBM Plex Mono", monospace;
}

.tool-row {
  padding: 4px 0;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}

#composer {
  grid-area: composer;
  padding: 10px 12px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: rgba(16, 20, 15, 0.94);
}

.composer-box {
  max-width: 760px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 8px;
}

textarea {
  width: 100%;
  resize: none;
  min-height: 48px;
  max-height: 160px;
  background: transparent;
  border: 0;
  outline: none;
  padding: 10px;
  line-height: 1.45;
}

.composer-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  justify-content: flex-end;
}

.send {
  border: 0;
  background: var(--lime);
  color: #15180f;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  font-size: 18px;
  font-weight: 700;
}

.send.stop { background: var(--rose); color: white; }

.icon-btn {
  border: 1px solid var(--line);
  background: #141b12;
  color: var(--lime);
  min-width: 44px;
  height: 44px;
  border-radius: 14px;
  font-size: 12px;
  font-weight: 650;
  padding: 0 8px;
}

.icon-btn:disabled { opacity: 0.45; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(176px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 40;
  max-width: min(86vw, 420px);
  background: #24301c;
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 13px;
  box-shadow: var(--shadow);
}

#app.dragover #composer {
  outline: 2px dashed var(--lime);
  outline-offset: -6px;
}

.meta-row {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
}

select {
  background: #0e130c;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 8px;
  max-width: 100%;
}

#terminal {
  grid-area: chat / chat / composer / composer;
  display: none;
  background: #000;
}

#terminal.visible { display: block; }
#terminal iframe {
  width: 100%;
  height: 100%;
  border: 0;
}

.files-menu {
  position: relative;
  flex: 0 0 auto;
}

.files-toggle {
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--muted);
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 13px;
}

.files-toggle[aria-expanded="true"] {
  background: var(--lime);
  color: #171d12;
  font-weight: 650;
}

.files-pop {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: min(440px, calc(100vw - 24px));
  max-height: min(72vh, 640px);
  display: flex;
  flex-direction: column;
  background: linear-gradient(180deg, #1f281c 0%, #151b13 100%);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  z-index: 40;
  overflow: hidden;
}

.files-pop-head {
  padding: 12px 14px 6px;
  border-bottom: 1px solid var(--line);
}

.files-pop-kicker {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 10px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--lime);
  margin-bottom: 4px;
}

.files-pop-path {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 13px;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.files-pop-hint {
  color: var(--muted);
  font-size: 12px;
  padding: 8px 14px 6px;
}

.files-pop-tree {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 4px 8px 12px;
  -webkit-overflow-scrolling: touch;
}

.tree-row {
  width: 100%;
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) auto auto;
  gap: 6px;
  align-items: center;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 10px;
  padding: 8px 8px 8px calc(8px + var(--d, 0) * 18px);
  color: var(--ink);
}

.tree-row:hover, .tree-row:focus-visible {
  background: var(--user);
}

.tree-twist {
  display: inline-block;
  color: var(--lime-dim);
  font-size: 12px;
  line-height: 1;
  transform-origin: center;
}

.tree-twist.open { transform: rotate(90deg); }
.tree-twist.leaf { color: var(--muted); font-size: 9px; }

.tree-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.tree-row.dim .tree-name { color: var(--muted); }

.tree-size {
  color: var(--muted);
  font-size: 11px;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  white-space: nowrap;
}

.tree-dl {
  border: 1px solid var(--line);
  background: #141b12;
  color: var(--lime);
  border-radius: 8px;
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 650;
}

.tree-empty, .tree-loading {
  color: var(--muted);
  padding: 16px 12px;
  font-size: 13px;
}

.menu-btn { display: none; }

.install-hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--lime);
  box-shadow: 0 0 10px var(--lime);
}

.status-dot.busy { background: var(--amber); animation: pulse 1s infinite; }
.status-dot.off { background: var(--muted); box-shadow: none; }

@keyframes pulse {
  50% { opacity: 0.4; }
}

@media (max-width: 480px) {
  .login-card { padding: 28px 18px 20px; border-radius: 22px; }
  .login-card h1, .brand { font-size: 32px; }
}

@media (max-width: 860px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "top"
      "chat"
      "composer";
  }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 320px);
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    z-index: 20;
    padding-top: var(--safe-top);
  }
  #sidebar.open { transform: none; }
  .menu-btn { display: inline-flex; }
  .scrim {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 15;
  }
  .scrim.show { display: block; }
  .msg.user .bubble { margin-left: 8%; }
  .brand-name { display: none; }
  .engine-switch button, .mode-switch button { padding: 6px 10px; }
  #topbar {
    display: grid;
    grid-template-columns: auto auto 1fr auto auto;
    grid-template-areas:
      "menu brand . engine files"
      "modes modes modes modes modes";
    align-items: center;
  }
  .menu-btn { grid-area: menu; }
  .brand-mark { grid-area: brand; }
  .grow { display: none; }
  .engine-switch { grid-area: engine; }
  .files-menu { grid-area: files; justify-self: end; }
  .mode-switch { grid-area: modes; justify-self: start; }
  .files-pop { max-height: min(68dvh, 560px); }
  .tree-size { display: none; }
}
