:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #dde3ec;
  --text: #1a2332;
  --muted: #5c6b82;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --assistant-bg: #eef2ff;
  --user-bg: #e8f5e9;
  --danger: #b91c1c;
  --kp-accent: #f97316;
  --contract-accent: #22c55e;
  --cashflow-accent: #6366f1;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(26, 35, 50, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.app {
  max-width: 1920px;
  margin: 0 auto;
  padding: 20px 24px 32px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
}

.header h1 {
  margin: 0 0 4px;
  font-size: 1.5rem;
  font-weight: 600;
}

.subtitle {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.session-badge {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns:
    220px
    minmax(260px, 1fr)
    minmax(200px, 1fr)
    minmax(200px, 1fr)
    minmax(220px, 1.1fr);
  grid-template-rows: minmax(520px, 1fr);
  gap: 12px;
  min-height: calc(100vh - 120px);
}

.docs-panel {
  grid-column: 1;
  grid-row: 1;
}

.chat-panel {
  grid-column: 2;
  grid-row: 1;
  min-height: 480px;
  max-height: calc(100vh - 120px);
  overflow: hidden;
}

.result-panel-kp {
  grid-column: 3;
  grid-row: 1;
  border-top: 4px solid var(--kp-accent);
}

.result-panel-contract {
  grid-column: 4;
  grid-row: 1;
  border-top: 4px solid var(--contract-accent);
}

.result-panel-cashflow {
  grid-column: 5;
  grid-row: 1;
  border-top: 4px solid var(--cashflow-accent);
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.panel h2 {
  margin: 0 0 14px;
  font-size: 1rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.cell-tag {
  font-size: 0.68rem;
  font-weight: 500;
  color: var(--muted);
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 8px;
}

.result-panel-content {
  flex: 1;
  overflow-y: auto;
  font-size: 0.82rem;
}

.upload-block + .upload-block {
  margin-top: 16px;
}

.upload-label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 8px;
}

.dropzone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 18px 12px;
  text-align: center;
  font-size: 0.82rem;
  color: var(--muted);
  transition: border-color 0.15s, background 0.15s;
  cursor: pointer;
}

.dropzone.dragover {
  border-color: var(--accent);
  background: #f0f5ff;
}

.dropzone p {
  margin: 0;
  line-height: 1.5;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
  padding: 0;
}

.file-status {
  margin-top: 8px;
  font-size: 0.8rem;
  color: var(--muted);
}

.file-status.ok {
  color: #166534;
}

.file-status.error {
  color: var(--danger);
}

.hint {
  margin-top: auto;
  padding-top: 16px;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.45;
}

.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: scroll;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-right: 8px;
  margin-bottom: 12px;
  scrollbar-gutter: stable;
  scrollbar-width: auto;
  scrollbar-color: #94a3b8 #eef2f7;
}

.chat-messages::-webkit-scrollbar {
  width: 14px;
}

.chat-messages::-webkit-scrollbar-track {
  background: #eef2f7;
  border-radius: 8px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: #94a3b8;
  border-radius: 8px;
  border: 3px solid #eef2f7;
  min-height: 40px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
  background: #64748b;
}

.message {
  display: flex;
}

.message.user {
  justify-content: flex-end;
}

.bubble {
  max-width: 88%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 0.9rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--assistant-bg);
  border-bottom-left-radius: 4px;
}

.message.user .bubble {
  background: var(--user-bg);
  border-bottom-right-radius: 4px;
}

.clarification {
  margin-top: 8px;
  padding: 8px 10px;
  background: #fff7ed;
  border-left: 3px solid #f59e0b;
  border-radius: 6px;
  font-size: 0.82rem;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
}

.chat-form textarea {
  resize: vertical;
  min-height: 44px;
  max-height: 140px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font: inherit;
  font-size: 0.9rem;
}

.chat-form textarea:disabled {
  background: #f8fafc;
  cursor: not-allowed;
}

.chat-form button {
  border: none;
  background: var(--accent);
  color: white;
  border-radius: var(--radius);
  padding: 10px 18px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.chat-form button:hover:not(:disabled) {
  background: var(--accent-hover);
}

.chat-form button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.empty-state {
  color: var(--muted);
  margin: 0;
}

.analysis-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fafbfc;
}

.analysis-card h3,
.analysis-card .card-subtitle {
  margin: 0 0 8px;
  font-size: 0.84rem;
  font-weight: 600;
}

.analysis-card .summary {
  color: var(--muted);
  margin: 0 0 8px;
  line-height: 1.45;
}

.kv {
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
}

.kv div {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.kv span:first-child {
  color: var(--muted);
}

.kv-row.no-amounts span:last-child {
  color: var(--muted);
  font-weight: 600;
}

.stage-item {
  border-top: 1px solid var(--border);
  padding-top: 8px;
  margin-top: 8px;
}

.stage-item strong {
  display: block;
  margin-bottom: 4px;
}

.stage-item p {
  margin: 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.cashflow-table-wrap {
  overflow-x: auto;
  margin-top: 8px;
}

.cashflow-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.78rem;
}

.cashflow-table th,
.cashflow-table td {
  border: 1px solid var(--border);
  padding: 6px 8px;
  text-align: right;
}

.cashflow-table th:first-child,
.cashflow-table td:first-child {
  text-align: left;
}

.cashflow-table th {
  background: #f8fafc;
  font-weight: 600;
}

.cashflow-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.cashflow-actions button {
  border: 1px solid var(--border);
  background: #f8fafc;
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.cashflow-actions button:hover {
  background: #eef2ff;
}

.cashflow-actions button.primary-action {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.cashflow-actions button.primary-action:hover {
  background: var(--accent-hover);
}

.cashflow-status {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 8px;
}

.cashflow-timeline {
  margin: 10px 0 12px;
  font-size: 0.78rem;
}

.cashflow-timeline h4 {
  margin: 0 0 8px;
  font-size: 0.82rem;
}

.cashflow-timeline ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.cashflow-timeline li {
  display: grid;
  gap: 4px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fafbfc;
}

.stage-badge {
  display: inline-block;
  width: fit-content;
  font-size: 0.68rem;
  font-weight: 600;
  border-radius: 999px;
  padding: 2px 8px;
}

.badge-doc {
  background: #eff6ff;
  color: #1d4ed8;
}

.badge-modified {
  background: #fff7ed;
  color: #c2410c;
}

.badge-user {
  background: #f0fdf4;
  color: #15803d;
}

.stage-meta {
  color: var(--muted);
}

.cell-amount {
  font-weight: 600;
}

.cell-note {
  margin-top: 4px;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.35;
  text-align: left;
}

.cell-zero {
  color: var(--muted);
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  color: white;
}

.overlay.hidden {
  display: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 1400px) {
  .layout {
    grid-template-columns: 200px 1fr 1fr;
    grid-template-rows: minmax(420px, 1fr) auto auto;
  }

  .docs-panel {
    grid-column: 1;
    grid-row: 1 / 3;
  }

  .chat-panel {
    grid-column: 2 / 4;
    grid-row: 1;
    min-height: 420px;
  }

  .result-panel-kp {
    grid-column: 2;
    grid-row: 2;
  }

  .result-panel-contract {
    grid-column: 3;
    grid-row: 2;
  }

  .result-panel-cashflow {
    grid-column: 2 / 4;
    grid-row: 3;
  }
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto;
  }

  .docs-panel,
  .chat-panel,
  .result-panel-kp,
  .result-panel-contract,
  .result-panel-cashflow {
    grid-column: 1;
    grid-row: auto;
  }

  .chat-panel {
    min-height: 360px;
  }
}
