/* ===== CSS Custom Properties ===== */
:root {
  /* Backgrounds */
  --parchment: #f5f4ed;
  --ivory: #faf9f5;
  --dark-surface: #30302e;

  /* Brand */
  --terracotta: #c96442;
  --coral: #d97757;

  /* Text */
  --near-black: #141413;
  --olive-gray: #5e5d59;
  --stone-gray: #87867f;
  --charcoal: #4d4c48;
  --warm-silver: #b0aea5;

  /* Buttons & Borders */
  --warm-sand: #e8e6dc;
  --border-cream: #f0eee6;
  --border-warm: #e8e6dc;
  --ring-warm: #d1cfc5;

  /* Accent */
  --focus-blue: #3898ec;
  --error-crimson: #b53333;

  /* Typography */
  --font-serif: Georgia, 'Times New Roman', serif;
  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;

  /* Shadows */
  --shadow-whisper: 0 4px 24px rgba(0, 0, 0, 0.05);
  --shadow-ring: 0 0 0 1px var(--ring-warm);
}

/* ===== Reset & Base ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--near-black);
  background: var(--parchment);
  -webkit-font-smoothing: antialiased;
}

/* ===== Login Screen ===== */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 16px;
}

.login-card {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 12px;
  box-shadow: var(--shadow-whisper);
  padding: 48px 40px;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.login-title {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 28px;
  line-height: 1.2;
  color: var(--near-black);
  margin-bottom: 8px;
}

.login-subtitle {
  font-size: 14px;
  color: var(--olive-gray);
  margin-bottom: 32px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-error {
  color: var(--error-crimson);
  font-size: 13px;
  margin-top: 4px;
}

/* ===== Shared Form Elements ===== */
.input {
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--near-black);
  background: var(--ivory);
  border: 1px solid var(--border-warm);
  border-radius: 12px;
  padding: 10px 14px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  width: 100%;
}

.input:focus {
  border-color: var(--focus-blue);
  box-shadow: 0 0 0 3px rgba(56, 152, 236, 0.15);
}

.input::placeholder {
  color: var(--stone-gray);
}

.select {
  font-family: var(--font-sans);
  font-size: 14px;
  color: var(--near-black);
  background: var(--ivory);
  border: 1px solid var(--border-warm);
  border-radius: 8px;
  padding: 6px 28px 6px 10px;
  outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%235e5d59' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  cursor: pointer;
  min-height: 36px;
}

.select:focus {
  border-color: var(--focus-blue);
  box-shadow: 0 0 0 3px rgba(56, 152, 236, 0.15);
}

/* ===== Buttons ===== */
.btn {
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background-color 0.15s, opacity 0.15s;
  min-height: 44px;
  min-width: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-brand {
  background: var(--terracotta);
  color: #fff;
  box-shadow: var(--shadow-ring);
}

.btn-brand:hover:not(:disabled) {
  background: var(--coral);
}

.btn-dark {
  background: var(--dark-surface);
  color: var(--warm-silver);
  box-shadow: var(--shadow-ring);
}

.btn-dark:hover:not(:disabled) {
  background: #3a3a38;
}

.btn-ghost {
  background: transparent;
  color: var(--olive-gray);
  padding: 8px;
}

.btn-ghost:hover {
  background: var(--warm-sand);
}

/* ===== App Layout ===== */
.app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--ivory);
  border-bottom: 1px solid var(--border-cream);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
  min-width: 0;
}

.header-title {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 18px;
  line-height: 1.3;
  color: var(--near-black);
  flex-shrink: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.usage-badge {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--olive-gray);
  background: var(--parchment);
  border: 1px solid var(--border-warm);
  border-radius: 6px;
  padding: 4px 10px;
  white-space: nowrap;
}

/* ===== Main Content ===== */
.main {
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* Chat Area */
.chat-area {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== Messages ===== */
.message {
  max-width: 720px;
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 15px;
  line-height: 1.6;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-user {
  background: var(--warm-sand);
  color: var(--near-black);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}

.message-assistant {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  color: var(--near-black);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.message-status {
  align-self: center;
  color: var(--stone-gray);
  font-size: 13px;
  font-style: italic;
  padding: 4px 0;
  background: none;
}

/* Message content */
.message p {
  margin-bottom: 8px;
}

.message p:last-child {
  margin-bottom: 0;
}

.message strong {
  font-weight: 600;
}

.message code {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--parchment);
  border: 1px solid var(--border-warm);
  border-radius: 4px;
  padding: 1px 5px;
}

.message pre {
  background: var(--near-black);
  color: var(--warm-silver);
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  line-height: 1.6;
  position: relative;
  max-height: 500px;
  overflow-y: auto;
}

.message pre code {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

/* Override highlight.js background to match our theme */
.message pre code.hljs {
  background: transparent !important;
  padding: 0 !important;
}

/* Markdown tables */
.message table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 14px;
}

.message th, .message td {
  border: 1px solid var(--border-warm);
  padding: 6px 10px;
  text-align: left;
}

.message th {
  background: var(--parchment);
  font-weight: 600;
}

/* Markdown lists */
.message ul, .message ol {
  margin: 8px 0;
  padding-left: 24px;
}

.message li {
  margin-bottom: 4px;
}

/* Markdown headings */
.message h1, .message h2, .message h3 {
  font-family: var(--font-serif);
  font-weight: 500;
  margin: 12px 0 6px;
}

.message h1 { font-size: 20px; }
.message h2 { font-size: 17px; }
.message h3 { font-size: 15px; }

/* Markdown blockquotes */
.message blockquote {
  border-left: 3px solid var(--terracotta);
  padding: 4px 12px;
  margin: 8px 0;
  color: var(--olive-gray);
  background: var(--parchment);
  border-radius: 0 4px 4px 0;
}

/* Markdown hr */
.message hr {
  border: none;
  border-top: 1px solid var(--border-warm);
  margin: 12px 0;
}

/* Streaming cursor */
.streaming-cursor::after {
  content: '';
  display: inline-block;
  width: 8px;
  height: 18px;
  background: var(--terracotta);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink-cursor 0.8s step-end infinite;
}

@keyframes blink-cursor {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ===== Tool Use Blocks ===== */
.tool-block {
  margin: 8px 0;
  border: 1px solid var(--border-warm);
  border-radius: 8px;
  overflow: hidden;
}

.tool-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--parchment);
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--olive-gray);
  user-select: none;
  min-height: 44px;
}

.tool-header:hover {
  background: var(--border-cream);
}

.tool-chevron {
  transition: transform 0.15s;
  flex-shrink: 0;
}

.tool-block.open .tool-chevron {
  transform: rotate(90deg);
}

.tool-body {
  display: none;
  padding: 12px;
  background: var(--parchment);
  border-top: 1px solid var(--border-warm);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  color: var(--charcoal);
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 300px;
  overflow-y: auto;
}

.tool-block.open .tool-body {
  display: block;
}

/* ===== Input Bar ===== */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border-cream);
  background: var(--ivory);
  flex-shrink: 0;
}

.mode-select {
  flex-shrink: 0;
  width: auto;
  min-width: 110px;
  align-self: flex-end;
}

.chat-textarea {
  flex: 1;
  resize: none;
  min-height: 44px;
  max-height: 200px;
  padding: 10px 14px;
}

/* ===== Sidebar ===== */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--ivory);
  border-left: 1px solid var(--border-cream);
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-title {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 16px;
  line-height: 1.3;
  color: var(--near-black);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-cream);
  margin-bottom: 4px;
}

.session-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.12s;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.session-item:hover {
  background: var(--parchment);
}

.session-item.active {
  background: var(--warm-sand);
}

.session-item-title {
  font-size: 14px;
  color: var(--near-black);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-item-meta {
  font-size: 12px;
  color: var(--stone-gray);
  margin-top: 2px;
}

/* ===== Responsive ===== */
@media (max-width: 767px) {
  .header {
    padding: 10px 12px;
  }

  .header-title {
    font-size: 16px;
  }

  .messages {
    padding: 12px;
  }

  .message {
    max-width: 90%;
  }

  .input-bar {
    padding: 10px 12px;
  }

  .sidebar {
    display: none;
  }

  .login-card {
    padding: 32px 24px;
  }
}

/* ===== Scrollbar ===== */
.messages::-webkit-scrollbar,
.sidebar::-webkit-scrollbar,
.tool-body::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track,
.sidebar::-webkit-scrollbar-track,
.tool-body::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb,
.tool-body::-webkit-scrollbar-thumb {
  background: var(--ring-warm);
  border-radius: 3px;
}

/* ===== Tab Navigation ===== */
.tab-nav {
  display: flex;
  flex-direction: row;
  gap: 4px;
  align-items: center;
}

.tab-btn {
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  color: var(--olive-gray);
  background: transparent;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
  white-space: nowrap;
}

.tab-btn:hover {
  background: var(--warm-sand);
}

.tab-btn.active {
  background: var(--terracotta);
  color: #fff;
}

.tab-content {
  display: none;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.tab-content.active {
  display: flex;
}

/* ===== Split Layout ===== */
.split-layout {
  display: flex;
  flex-direction: row;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.split-layout .chat-area {
  flex: 1;
  min-width: 0;
}

/* ===== Task Panel ===== */
.task-panel {
  width: 320px;
  flex-shrink: 0;
  background: var(--ivory);
  border-left: 1px solid var(--border-cream);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.task-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-cream);
}

.task-panel-title {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 16px;
  color: var(--near-black);
}

.task-panel-filter {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border-cream);
}

.task-panel-filter .select {
  width: 100%;
}

.task-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ===== Task Items ===== */
.task-item {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  transition: background-color 0.12s;
}

.task-item:hover {
  background: var(--parchment);
}

.task-item-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--near-black);
  margin-bottom: 6px;
}

.task-item-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

/* ===== Priority Badges ===== */
.priority-badge {
  font-size: 11px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: capitalize;
}

.priority-high {
  background: var(--terracotta);
  color: #fff;
}

.priority-normal {
  background: var(--warm-sand);
  color: var(--charcoal);
}

.priority-low {
  background: var(--parchment);
  color: var(--stone-gray);
}

/* ===== Status Badges ===== */
.status-badge {
  font-size: 11px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 4px;
}

.status-backlog {
  background: var(--warm-sand);
  color: var(--charcoal);
}

.status-in_progress {
  background: #e8d8b8;
  color: var(--charcoal);
}

.status-done {
  background: #d4e4d4;
  color: var(--charcoal);
}

.status-blocked {
  background: #f0d0d0;
  color: var(--charcoal);
}

/* ===== Kanban ===== */
.kanban-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px 0;
  flex-shrink: 0;
}

.kanban {
  display: flex;
  flex-direction: row;
  gap: 16px;
  padding: 20px;
  flex: 1;
  min-height: 0;
  overflow-x: auto;
}

.kanban-column {
  flex: 1;
  min-width: 240px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.kanban-title {
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  color: var(--near-black);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-cream);
}

.kanban-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  flex: 1;
}

.kanban-card {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 8px;
  padding: 12px;
  cursor: pointer;
  transition: box-shadow 0.15s, background-color 0.12s;
}

.kanban-card:hover {
  box-shadow: var(--shadow-whisper);
}

.kanban-card-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--near-black);
  margin-bottom: 6px;
}

.kanban-card-meta {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ===== Dashboard ===== */
.dashboard {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.metric-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

.metric-card {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 12px;
  padding: 20px;
}

.metric-label {
  font-size: 13px;
  color: var(--stone-gray);
  margin-bottom: 4px;
}

.metric-value {
  font-family: var(--font-mono);
  font-size: 28px;
  font-weight: 600;
  color: var(--near-black);
}

.metric-bar {
  height: 6px;
  background: var(--warm-sand);
  border-radius: 3px;
  margin-top: 10px;
  overflow: hidden;
}

.metric-bar-fill {
  height: 100%;
  background: var(--terracotta);
  border-radius: 3px;
  transition: width 0.3s;
}

.chart-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.chart-card {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 12px;
  padding: 20px;
}

.chart-title {
  font-family: var(--font-serif);
  font-size: 16px;
  font-weight: 500;
  color: var(--near-black);
  margin-bottom: 12px;
}

/* ===== Terminal ===== */
.terminal-container {
  flex: 1;
  background: var(--near-black);
  padding: 4px;
  min-height: 0;
}

.terminal-container .xterm {
  height: 100%;
}

/* ===== Agent Flow ===== */
.agentflow-frame {
  width: 100%;
  flex: 1;
  border: none;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-card {
  background: var(--ivory);
  border: 1px solid var(--border-cream);
  border-radius: 12px;
  box-shadow: var(--shadow-whisper);
  width: 100%;
  max-width: 480px;
  padding: 32px;
}

.modal-title {
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 22px;
  color: var(--near-black);
  margin-bottom: 20px;
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  font-weight: 500;
  color: var(--olive-gray);
}

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

/* ===== Responsive additions ===== */
@media (max-width: 767px) {
  .task-panel {
    display: none;
  }

  .kanban {
    flex-direction: column;
  }

  .kanban-column {
    min-width: 0;
  }

  .chart-row {
    grid-template-columns: 1fr;
  }

  .tab-nav {
    gap: 2px;
  }

  .tab-btn {
    font-size: 12px;
    padding: 4px 8px;
  }
}

/* ===== Scrollbar additions ===== */
.task-list::-webkit-scrollbar,
.kanban-cards::-webkit-scrollbar,
.dashboard::-webkit-scrollbar {
  width: 6px;
}

.task-list::-webkit-scrollbar-track,
.kanban-cards::-webkit-scrollbar-track,
.dashboard::-webkit-scrollbar-track {
  background: transparent;
}

.task-list::-webkit-scrollbar-thumb,
.kanban-cards::-webkit-scrollbar-thumb,
.dashboard::-webkit-scrollbar-thumb {
  background: var(--ring-warm);
  border-radius: 3px;
}

/* ===== Utility ===== */
[hidden] {
  display: none !important;
}
