:root {
  --bg: #f4f6f9;
  --card-bg: #ffffff;
  --primary: #1a73e8;
  --primary-dark: #1557b0;
  --text: #1a1a2e;
  --text-muted: #6b7280;
  --border: #e2e8f0;
  --warning-bg: #fff8e1;
  --warning-border: #ffc107;
  --error: #dc3545;
  --success: #28a745;
  --code-bg: #f0f0f0;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.app-header {
  background: linear-gradient(135deg, #0d47a1, #1565c0);
  color: #fff;
  padding: 1rem 2rem;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
  max-width: 1200px;
  margin: 0 auto;
}

.header-title h1 { font-size: 1.5rem; }
.subtitle { font-size: 0.85rem; opacity: 0.9; margin-top: 0.25rem; }

.header-user .dev-notice { font-size: 0.8rem; opacity: 0.8; }
.header-user code { background: rgba(255,255,255,0.15); padding: 2px 6px; border-radius: 4px; }

.app-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.75rem 2rem;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  max-width: 1200px;
  margin: 0 auto;
}

.nav-btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text);
  transition: all 0.15s;
}

.nav-btn:hover { background: #e8f0fe; border-color: var(--primary); }
.nav-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.app-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 2rem;
}

.view { display: none; }
.view.active { display: block; }

.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  padding: 1.5rem;
  margin-bottom: 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.card h2 { font-size: 1.25rem; margin-bottom: 0.75rem; color: var(--primary-dark); }
.card h3 { font-size: 1.05rem; margin-bottom: 0.5rem; }

.source-note { font-style: italic; color: var(--text-muted); font-size: 0.9rem; margin: 0.5rem 0; }
.disclaimer { color: #856404; background: var(--warning-bg); padding: 0.75rem; border-radius: var(--radius); border: 1px solid var(--warning-border); font-size: 0.9rem; }

.objectives-list { padding-left: 1.5rem; }
.objectives-list li { margin-bottom: 0.5rem; }

.rules-list { list-style: disc; padding-left: 1.5rem; }
.rules-list li { margin-bottom: 0.4rem; }

.legend { display: flex; flex-wrap: wrap; gap: 1rem; margin-top: 0.75rem; }
.legend-item { display: flex; align-items: center; gap: 0.4rem; font-size: 0.9rem; }
.code-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  background: var(--primary);
  color: #fff;
  border-radius: 50%;
  font-weight: bold;
  font-size: 0.8rem;
}

.sections-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; }
.section-info { padding: 1rem; background: var(--bg); border-radius: var(--radius); }
.section-info h3 { color: var(--primary); font-size: 0.95rem; }
.section-info p { font-size: 0.85rem; color: var(--text-muted); }

.submodality-list { list-style: disc; padding-left: 1.2rem; margin-top: 0.4rem; }
.submodality-list li { font-size: 0.82rem; color: var(--text-muted); margin-bottom: 0.2rem; }

.anomaly-text { color: #856404; font-size: 0.82rem; margin-top: 0.3rem; }

.findings-list { list-style: none; padding-left: 0; margin-top: 0.5rem; }
.findings-list li { display: flex; align-items: center; gap: 0.5rem; padding: 0.3rem 0; font-size: 0.85rem; }
.findings-list li::before { content: "•"; color: var(--primary); font-weight: bold; }
.findings-list .finding-text { flex: 1; }
.findings-list .btn-danger { padding: 2px 8px; font-size: 0.75rem; }

.anomaly-card { border: 2px solid var(--warning-border); background: var(--warning-bg); }
.anomaly-list { list-style: disc; padding-left: 1.2rem; margin-top: 0.4rem; }
.anomaly-list li { font-size: 0.82rem; color: #856404; margin-bottom: 0.3rem; }

.warning-card { border: 2px solid var(--warning-border); background: var(--warning-bg); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-top: 0.5rem;
}

.form-grid label {
  display: flex;
  flex-direction: column;
  font-size: 0.85rem;
  font-weight: 500;
  gap: 0.3rem;
}

.form-grid input, .form-grid select, .form-grid textarea {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
}

.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(26,115,232,0.15);
}

.form-full { grid-column: 1 / -1; }

.checkbox-label { flex-direction: row; align-items: center; gap: 0.5rem; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 0.6rem 1.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s;
}

.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary {
  background: transparent;
  color: var(--primary);
  border: 1px solid var(--primary);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.btn-secondary:hover { background: #e8f0fe; }

.btn-danger {
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th, td {
  padding: 0.6rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th { background: var(--bg); font-weight: 600; color: var(--text); }
tr:hover { background: #f8fafd; }

.total-label { font-weight: bold; text-align: right; }
tfoot td { font-weight: bold; border-top: 2px solid var(--border); }

.status-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.75rem;
  font-weight: 500;
}

.status-draft { background: #e0e0e0; color: #555; }
.status-submitted { background: #fff3cd; color: #856404; }
.status-signed { background: #d4edda; color: #155724; }
.status-rejected { background: #f8d7da; color: #721c24; }

.anomaly-banner {
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  margin: 0.5rem 0;
}

.anomaly-banner p { font-size: 0.85rem; }

.logbooks-list { display: grid; gap: 0.75rem; }
.logbook-card {
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s;
}
.logbook-card:hover { border-color: var(--primary); }
.logbook-card.active { border-color: var(--primary); background: #e8f0fe; }
.logbook-card h4 { color: var(--primary-dark); }
.logbook-card .meta { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.25rem; }

.muted { color: var(--text-muted); font-size: 0.9rem; }

.toast {
  position: fixed;
  bottom: 2rem;
  right: 2rem;
  padding: 0.75rem 1.5rem;
  border-radius: var(--radius);
  color: #fff;
  font-size: 0.9rem;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 1000;
  pointer-events: none;
}

.toast.show { opacity: 1; }
.toast.success { background: var(--success); }
.toast.error { background: var(--error); }

.app-footer {
  text-align: center;
  padding: 1.5rem;
  color: var(--text-muted);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}

.nav-link { color: var(--primary); cursor: pointer; }

/* --- Dashboard --- */

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1rem;
}

.stat-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1.2rem;
  text-align: center;
}

.stat-card.stat-wide {
  grid-column: 1 / -1;
  text-align: left;
}

.stat-big {
  font-size: 2rem;
  font-weight: 700;
  color: var(--primary);
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
}

.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.8rem;
  margin-top: 0.5rem;
}

.stat-mini {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--bg);
  border-radius: 6px;
  padding: 0.5rem 1rem;
  min-width: 80px;
}

.stat-mini .stat-num {
  font-size: 1.4rem;
  font-weight: 600;
  color: var(--text);
}

/* --- Print preview --- */

.print-page { page-break-after: always; }
.print-info { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }
.print-info td { padding: 0.3rem 0.5rem; border: none; }
.print-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.print-table th, .print-table td { border: 1px solid #ccc; padding: 0.3rem 0.4rem; text-align: left; }
.print-table th { background: #f0f0f0; font-weight: 600; }

@media print {
  /* Hide everything by default */
  body * { visibility: hidden; }
  /* Show only the print view and its children */
  #view-print, #view-print * { visibility: visible; }
  /* Hide non-print elements within the print view */
  #view-print .app-header, #view-print .app-nav,
  #view-print form, #view-print select,
  #view-print .btn-primary, #view-print .btn-secondary,
  #view-print > .card:first-child,
  #view-print .muted { display: none !important; }
  /* Position print content at top of page */
  #view-print { position: absolute; left: 0; top: 0; width: 100%; }
  .app-main { max-width: 100% !important; padding: 0 !important; margin: 0 !important; }
  .card { border: 1px solid #ccc; page-break-inside: avoid; box-shadow: none !important; }
  .print-page { page-break-after: always; }
  table { font-size: 9pt; }
  th, td { padding: 0.3rem; }
  body { background: white !important; }
}

/* --- Mobile optimization --- */

@media (max-width: 768px) {
  .app-header, .app-nav, .app-main { padding-left: 1rem; padding-right: 1rem; }
  .header-content { flex-direction: column; align-items: flex-start; }
  .app-nav { overflow-x: auto; white-space: nowrap; position: sticky; top: 0; z-index: 100; background: var(--bg); }
  .nav-btn { white-space: nowrap; padding: 0.5rem 0.8rem; font-size: 0.85rem; }
  .form-grid { grid-template-columns: 1fr !important; }
  .form-full { grid-column: 1 !important; }
  .dashboard-grid { grid-template-columns: 1fr; }
  .stat-card { padding: 0.8rem; }
  .stat-big { font-size: 1.5rem; }
  table { font-size: 0.75rem; }
  th, td { padding: 0.4rem 0.3rem; }
  .card { padding: 0.8rem; }
  input, select, textarea { font-size: 16px !important; /* prevents iOS zoom */ }
  .btn-primary, .btn-secondary, .btn-danger {
    padding: 0.6rem 1rem;
    min-height: 44px; /* touch target */
  }
}
