/* ─── Design tokens ─────────────────────────────────────────────────── */
:root {
  --color-primary:       #1a56db;
  --color-primary-dark:  #1344b3;
  --color-primary-light: #e8f0fe;
  --color-surface:       #f8fafc;
  --color-surface-alt:   #ffffff;
  --color-border:        #e2e8f0;
  --color-text:          #1a202c;
  --color-muted:         #718096;
  --color-ok:            #276749;
  --color-ok-bg:         #c6f6d5;
  --color-warn:          #744210;
  --color-warn-bg:       #fefcbf;
  --color-critical:      #9b2c2c;
  --color-critical-bg:   #fed7d7;
  --color-error:         #e53e3e;
  --radius-card:         12px;
  --radius-sm:           6px;
  --shadow-card:         0 2px 12px rgba(0,0,0,0.07);
  --shadow-drawer:       -4px 0 24px rgba(0,0,0,0.15);
  --font:                'Inter', system-ui, -apple-system, sans-serif;
  --transition:          0.2s ease;
}

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

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--color-surface);
  color: var(--color-text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ─── Header ─────────────────────────────────────────────────────────── */
.app-header {
  background: var(--color-surface-alt);
  border-bottom: 1px solid var(--color-border);
  padding: 0 1.5rem;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
  gap: 1rem;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header-icon { font-size: 1.75rem; line-height: 1; }

.header-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.2;
}

.header-sub {
  font-size: 0.75rem;
  color: var(--color-muted);
  white-space: nowrap;
}

/* ─── Main layout ────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
  padding: 2rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ─── Buttons ────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
  white-space: nowrap;
}

.btn-primary {
  background: var(--color-primary);
  color: #fff;
  border-color: var(--color-primary);
}
.btn-primary:hover { background: var(--color-primary-dark); border-color: var(--color-primary-dark); }

.btn-ghost {
  background: transparent;
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn-ghost:hover { background: var(--color-surface); }

.btn-icon {
  background: transparent;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: var(--color-muted);
  padding: 0.25rem;
  border-radius: var(--radius-sm);
  transition: color var(--transition);
}
.btn-icon:hover { color: var(--color-text); }

/* ─── Status bar ─────────────────────────────────────────────────────── */
.status-bar {
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  font-weight: 500;
}
.status-info     { background: var(--color-primary-light); color: var(--color-primary-dark); }
.status-ok       { background: var(--color-ok-bg);         color: var(--color-ok); }
.status-error    { background: var(--color-critical-bg);   color: var(--color-critical); }

/* ─── Drop zone ──────────────────────────────────────────────────────── */
.drop-zone {
  border: 2px dashed var(--color-border);
  border-radius: var(--radius-card);
  background: var(--color-surface-alt);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition), transform var(--transition);
  min-height: 220px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.drop-zone:hover,
.drop-zone.dragover {
  border-color: var(--color-primary);
  background: var(--color-primary-light);
  transform: scale(1.005);
}

.drop-zone-idle {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  padding: 2.5rem;
  pointer-events: none;
}

.drop-icon { font-size: 3rem; line-height: 1; }

.drop-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--color-text);
}

.drop-sub {
  font-size: 0.875rem;
  color: var(--color-muted);
  max-width: 360px;
}

.drop-zone-loading {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 2.5rem;
}

.drop-loading-text { font-size: 0.875rem; color: var(--color-muted); }

/* Spinner */
.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Results panel ──────────────────────────────────────────────────── */
.results-panel {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ─── Card ───────────────────────────────────────────────────────────── */
.card {
  background: var(--color-surface-alt);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-card);
  overflow: hidden;
}

.card-header {
  padding: 1.25rem 1.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border);
}

.card-title { font-size: 1rem; font-weight: 600; }
.card-hint  { font-size: 0.75rem; color: var(--color-muted); margin-top: 0.2rem; }

/* ─── Values table ───────────────────────────────────────────────────── */
.table-wrapper { overflow-x: auto; }

.values-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.values-table th {
  background: #eef2ff;
  color: var(--color-primary-dark);
  font-weight: 600;
  padding: 0.65rem 0.9rem;
  text-align: center;
  white-space: nowrap;
  border-bottom: 2px solid var(--color-primary);
  border-right: 1px solid var(--color-border);
}
.values-table th:last-child { border-right: none; }

.values-table td {
  padding: 0.55rem 0.9rem;
  text-align: center;
  border-bottom: 1px solid var(--color-border);
  border-right: 1px solid var(--color-border);
  vertical-align: middle;
}
.values-table td:last-child { border-right: none; }

.values-table tbody tr:last-child td { border-bottom: none; }

.row-soll td { background: #fafafa; }

.cell-label {
  font-style: italic;
  color: var(--color-muted);
  font-size: 0.8rem;
  white-space: nowrap;
  text-align: left !important;
}

.cell-datum-empty { background: transparent !important; }

/* Editable cells */
.cell-edit {
  display: block;
  min-width: 3rem;
  min-height: 1.4em;
  outline: none;
  border-radius: 4px;
  padding: 2px 4px;
  transition: background var(--transition);
}
.cell-edit:focus {
  background: var(--color-primary-light);
  box-shadow: 0 0 0 2px var(--color-primary);
}
.cell-edit:empty::before {
  content: '—';
  color: #cbd5e0;
}

/* Color coding */
.val-ok       { color: var(--color-ok);       background: var(--color-ok-bg);       border-radius: 4px; }
.val-warn     { color: var(--color-warn);     background: var(--color-warn-bg);     border-radius: 4px; font-weight: 600; }
.val-critical { color: var(--color-critical); background: var(--color-critical-bg); border-radius: 4px; font-weight: 700; }

/* Inferred cell — positional inference, needs user verification */
.val-inferred {
  outline: 2px dashed #d97706 !important;
  background: rgba(217, 119, 6, 0.08) !important;
  border-radius: 4px;
}

/* Inferred parameters banner */
.inferred-banner {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin: 0.75rem 1.5rem 0;
  padding: 0.6rem 1rem;
  background: rgba(217, 119, 6, 0.07);
  border: 1px solid rgba(217, 119, 6, 0.3);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  color: #92400e;
  line-height: 1.45;
}

/* Legend */
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  border-top: 1px solid var(--color-border);
  font-size: 0.75rem;
  color: var(--color-muted);
}
.legend-item { display: flex; align-items: center; gap: 0.4rem; }
.legend-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 3px;
}

/* ─── Interpretation ─────────────────────────────────────────────────── */
.interpretation-text {
  padding: 1.25rem 1.5rem;
  font-size: 0.9375rem;
  line-height: 1.7;
  color: var(--color-text);
  min-height: 80px;
  outline: none;
}
.interpretation-text:focus { background: #fafbff; }
.interpretation-text:empty::before {
  content: 'Beurteilung erscheint nach der Analyse…';
  color: #cbd5e0;
  pointer-events: none;
}

/* ─── Actions row ────────────────────────────────────────────────────── */
.actions-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.btn-export {
  padding: 0.7rem 1.5rem;
  font-size: 0.9375rem;
}

/* ─── OCR method radio group ─────────────────────────────────────────── */
.ocr-method-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.4rem;
  margin-bottom: 0.25rem;
}

.radio-option {
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  padding: 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition);
}
.radio-option:hover { border-color: var(--color-primary); background: var(--color-primary-light); }
.radio-option input[type="radio"] { margin-top: 3px; accent-color: var(--color-primary); flex-shrink: 0; }
.radio-option:has(input:checked) { border-color: var(--color-primary); background: var(--color-primary-light); }

.radio-content { display: flex; flex-direction: column; gap: 2px; }
.radio-title { font-size: 0.875rem; font-weight: 600; color: var(--color-text); }
.radio-desc  { font-size: 0.75rem; color: var(--color-muted); }

/* ─── Settings drawer ────────────────────────────────────────────────── */
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 199;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}

.settings-drawer {
  position: fixed;
  top: 0;
  right: 0;
  height: 100%;
  width: min(400px, 95vw);
  background: var(--color-surface-alt);
  box-shadow: var(--shadow-drawer);
  z-index: 200;
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
}

.settings-drawer.open { transform: translateX(0); }
.settings-drawer.open ~ .settings-overlay,
.settings-overlay:has(+ .settings-drawer.open) { opacity: 1; pointer-events: auto; }

/* Sibling selector fallback */
body:has(.settings-drawer.open) .settings-overlay {
  opacity: 1;
  pointer-events: auto;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
  font-size: 1rem;
  font-weight: 600;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-label { font-size: 0.8125rem; font-weight: 600; color: var(--color-text); }
.form-input {
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.875rem;
  color: var(--color-text);
  background: var(--color-surface-alt);
  transition: border-color var(--transition), box-shadow var(--transition);
  margin-top: 0.3rem;
}
.form-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(26,86,219,0.15);
}
.form-hint {
  font-size: 0.75rem;
  color: var(--color-muted);
  line-height: 1.5;
  margin-top: 0.4rem;
  margin-bottom: 0.75rem;
}
.form-hint a { color: var(--color-primary); }

/* ─── Debug panel ────────────────────────────────────────────────────── */
.debug-card {
  border-color: #e9d8fd;
  background: #faf5ff;
  overflow: visible;
}

.debug-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.9rem 1.25rem;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 600;
  color: #6b46c1;
  text-align: left;
  transition: background var(--transition);
}
.debug-toggle:hover { background: #f3e8ff; }
.debug-toggle-icon {
  font-size: 0.65rem;
  transition: transform 0.2s ease;
  display: inline-block;
}
.debug-toggle[aria-expanded="true"] .debug-toggle-icon { transform: rotate(90deg); }

.debug-badge {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 500;
  color: #805ad5;
  background: #e9d8fd;
  border-radius: 20px;
  padding: 2px 8px;
}

.debug-body {
  display: none;
  flex-direction: column;
  border-top: 1px solid #e9d8fd;
}
.debug-body.open { display: flex; }

.debug-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 1.25rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.debug-hint { font-size: 0.75rem; color: #805ad5; }

.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.8rem; }

.debug-pre {
  margin: 0;
  padding: 1rem 1.25rem 1.25rem;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 0.78rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  color: #2d3748;
  background: #1a1a2e;
  color: #a8d8a8;
  overflow-x: auto;
  max-height: 420px;
  overflow-y: auto;
  border-radius: 0 0 var(--radius-card) var(--radius-card);
}

/* ─── Footer ─────────────────────────────────────────────────────────── */
.app-footer {
  text-align: center;
  padding: 1.25rem;
  font-size: 0.75rem;
  color: var(--color-muted);
  border-top: 1px solid var(--color-border);
}

/* ─── State machine ──────────────────────────────────────────────────── */

/* idle: show drop zone, hide results */
body.state-idle .drop-zone          { display: flex; }
body.state-idle .drop-zone-idle     { display: flex; }
body.state-idle .drop-zone-loading  { display: none; }
body.state-idle .results-panel      { display: none; }

/* loading / ocr: show drop zone in loading state, hide results */
body.state-loading .drop-zone,
body.state-ocr .drop-zone           { display: flex; cursor: default; pointer-events: none; }
body.state-loading .drop-zone-idle,
body.state-ocr .drop-zone-idle      { display: none; }
body.state-loading .drop-zone-loading,
body.state-ocr .drop-zone-loading   { display: flex; }
body.state-loading .results-panel,
body.state-ocr .results-panel       { display: none; }

/* review: hide drop zone, show results */
body.state-review .drop-zone        { display: none; }
body.state-review .results-panel    { display: flex; }

/* exporting: show results, disable export button */
body.state-exporting .drop-zone     { display: none; }
body.state-exporting .results-panel { display: flex; }
body.state-exporting .btn-export    { opacity: 0.6; pointer-events: none; }
body.state-exporting .export-idle   { display: none; }
body.state-exporting .export-loading{ display: inline; }

/* default export button text visibility */
.export-loading { display: none; }

/* ─── Responsive ─────────────────────────────────────────────────────── */
@media (max-width: 680px) {
  .header-sub { display: none; }
  .main-content { padding: 1.25rem 1rem; }
  .values-table th,
  .values-table td { padding: 0.45rem 0.5rem; font-size: 0.8rem; }
  .actions-row { flex-direction: column; align-items: stretch; }
  .btn-export { text-align: center; justify-content: center; }
}
