* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

/* ── APP SHELL ─────────────────────────────────────────────── */
#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ── TOP BAR ───────────────────────────────────────────────── */
.topbar {
  height: 48px;
  flex-shrink: 0;
  background: #16162a;
  border-bottom: 1px solid #2d2d4e;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
}
.topbar-left  { display: flex; align-items: center; gap: 1.5rem; }
.topbar-right { display: flex; align-items: center; gap: 1rem; font-size: .78rem; color: #475569; }
.logo { font-weight: 700; font-size: 1rem; color: #a78bfa; letter-spacing: -.3px; }
.nav-tabs { display: flex; gap: .25rem; }
.tab { padding: .35rem .85rem; border-radius: 6px; font-size: .82rem; cursor: pointer; color: #64748b; transition: all .15s; }
.tab:hover  { background: #1e1b3a; color: #c4b5fd; }
.tab.active { background: #2d1b4e; color: #a78bfa; font-weight: 600; }

/* ── MAIN LAYOUT (fills remaining height) ──────────────────── */
.layout {
  flex: 1;
  display: flex;
  overflow: hidden;   /* critical: clips children to this height */
}

/* ── SIDEBAR ───────────────────────────────────────────────── */
.sidebar {
  width: 220px;
  flex-shrink: 0;
  background: #16162a;
  border-right: 1px solid #2d2d4e;
  overflow-y: auto;
  padding: .75rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}
.sidebar-section { font-size: .68rem; text-transform: uppercase; letter-spacing: .08em; color: #64748b; margin: .5rem 0 .25rem; font-weight: 700; }
.stat-card { background: #0f0f1a; border: 1px solid #1e1e3a; border-radius: 8px; padding: .65rem .75rem; }
.stat-label { font-size: .7rem; color: #475569; margin-bottom: .3rem; }
.token-row { display: flex; justify-content: space-between; align-items: center; font-size: .73rem; padding: .25rem 0; border-bottom: 1px solid #1a1a2e; }
.token-row:last-child { border-bottom: none; }
.token-type  { color: #64748b; }
.token-count { color: #c4b5fd; font-weight: 600; font-variant-numeric: tabular-nums; }
.progress-bar-bg   { height: 5px; background: #1e1e3a; border-radius: 3px; overflow: hidden; margin-top: 4px; }
.progress-bar-fill { height: 100%; border-radius: 3px; background: linear-gradient(90deg, #0f766e, #2dd4bf); transition: width .5s; }
.progress-bar-fill.med  { background: linear-gradient(90deg, #1d4ed8, #60a5fa); }
.progress-bar-fill.warn { background: linear-gradient(90deg, #d97706, #fbbf24); }
.timer-display { font-size: .85rem; font-weight: 700; color: #6b7280; margin-bottom: .4rem; }
.timer-toggle-row { display: flex; align-items: center; justify-content: space-between; background: #0f0f1a; border: 1px solid #1e1e3a; border-radius: 7px; padding: .45rem .65rem; }
.toggle { position: relative; width: 34px; height: 18px; background: #4ade80; border-radius: 9px; cursor: pointer; transition: background .2s; flex-shrink: 0; }
.toggle[data-enabled="false"] { background: #374151; }
.toggle-knob { position: absolute; top: 2px; right: 2px; width: 14px; height: 14px; background: white; border-radius: 50%; transition: right .2s; }
.toggle[data-enabled="false"] .toggle-knob { right: 18px; }
.toggle.toggle-disabled { opacity: .35; cursor: not-allowed; }
.btn-reset   { background: #1e1b3a; border: 1px solid #3730a3; color: #818cf8; border-radius: 7px; padding: .45rem; font-size: .75rem; cursor: pointer; text-align: center; }
.btn-reset:hover { background: #2d1b4e; color: #a78bfa; }
.btn-history { background: #1e1b3a; border: 1px solid #4c1d95; color: #8b5cf6; border-radius: 7px; padding: .45rem; font-size: .75rem; cursor: pointer; text-align: center; margin-top: .25rem; }
.btn-history:hover { background: #2d1b4e; }
.sidebar-active { background: #0c1a0c; border: 1px solid #166534; border-radius: 7px; padding: .45rem .65rem; cursor: pointer; margin-top: .25rem; }
.sidebar-active:hover { background: #112211; }
.sidebar-active-header { color: #4ade80; font-size: .72rem; font-weight: 700; margin-bottom: .2rem; }
.sidebar-active-tokens { color: #4b5563; font-size: .68rem; }

/* ── CENTER ─────────────────────────────────────────────────── */
.center {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Tab panels */
.tab-content { display: none; }
.tab-content.active {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Iframe tabs */
.iframe-tab iframe { flex: 1; width: 100%; height: 100%; border: none; }

/* ── CHAT ───────────────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: #0f0f13;
}
.msg { max-width: 680px; }
.msg.user      { align-self: flex-end; }
.msg.assistant { align-self: flex-start; }
.msg-bubble { padding: .7rem 1rem; border-radius: 12px; font-size: .87rem; line-height: 1.6; word-break: break-word; }
.msg.user .msg-bubble      { background: #2d1b4e; color: #e2e8f0; border-bottom-right-radius: 3px; white-space: pre-wrap; }
.msg.assistant .msg-bubble { background: #16162a; border: 1px solid #2d2d4e; color: #cbd5e1; border-bottom-left-radius: 3px; }
.msg.assistant .msg-bubble.streaming { border-color: #7c3aed44; }
/* Markdown styles inside assistant bubbles */
.msg-bubble.md p  { margin: 0 0 .6em; }
.msg-bubble.md p:last-child { margin-bottom: 0; }
.msg-bubble.md h1,.msg-bubble.md h2,.msg-bubble.md h3 { color: #c4b5fd; margin: .8em 0 .4em; font-size: 1em; font-weight: 700; }
.msg-bubble.md ul,.msg-bubble.md ol { padding-left: 1.4em; margin: .4em 0; }
.msg-bubble.md li { margin: .2em 0; }
.msg-bubble.md code { background: #0f0f1a; border: 1px solid #2d2d4e; border-radius: 4px; padding: .1em .35em; font-size: .83em; color: #a78bfa; font-family: 'Fira Code', monospace; }
.msg-bubble.md pre { background: #0a0a12; border: 1px solid #2d2d4e; border-radius: 8px; padding: .85rem 1rem; overflow-x: auto; margin: .6em 0; }
.msg-bubble.md pre code { background: none; border: none; padding: 0; color: #94a3b8; font-size: .82em; }
.msg-bubble.md blockquote { border-left: 3px solid #4c1d95; padding-left: .75rem; margin: .5em 0; color: #64748b; }
.msg-bubble.md strong { color: #e2e8f0; }
.msg-bubble.md a { color: #818cf8; }
.msg-meta      { font-size: .68rem; color: #4b5563; margin-top: .35rem; display: flex; gap: .5rem; align-items: center; }
.msg-cost      { color: #d97706; }
.msg-thinking  { align-self: flex-start; padding: .55rem 1rem; background: #16162a; border: 1px solid #2d2d4e; border-radius: 10px; border-bottom-left-radius: 3px; font-size: .82rem; color: #6d7fa8; display: flex; align-items: center; gap: .3rem; }
.think-label   { color: #94a3b8; }
.think-timer   { color: #4b5563; font-size: .75rem; min-width: 2.5rem; }
.thinking-dots { display: flex; gap: 3px; }
.thinking-dots span { width: 5px; height: 5px; background: #7c3aed; border-radius: 50%; animation: thinking-pulse 1.4s ease-in-out infinite; }
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes thinking-pulse { 0%,80%,100% { opacity:.25; transform:translateY(0); } 40% { opacity:1; transform:translateY(-3px); } }
.chat-empty { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .6rem; pointer-events: none; }
.chat-empty-icon  { font-size: 2.5rem; color: #374151; }
.chat-empty-title { font-size: 1.1rem; font-weight: 700; color: #6b7280; }
.chat-empty-sub   { font-size: .8rem; color: #4b5563; text-align: center; max-width: 300px; }
.reset-notice  { font-size: .72rem; color: #4b5563; text-align: center; padding: .4rem; background: #0f0f1a; border-top: 1px dashed #1e1e3a; flex-shrink: 0; }

.chat-input-wrap {
  flex-shrink: 0;
  padding: .85rem 1.25rem;
  border-top: 1px solid #2d2d4e;
  background: #16162a;
  display: flex;
  gap: .6rem;
  align-items: flex-end;
}
.chat-input {
  flex: 1;
  background: #0f0f1a;
  border: 1px solid #2d2d4e;
  border-radius: 10px;
  color: #e2e8f0;
  padding: .7rem .9rem;
  font-size: .87rem;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  font-family: inherit;
  outline: none;
}
.chat-input:focus { border-color: #7c3aed; }
.btn-send { background: #7c3aed; border: none; color: white; border-radius: 9px; padding: .6rem 1rem; cursor: pointer; font-size: .82rem; font-weight: 600; height: 42px; white-space: nowrap; }
.btn-send:hover    { background: #6d28d9; }
.btn-send:disabled { background: #3730a3; cursor: not-allowed; }
.btn-stop { background: #7f1d1d; border: 1px solid #ef4444; color: #fca5a5; border-radius: 9px; padding: .6rem 1rem; cursor: pointer; font-size: .82rem; font-weight: 600; height: 42px; white-space: nowrap; }
.btn-stop:hover { background: #991b1b; }
.btn-attach { cursor: pointer; font-size: 1.1rem; padding: .3rem .4rem; border-radius: 7px; color: #64748b; transition: color .15s; line-height: 42px; }
.btn-attach:hover { color: #a78bfa; }
.attach-preview { display: flex; align-items: center; gap: .4rem; background: #1a1a2e; border: 1px solid #2d2d4e; border-radius: 7px; padding: .3rem .6rem; font-size: .75rem; color: #94a3b8; white-space: nowrap; }
.attach-name { max-width: 160px; overflow: hidden; text-overflow: ellipsis; }
.attach-remove { background: none; border: none; color: #64748b; cursor: pointer; font-size: .8rem; padding: 0 .1rem; }
.attach-remove:hover { color: #f87171; }

/* ── KUMA PANEL ─────────────────────────────────────────────── */
.kuma-panel {
  width: 240px;
  flex-shrink: 0;
  background: #16162a;
  border-left: 1px solid #2d2d4e;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.kuma-header {
  flex-shrink: 0;
  padding: .75rem 1rem;
  border-bottom: 1px solid #2d2d4e;
  font-size: .78rem;
  font-weight: 700;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: .07em;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.kuma-list {
  flex: 1;
  overflow-y: auto;
  padding: .5rem;
  display: flex;
  flex-direction: column;
  gap: .3rem;
}
.kuma-loading { color: #4b5563; font-size: .78rem; padding: .5rem; }
.kuma-item { display: flex; align-items: center; justify-content: space-between; padding: .45rem .6rem; border-radius: 7px; background: #0f0f1a; border: 1px solid #1e1e3a; font-size: .75rem; }
.kuma-name { color: #94a3b8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; margin-right: .4rem; }
.kuma-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.kuma-dot.up      { background: #4ade80; box-shadow: 0 0 5px #22c55e44; }
.kuma-dot.down    { background: #f87171; box-shadow: 0 0 5px #ef444444; }
.kuma-dot.unknown { background: #fbbf24; }
.kuma-ping { font-size: .68rem; color: #4b5563; margin-right: .3rem; }

/* ── KUMA FULL TAB ──────────────────────────────────────────── */
.kuma-tab-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: .5rem;
  align-content: start;
  background: #0f0f13;
}
.kuma-tab-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: .55rem .75rem;
  border-radius: 8px;
  background: #16162a;
  border: 1px solid #1e1e3a;
  font-size: .78rem;
}
.kuma-tab-item.down    { border-color: #7f1d1d; background: #1a0a0a; }
.kuma-tab-item.unknown { border-color: #78350f; background: #1a1200; }
.kuma-tab-name { color: #94a3b8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; margin-right: .5rem; }
.kuma-tab-item.down .kuma-tab-name    { color: #fca5a5; }
.kuma-tab-item.unknown .kuma-tab-name { color: #fbbf24; }

/* ── BADGES & MISC ──────────────────────────────────────────── */
.badge      { background: #1a2e1a; color: #4ade80; border: 1px solid #166534; padding: .2rem .55rem; border-radius: 20px; font-size: .72rem; font-weight: 600; }
.badge.warn { background: #2a1f0a; color: #fbbf24; border-color: #92400e; }
.topbar-link { color: #64748b; text-decoration: none; font-size: .78rem; }
.topbar-link:hover { color: #a78bfa; }
.btn-link { background: none; border: none; color: #64748b; cursor: pointer; font-size: .78rem; }
.btn-link:hover { color: #f87171; }

/* ── LOGIN ──────────────────────────────────────────────────── */
.hidden { display: none !important; }
.login-screen { position: fixed; inset: 0; background: #0f0f13; display: flex; align-items: center; justify-content: center; z-index: 200; }
.login-box { background: #16162a; border: 1px solid #2d2d4e; border-radius: 14px; padding: 2.5rem 2rem; width: 320px; display: flex; flex-direction: column; gap: 1rem; }
.login-box h1 { font-size: 1.4rem; color: #a78bfa; text-align: center; }
.login-box input { background: #0f0f1a; border: 1px solid #2d2d4e; border-radius: 8px; color: #e2e8f0; padding: .6rem .8rem; font-size: .9rem; outline: none; width: 100%; }
.login-box input:focus { border-color: #7c3aed; }
.login-box button { background: #7c3aed; border: none; color: white; border-radius: 8px; padding: .65rem; font-size: .9rem; font-weight: 600; cursor: pointer; }
.error { color: #f87171; font-size: .8rem; text-align: center; }

/* ── HISTORY DRAWER ─────────────────────────────────────────── */
.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 100; display: flex; justify-content: flex-start; }
.drawer { width: 420px; background: #16162a; border-right: 1px solid #2d2d4e; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; }
.drawer-header { padding: .9rem 1.1rem; border-bottom: 1px solid #2d2d4e; display: flex; justify-content: space-between; align-items: center; flex-shrink: 0; }
.drawer-title { font-size: .9rem; font-weight: 700; color: #c4b5fd; }
.drawer-close { color: #475569; cursor: pointer; font-size: 1.1rem; }
.drawer-list { display: flex; flex-direction: column; }
.session-item { padding: .75rem 1.1rem; border-bottom: 1px solid #1e1e3a; display: flex; flex-direction: column; gap: .35rem; }
.session-item:hover { background: #1a1a2e; }
.session-row { display: flex; justify-content: space-between; align-items: center; }
.session-date { font-size: .72rem; color: #4b5563; }
.session-summary { font-size: .8rem; color: #94a3b8; line-height: 1.45; }
.session-tokens { font-size: .68rem; color: #4b5563; }
.session-tokens span { color: #6b7280; }
.btn-resume { background: #2d1b4e; border: 1px solid #7c3aed; color: #a78bfa; border-radius: 6px; padding: .25rem .65rem; font-size: .72rem; cursor: pointer; white-space: nowrap; }
.btn-resume:hover { background: #3d2060; }
.tag-old { background: #1a1a2a; color: #4b5563; border: 1px solid #2d2d4e; border-radius: 4px; padding: .15rem .4rem; font-size: .67rem; }
.tag-active { background: #0c2a1a; color: #4ade80; border: 1px solid #166534; border-radius: 4px; padding: .15rem .4rem; font-size: .67rem; font-weight: 600; }
.session-active { border-left: 2px solid #4ade80; background: #0c1a0c; }
.drawer-empty { color: #4b5563; font-size: .82rem; text-align: center; padding: 2rem; }

/* ── RESPONSIVE ─────────────────────────────────────────────── */
@media (max-width: 900px) {
  .sidebar, .kuma-panel { display: none; }
}
@media (max-width: 600px) {
  .topbar { padding: 0 .75rem; flex-wrap: wrap; height: auto; min-height: 48px; padding-top: .35rem; padding-bottom: .35rem; gap: .25rem; }
  .topbar-left { gap: .75rem; width: 100%; }
  .topbar-right { font-size: .72rem; gap: .6rem; }
  .nav-tabs { gap: .1rem; }
  .tab { padding: .3rem .55rem; font-size: .75rem; }
  .logo { font-size: .9rem; }
  .chat-messages { padding: .75rem; }
  .chat-input-wrap { padding: .6rem .75rem; gap: .4rem; }
  .msg-bubble { font-size: .82rem; padding: .55rem .75rem; }
}
