:root {
  --bg: #f5f4ee;
  --panel: #ffffff;
  --border: #e6e3d8;
  --input-bg: #f5f4ee;
  --hover-bg: #efeee6;
  --text: #201f1c;
  --muted: #83807a;
  --accent: #1a1a19;
  --accent-text: #ffffff;
  --brand: #d97757;
  --serif: "Source Serif 4", Georgia, "Iowan Old Style", "Palatino Linotype", ui-serif, serif;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@import url('https://fonts.googleapis.com/css2?family=Source+Serif+4:opsz,wght@8..60,400;8..60,500;8..60,600&family=Inter:wght@400;500;600;700&display=swap');

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  max-width: 100vw;
}

img, video { max-width: 100%; height: auto; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

.messages, .session-list {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.logo {
  font-family: var(--serif);
  font-weight: 500;
  font-size: 22px;
  letter-spacing: 0.2px;
}
.logo span { color: var(--brand); }

/* --- index / login --- */
.center-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 24px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 36px;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 8px 30px rgba(32, 31, 28, 0.06);
}

.card h2 { margin: 0 0 4px; font-size: 16px; color: var(--muted); font-weight: 500; }

.dept-list { display: flex; flex-direction: column; gap: 8px; }
.dept-list a {
  display: block;
  padding: 12px 14px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
}
.dept-list a:hover { border-color: var(--accent); }

input[type="password"], input[type="text"] {
  background: var(--input-bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 14px;
  border-radius: 12px;
  font-size: 14px;
}

button {
  background: var(--accent);
  color: var(--accent-text);
  border: none;
  padding: 10px 16px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
}
button:hover { opacity: 0.88; }
button.secondary { background: var(--input-bg); color: var(--text); border: 1px solid var(--border); }

.error { color: #ff6b6b; font-size: 13px; min-height: 16px; }

/* --- chat app --- */
.app { display: flex; height: 100%; position: relative; }

.mobile-topbar { display: none; }
.sidebar-backdrop { display: none; }

@media (max-width: 768px) {
  .mobile-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--panel);
  }
  .mobile-topbar button {
    background: none;
    color: var(--text);
    font-size: 20px;
    padding: 4px 8px;
  }
  .chat-main { width: 100%; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 80vw;
    max-width: 300px;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar .sidebar-header { padding-top: calc(16px + env(safe-area-inset-top)); }
  .sidebar .session-list { padding-bottom: env(safe-area-inset-bottom); }
  .mobile-topbar { padding-top: calc(12px + env(safe-area-inset-top)); }
  .sidebar-backdrop.open {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 10;
  }
  .msg { max-width: 88%; }
}

.sidebar {
  width: 260px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--panel);
}
.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.dept-name { color: var(--muted); font-size: 13px; }

.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10px;
  border-radius: 8px;
  cursor: pointer;
  gap: 6px;
}
.session-item:hover { background: var(--hover-bg); }
.session-item.active { background: #eee8d9; }
.session-item .meta { overflow: hidden; }
.session-item .title { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .date { font-size: 11px; color: var(--muted); }
.session-item .del { color: var(--muted); background: none; padding: 2px 6px; font-size: 14px; }
.session-item .del:hover { color: #ff6b6b; }

.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.messages { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 24px; display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.msg { max-width: 70%; padding: 12px 14px; border-radius: 10px; white-space: pre-wrap; line-height: 1.4; overflow-wrap: anywhere; min-width: 0; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-text); }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); }
.msg .attachments { font-size: 12px; color: var(--muted); margin-top: 6px; }

.composer-wrap { padding: 12px 16px 16px; }
.composer-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 14px 16px 10px;
  box-shadow: 0 6px 24px rgba(32, 31, 28, 0.05);
}
.composer-card textarea {
  width: 100%;
  resize: none;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  padding: 4px 2px 10px;
  font-size: 15px;
  font-family: inherit;
  min-height: 28px;
  max-height: 200px;
  overflow-y: auto;
  overflow-wrap: anywhere;
  display: block;
}
.composer-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.composer-row-left, .composer-row-right { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  background: var(--input-bg);
  color: var(--text);
  border: 1px solid var(--border);
  width: 32px;
  height: 32px;
  padding: 0;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mode-toggle {
  display: flex;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
}
.mode-btn {
  background: none;
  color: var(--muted);
  border: none;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13px;
}
.mode-btn.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }

.model-picker { position: relative; }
.model-btn {
  background: none;
  color: var(--text);
  border: 1px solid transparent;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 13px;
}
.model-btn:hover { background: var(--input-bg); }
.model-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(32, 31, 28, 0.12);
  padding: 6px;
  width: 220px;
  z-index: 30;
}
.model-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  width: 100%;
  background: none;
  color: var(--text);
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  text-align: left;
}
.model-option:hover { background: var(--input-bg); }
.model-option-name { font-size: 13px; font-weight: 600; }
.model-option-desc { font-size: 11px; color: var(--muted); }

.model-menu-divider { height: 1px; background: var(--border); margin: 6px 2px; }
.model-option-row { flex-direction: row; align-items: center; justify-content: space-between; }
.model-option-value { font-size: 12px; color: var(--muted); }
.effort-back { color: var(--muted); font-size: 12px; margin-bottom: 4px; }

.switch { position: relative; display: inline-block; width: 34px; height: 20px; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--border); border-radius: 999px; transition: 0.15s;
}
.switch-slider::before {
  content: ""; position: absolute; height: 16px; width: 16px; left: 2px; top: 2px;
  background: white; border-radius: 50%; transition: 0.15s;
}
.switch input:checked + .switch-slider { background: var(--accent); }
.switch input:checked + .switch-slider::before { transform: translateX(14px); }

.plus-picker { position: relative; }
.plus-menu { left: 0; right: auto; width: 220px; }

.composer-project-picker { position: relative; }
#composerProjectMenu { width: 240px; bottom: calc(100% + 8px); left: 0; right: auto; }
#composerProjectSearch { width: 100%; margin-bottom: 6px; }
.composer-project-list { display: flex; flex-direction: column; gap: 2px; max-height: 160px; overflow-y: auto; }
.composer-project-list .model-option.selected { background: #eee8d9; }
.md-body p { margin: 0 0 8px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body code { background: rgba(0,0,0,0.06); padding: 1px 5px; border-radius: 4px; font-size: 0.9em; }
.md-body pre { background: rgba(0,0,0,0.06); padding: 10px; border-radius: 6px; overflow-x: auto; margin: 0 0 8px; }
.md-body pre code { background: none; padding: 0; }
.md-body ul { margin: 0 0 8px; padding-left: 20px; }
.md-table-wrap { max-width: 100%; overflow-x: auto; margin: 0 0 8px; -webkit-overflow-scrolling: touch; }
.md-table { border-collapse: collapse; font-size: 0.92em; }
.md-table th, .md-table td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; white-space: nowrap; }
.md-table th { background: var(--input-bg); font-weight: 600; }

.copy-btn {
  display: inline-block;
  margin-top: 6px;
  background: none;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 3px 8px;
  font-size: 11px;
  border-radius: 6px;
}
.copy-btn:hover { color: var(--text); }

#sessionSearch {
  width: 100%;
  margin-top: 4px;
}

.pending-files { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 16px; max-width: 100%; }
.pending-files .chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  padding: 4px 6px 4px 8px;
  border-radius: 6px;
  font-size: 12px;
  max-width: 260px;
}
.pending-files .chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pending-files .chip-remove {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--muted);
  padding: 0 2px;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
}
.pending-files .chip-remove:hover { color: #c0392b; }

.area-tabs { display: flex; gap: 2px; background: var(--input-bg); border-radius: 10px; padding: 2px; margin-top: 4px; }
.area-tab { flex: 1; background: none; color: var(--muted); padding: 6px 10px; border-radius: 8px; font-size: 13px; }
.area-tab.active { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
.sidebar-footer-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--brand); color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; flex-shrink: 0;
}
.sidebar-footer-name { font-size: 13px; font-weight: 500; }
.sidebar-footer-plan { font-size: 11px; color: var(--muted); }

.code-indicator {
  margin: 0 16px 8px;
  padding: 8px 14px;
  background: #eef2ff;
  border: 1px solid #c7d2fe;
  color: #3730a3;
  border-radius: 8px;
  font-size: 12px;
}

.projects-section { padding: 4px 16px 10px; border-bottom: 1px solid var(--border); }
.projects-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin-bottom: 6px; }
.project-list { display: flex; flex-direction: column; gap: 2px; margin-bottom: 6px; }
.project-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
}
.project-item:hover { background: var(--hover-bg); }
.project-item.active { background: #eee8d9; }
.project-item .project-item-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-item .project-gear { background: none; border: none; color: var(--muted); padding: 2px 4px; font-size: 12px; flex-shrink: 0; }
.project-item .project-gear:hover { color: var(--text); }
.project-new-btn { width: 100%; font-size: 13px; padding: 6px 10px; }

.project-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(32, 31, 28, 0.35);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.project-modal {
  background: var(--panel);
  border-radius: 20px;
  padding: 20px;
  width: 100%;
  max-width: 420px;
  max-height: 80vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}
.project-modal-row { display: flex; align-items: center; gap: 8px; }
.project-modal-row input { flex: 1; }
#projectInstructionsInput {
  width: 100%;
  min-height: 80px;
  resize: vertical;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
}
.project-files-list { display: flex; flex-direction: column; gap: 4px; }
.project-file-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--input-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12px;
}
.project-file-row a { color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-file-row button { background: none; border: none; color: var(--muted); padding: 0 2px; font-size: 12px; }
.project-file-row button:hover { color: #c0392b; }
.project-modal-actions { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; }

.session-item .project-tag {
  font-size: 10px;
  color: var(--brand);
  background: #f7ece6;
  border-radius: 4px;
  padding: 1px 5px;
  margin-right: 4px;
}

.new-session-wrap { position: relative; padding: 10px 16px 0; }
.new-session-wrap #newSession { width: 100%; }
.finger-point {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  font-size: 12px;
  color: #b45309;
  white-space: nowrap;
  animation: finger-bounce 1s ease-in-out infinite;
}
@keyframes finger-bounce {
  0%, 100% { transform: translateX(0); }
  50% { transform: translateX(-4px); }
}

.context-warning {
  margin: 0 16px 8px;
  padding: 10px 14px;
  background: #fff4e5;
  border: 1px solid #f0c27b;
  color: #7a4a00;
  border-radius: 8px;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.context-warning.urgent { background: #fdeaea; border-color: #f0a6a6; color: #7a1f1f; }
.context-warning button {
  background: var(--accent);
  color: white;
  padding: 6px 12px;
  font-size: 13px;
  white-space: nowrap;
}

.stream-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.thinking.dot-only { display: inline-flex; margin-left: 4px; }
.thinking.dot-only .dot { vertical-align: middle; }

.thinking { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 14px; }
.thinking .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: #c0392b;
  animation: thinking-pulse 1s ease-in-out infinite;
}
@keyframes thinking-pulse {
  0%, 100% { opacity: 0.25; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1); }
}
