:root {
  --bg: #f6f7fb;
  --card: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d9dde8;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --warning: #b45309;
  --success: #047857;
  --shadow: 0 10px 30px rgba(20, 32, 58, 0.08);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 28px;
  background: linear-gradient(135deg, #13213a, #243b63);
  color: #fff;
}
.topbar h1 { margin: 0; font-size: 28px; }
.topbar p { margin: 4px 0 0; color: #dbeafe; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }

.layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 18px;
  padding: 18px;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 18px;
}
.card + .card { margin-top: 18px; }
.card h2 { margin: 0 0 14px; font-size: 20px; }
.card-title-row, .section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.section-title-row h3 { margin: 0; }

button, .button {
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
button:hover, .button:hover { background: var(--primary-dark); }
button.secondary, .button.secondary {
  background: #eef2ff;
  color: #1e3a8a;
}
button.small { padding: 7px 10px; font-size: 13px; }
button.primary { background: var(--success); }
button.primary:hover { background: #065f46; }
button.danger { background: var(--danger); }
button.danger:hover { background: #b91c1c; }
button:disabled { opacity: 0.55; cursor: not-allowed; }

.filter-row { display: flex; gap: 8px; margin-bottom: 12px; }
select, input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  background: #fff;
}

.document-list { display: grid; gap: 8px; max-height: calc(100vh - 250px); overflow: auto; }
.doc-item {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
  background: #fff;
}
.doc-item:hover, .doc-item.active { border-color: var(--primary); background: #f8fbff; }
.doc-title { font-weight: 800; margin-bottom: 4px; word-break: break-word; }
.doc-meta { color: var(--muted); font-size: 13px; }
.doc-path { color: var(--muted); font-size: 11px; font-family: monospace; word-break: break-all; margin-top: 3px; }
.badge {
  display: inline-flex;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 12px;
  background: #eef2ff;
  color: #1e3a8a;
  margin-top: 8px;
}
.badge.invalid { background: #fee2e2; color: #991b1b; }
.badge.valid { background: #dcfce7; color: #166534; }
.badge.warning { background: #fef3c7; color: #92400e; }

.document-view {
  display: grid;
  grid-template-columns: minmax(420px, 48%) 1fr;
  gap: 18px;
}
.hidden { display: none !important; }
.empty { min-height: 240px; display: flex; flex-direction: column; justify-content: center; align-items: center; color: var(--muted); text-align: center; }

.preview iframe {
  width: 100%;
  height: calc(100vh - 210px);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #f3f4f6;
}

.preview img {
  width: 100%;
  max-height: calc(100vh - 210px);
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #f3f4f6;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
label { font-size: 13px; color: var(--muted); font-weight: 700; }
label input { margin-top: 6px; color: var(--text); font-weight: 500; }

.target-path-box {
  border-radius: 10px;
  padding: 8px 12px;
  margin-bottom: 10px;
  border: 1px solid var(--line);
  background: #f8fafc;
  font-size: 12px;
  font-family: monospace;
  word-break: break-all;
  color: var(--muted);
}

.validation-box {
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  background: #f8fafc;
}
.validation-box.valid { border-color: #bbf7d0; background: #f0fdf4; color: var(--success); }
.validation-box.warning { border-color: #fde68a; background: #fffbeb; color: var(--warning); }
.validation-box.invalid { border-color: #fecaca; background: #fef2f2; color: var(--danger); }
.muted { color: var(--muted); }

.autosave-status { font-size: 0.75rem; font-weight: 400; color: var(--muted); margin-left: 0.5rem; }
.autosave-status.error { color: #dc2626; }

.vat-table {
  width: 100%;
  border-collapse: collapse;
  margin: 8px 0 16px;
}
.vat-table th, .vat-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
}
.vat-table input { padding: 8px; border-radius: 10px; }
.vat-table .delete { background: #fee2e2; color: #991b1b; }

summary { cursor: pointer; font-weight: 800; margin: 12px 0; }

.doc-tree { max-height: calc(100vh - 250px); overflow: auto; }
.doc-tree details { margin: 2px 0; }
.doc-tree details > summary {
  font-weight: 700;
  font-size: 13px;
  color: var(--text);
  padding: 4px 6px;
  border-radius: 8px;
  margin: 2px 0;
}
.doc-tree details > summary:hover { background: #eef2ff; }
.doc-tree .tree-children {
  padding-left: 14px;
  border-left: 2px solid var(--line);
  margin-left: 10px;
}
.doc-tree .tree-file {
  padding: 5px 8px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text);
  word-break: break-all;
}
.doc-tree .tree-file:hover { background: #eef2ff; }
.doc-tree .tree-file.active { background: #dbeafe; color: var(--primary); font-weight: 700; }
textarea#jsonEditor { min-height: 260px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
pre#ocrText { white-space: pre-wrap; background: #0f172a; color: #e2e8f0; border-radius: 14px; padding: 14px; max-height: 360px; overflow: auto; }
.status-box { font-size: 14px; color: var(--muted); word-break: break-word; }

.upload-zone {
  border: 2px dashed var(--line);
  border-radius: 14px;
  padding: 14px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 14px;
  transition: border-color 0.15s, background 0.15s;
}
.upload-zone.drag-over {
  border-color: var(--primary);
  background: #eff6ff;
  color: var(--primary);
}
.upload-status { font-size: 13px; width: 100%; text-align: center; min-height: 1em; }

.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(19, 33, 58, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--card);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 40px 36px;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-card h2 { margin: 0; font-size: 24px; }
.login-card p { margin: 0; color: var(--muted); }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; }
.login-card button[type="submit"] { margin-top: 4px; width: 100%; }
.login-error {
  color: var(--danger);
  font-size: 13px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 8px 12px;
}

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .document-view { grid-template-columns: 1fr; }
  .preview iframe { height: 520px; }
  .preview img { max-height: 520px; }
}
