:root {
  --navy: #254260;
  --navy-2: #2a3f56;
  --teal: #528093;
  --teal-dark: #477989;
  --soft: #edf5f7;
  --mint: #a9d4d2;
  --paper: #ffffff;
  --line: #d9e7eb;
  --text: #1f2933;
  --muted: #64748b;
  --danger: #b42318;
  --warning: #b7791f;
  --success: #27735a;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, #f7fbfc, var(--soft));
}

a { color: var(--teal-dark); text-decoration: none; }

.app-shell { min-height: 100vh; display: flex; }
.sidebar {
  width: 280px;
  padding: 22px;
  background: var(--navy);
  color: white;
  display: flex;
  flex-direction: column;
  gap: 24px;
  position: fixed;
  inset: 0 auto 0 0;
}
.brand-card {
  background: white;
  border-radius: 24px;
  padding: 14px;
  box-shadow: 0 18px 40px rgba(0,0,0,.15);
}
.brand-logo, .login-logo { width: 100%; display: block; }
.nav-link {
  display: block;
  padding: 14px 16px;
  border-radius: 16px;
  color: white;
  background: rgba(255,255,255,.10);
  font-weight: 700;
}
.user-box {
  margin-top: auto;
  display: grid;
  gap: 6px;
  padding: 16px;
  border-radius: 18px;
  background: rgba(255,255,255,.10);
}
.user-box span { color: var(--mint); font-size: 14px; }
.user-box a { color: white; font-size: 14px; text-decoration: underline; }
.main-content { margin-left: 280px; padding: 32px; width: calc(100% - 280px); }
.main-content.full { margin-left: 0; width: 100%; padding: 0; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(460px, 100%);
  background: white;
  padding: 32px;
  border-radius: 28px;
  box-shadow: 0 24px 80px rgba(37, 66, 96, .16);
}
.login-logo { margin-bottom: 24px; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 22px;
}
.page-header.compact { align-items: flex-start; }
h1 { color: var(--navy); margin: 4px 0 6px; font-size: clamp(28px, 4vw, 42px); line-height: 1.05; }
h2 { color: var(--navy-2); margin: 0 0 14px; }
.eyebrow { text-transform: uppercase; letter-spacing: .14em; color: var(--teal); font-weight: 800; font-size: 12px; margin: 0; }
.muted { color: var(--muted); margin: 0; }
.tiny { font-size: 12px; }
.header-actions { display: flex; gap: 10px; flex-wrap: wrap; }

.card {
  background: rgba(255,255,255,.92);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 22px;
  box-shadow: 0 16px 40px rgba(37, 66, 96, .08);
  margin-bottom: 22px;
}
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 22px; }
.section-title { margin-bottom: 16px; }

.btn {
  border: 0;
  border-radius: 999px;
  padding: 12px 18px;
  font-weight: 800;
  cursor: pointer;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  transition: transform .15s ease, box-shadow .15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn.primary { background: var(--teal); color: white; box-shadow: 0 12px 28px rgba(82,128,147,.28); }
.btn.secondary { background: var(--soft); color: var(--navy); }
.btn.ghost { background: transparent; color: var(--teal-dark); border: 1px solid var(--line); padding: 8px 12px; }

.form-stack, .form-grid { display: grid; gap: 16px; }
.form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
label { display: grid; gap: 7px; color: var(--navy-2); font-weight: 800; }
input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 14px;
  font: inherit;
  color: var(--text);
  background: white;
}
textarea { resize: vertical; }
input:focus, textarea:focus { outline: 3px solid rgba(82,128,147,.18); border-color: var(--teal); }
.form-actions { display: flex; justify-content: flex-end; }
.search-row { display: grid; grid-template-columns: 1fr auto; gap: 12px; }
.alert { background: #fee4e2; color: var(--danger); padding: 12px 14px; border-radius: 14px; margin: 14px 0; }

.table-card { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; color: var(--navy); font-size: 13px; text-transform: uppercase; letter-spacing: .06em; }
th, td { padding: 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
.empty { text-align: center; color: var(--muted); padding: 36px; }
.badge { display: inline-flex; padding: 6px 10px; border-radius: 999px; font-weight: 800; font-size: 12px; }
.badge.success { background: #def7ec; color: var(--success); }
.badge.warning { background: #fff7db; color: var(--warning); }
.signature-thumb { max-width: 150px; max-height: 54px; display: block; margin-top: 8px; background: white; border: 1px solid var(--line); border-radius: 10px; }

.stats-card { display: grid; align-content: start; }
.stat-line { display: flex; justify-content: space-between; padding: 12px 0; border-bottom: 1px solid var(--line); }
.stat-line strong { color: var(--teal-dark); }

.sign-layout { align-items: start; }
.clinical-summary dl { display: grid; grid-template-columns: 150px 1fr; gap: 12px 16px; }
.clinical-summary dt { color: var(--teal-dark); font-weight: 900; }
.clinical-summary dd { margin: 0; }
.consent { background: var(--soft); border-left: 5px solid var(--teal); padding: 14px; border-radius: 14px; color: var(--navy-2); font-weight: 650; }
.canvas-wrap {
  margin: 14px 0;
  border: 2px dashed var(--teal);
  border-radius: 20px;
  background: white;
  min-height: 260px;
  overflow: hidden;
}
#signatureCanvas { width: 100%; height: 260px; display: block; touch-action: none; }
.signature-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.signature-preview { width: 100%; max-height: 260px; object-fit: contain; background: white; border: 1px solid var(--line); border-radius: 18px; margin: 12px 0; }

@media (max-width: 900px) {
  .sidebar { position: static; width: 100%; }
  .app-shell { display: block; }
  .main-content { margin-left: 0; width: 100%; padding: 18px; }
  .page-header, .grid-2, .form-grid { grid-template-columns: 1fr; display: grid; }
  .span-2 { grid-column: span 1; }
  .clinical-summary dl { grid-template-columns: 1fr; }
}

select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 14px;
  font: inherit;
  color: var(--text);
  background: white;
}
select:focus { outline: 3px solid rgba(82,128,147,.18); border-color: var(--teal); }

h3 { color: var(--teal-dark); margin: 0 0 12px; }
.form-section { scroll-margin-top: 24px; }
.patient-hc-form { display: grid; gap: 0; }
.top-gap { margin-top: 16px; }

.radio-group {
  display: grid;
  gap: 9px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(237,245,247,.55);
}
.radio-group span { font-weight: 900; color: var(--navy-2); }
.radio-group label,
.check-item {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  color: var(--text);
}
.radio-group input,
.check-item input {
  width: auto;
  accent-color: var(--teal);
}
.checkbox-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.check-item {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: white;
}
.sticky-actions {
  position: sticky;
  bottom: 16px;
  z-index: 5;
  background: rgba(255,255,255,.92);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 12px;
  box-shadow: 0 16px 40px rgba(37, 66, 96, .14);
  margin-bottom: 22px;
}

.detail-list {
  display: grid;
  grid-template-columns: 170px 1fr;
  gap: 10px 14px;
  margin: 0;
}
.detail-list dt { color: var(--teal-dark); font-weight: 900; }
.detail-list dd { margin: 0; color: var(--text); }
.compact-list { grid-template-columns: 160px 1fr; font-size: 14px; }
.history-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 22px;
}
.history-grid article {
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  background: rgba(255,255,255,.68);
}
.tag-cloud { display: flex; flex-wrap: wrap; gap: 8px; }
.tag-cloud span {
  padding: 7px 10px;
  border-radius: 999px;
  background: var(--soft);
  color: var(--navy-2);
  font-weight: 800;
  font-size: 13px;
}
.tag-cloud .muted-tag { color: var(--muted); background: #f8fafc; }
code {
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
  background: var(--soft);
  padding: 4px 6px;
  border-radius: 8px;
  color: var(--navy-2);
}

@media (max-width: 900px) {
  .checkbox-grid, .history-grid { grid-template-columns: 1fr; }
  .detail-list, .compact-list { grid-template-columns: 1fr; }
  .sticky-actions { border-radius: 22px; }
}

.split-title {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
}
.clinical-note-block {
  white-space: pre-wrap;
  background: #f8fafc;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  color: var(--navy-2);
  font: inherit;
  margin-top: 14px;
}

.odontogram-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 22px;
  align-items: start;
}
.tooth-editor-card {
  position: sticky;
  top: 22px;
}
.legend-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 18px;
}
.legend-chip {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 7px 10px;
  font-weight: 850;
  font-size: 12px;
  border: 1px solid var(--line);
  background: white;
}
.odontogram-board { display: grid; gap: 24px; }
.arch-group {
  border: 1px solid var(--line);
  background: rgba(237,245,247,.45);
  border-radius: 22px;
  padding: 16px;
}
.arch-group h3 { margin-bottom: 14px; }
.arch-pair {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}
.tooth-row {
  display: grid;
  grid-template-columns: repeat(8, minmax(58px, 1fr));
  gap: 8px;
}
.arch-group:nth-of-type(3) .tooth-row,
.arch-group:nth-of-type(4) .tooth-row {
  grid-template-columns: repeat(5, minmax(58px, 1fr));
}
.tooth-card {
  border: 2px solid var(--line);
  border-radius: 18px;
  background: white;
  padding: 9px 6px;
  min-height: 112px;
  display: grid;
  justify-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--navy-2);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
  position: relative;
}
.tooth-card:hover,
.tooth-card.selected {
  transform: translateY(-2px);
  box-shadow: 0 14px 28px rgba(37, 66, 96, .12);
  border-color: var(--teal);
}
.tooth-number {
  font-weight: 950;
  color: var(--navy);
  letter-spacing: .03em;
}
.tooth-icon {
  width: 34px;
  height: 42px;
  background: #fff;
  border: 2px solid currentColor;
  border-radius: 50% 50% 45% 45% / 38% 38% 62% 62%;
  position: relative;
  display: block;
}
.tooth-root {
  position: absolute;
  inset: auto 9px -10px 9px;
  height: 20px;
  border-left: 2px solid currentColor;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  border-radius: 0 0 12px 12px;
  background: white;
}
.tooth-status {
  font-size: 11px;
  font-weight: 900;
  color: var(--muted);
  text-align: center;
}
.tooth-card[data-has-details="true"]::after {
  content: "•";
  position: absolute;
  right: 8px;
  top: 4px;
  font-size: 24px;
  color: var(--teal);
}
.status-sano { color: var(--navy-2); }
.status-caries { color: #b42318; border-color: rgba(180, 35, 24, .45); background: #fff5f5; }
.status-obturado { color: #2563eb; border-color: rgba(37, 99, 235, .45); background: #eff6ff; }
.status-ausente { color: #6b7280; background: #f3f4f6; }
.status-ausente .tooth-icon::after {
  content: "X";
  position: absolute;
  inset: -8px 0 0 0;
  display: grid;
  place-items: center;
  color: #111827;
  font-size: 36px;
  font-weight: 950;
}
.status-corona { color: #7c3aed; border-color: rgba(124, 58, 237, .45); background: #f5f3ff; }
.status-corona .tooth-icon::before { content: "Co"; position: absolute; inset: 8px 0 0 0; text-align: center; font-size: 11px; font-weight: 950; }
.status-incrustacion { color: #0f766e; border-color: rgba(15, 118, 110, .45); background: #f0fdfa; }
.status-incrustacion .tooth-icon::before { content: "In"; position: absolute; inset: 8px 0 0 0; text-align: center; font-size: 11px; font-weight: 950; }
.status-puente { color: #92400e; border-color: rgba(146, 64, 14, .45); background: #fffbeb; }
.status-puente .tooth-icon::before { content: "⌒"; position: absolute; inset: 5px 0 0 0; text-align: center; font-size: 24px; font-weight: 950; }

@media (max-width: 1180px) {
  .odontogram-layout { grid-template-columns: 1fr; }
  .tooth-editor-card { position: static; }
}
@media (max-width: 720px) {
  .split-title { display: grid; }
  .arch-pair { grid-template-columns: 1fr; }
  .tooth-row { grid-template-columns: repeat(4, minmax(58px, 1fr)); }
  .arch-group:nth-of-type(3) .tooth-row,
  .arch-group:nth-of-type(4) .tooth-row { grid-template-columns: repeat(3, minmax(58px, 1fr)); }
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}
.detail-hero-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 22px;
  align-items: stretch;
}
.status-card {
  display: grid;
  align-content: start;
}
.status-pill {
  display: inline-flex;
  width: fit-content;
  padding: 10px 14px;
  border-radius: 999px;
  font-weight: 950;
  letter-spacing: .02em;
}
.status-pill.success { background: #def7ec; color: var(--success); }
.status-pill.warning { background: #fff7db; color: var(--warning); }
.clinical-detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}
.signature-detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.2fr) minmax(260px, .8fr);
  gap: 22px;
  align-items: start;
}
.technical-details {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 12px 14px;
  background: #f8fafc;
  color: var(--muted);
}
.technical-details summary {
  cursor: pointer;
  color: var(--teal-dark);
  font-weight: 900;
}
.technical-details p {
  overflow-wrap: anywhere;
  margin-bottom: 0;
}
@media (max-width: 1000px) {
  .detail-hero-grid,
  .clinical-detail-grid,
  .signature-detail-grid {
    grid-template-columns: 1fr;
  }
  .row-actions { justify-content: flex-start; }
}

/* Tablet-first odontogram improvements */
.odontogram-board-card {
  overflow: hidden;
}
.odontogram-board-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 6px;
  -webkit-overflow-scrolling: touch;
  scrollbar-color: var(--teal) transparent;
}
.tablet-hint {
  display: none;
  margin: -6px 0 14px;
}
.tooth-card {
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(82, 128, 147, .18);
}

@media (max-width: 1280px) {
  .odontogram-layout {
    grid-template-columns: 1fr;
  }
  .tooth-editor-card {
    position: static;
  }
}

@media (min-width: 721px) and (max-width: 1180px) {
  .tablet-hint { display: block; }

  .odontogram-board-card,
  .tooth-editor-card {
    padding: 18px;
    border-radius: 22px;
  }

  .odontogram-board {
    gap: 18px;
  }

  .arch-pair {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .arch-group {
    padding: 14px;
    border-radius: 20px;
  }

  .tooth-row {
    grid-template-columns: repeat(8, minmax(44px, 1fr));
    gap: 7px;
  }

  .arch-group:nth-of-type(3) .tooth-row,
  .arch-group:nth-of-type(4) .tooth-row {
    grid-template-columns: repeat(5, minmax(52px, 1fr));
  }

  .tooth-card {
    min-height: 96px;
    padding: 8px 5px;
    border-radius: 16px;
  }

  .tooth-icon {
    width: 30px;
    height: 37px;
  }

  .tooth-root {
    inset: auto 8px -9px 8px;
    height: 18px;
  }

  .tooth-status {
    font-size: 10px;
    line-height: 1.1;
  }

  .tooth-editor-card .form-stack {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
  }

  .tooth-editor-card .form-stack label:nth-child(n+4) {
    grid-column: span 2;
  }

  .tooth-editor-card textarea {
    min-height: 92px;
  }

  .signature-actions.top-gap {
    justify-content: flex-start;
  }
}

@media (min-width: 721px) and (max-width: 900px) {
  .page-header.compact {
    gap: 12px;
  }

  .legend-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }

  .legend-chip {
    white-space: nowrap;
  }

  .tooth-row {
    grid-template-columns: repeat(8, minmax(42px, 1fr));
    gap: 6px;
  }

  .arch-group:nth-of-type(3) .tooth-row,
  .arch-group:nth-of-type(4) .tooth-row {
    grid-template-columns: repeat(5, minmax(48px, 1fr));
  }

  .tooth-card {
    min-height: 90px;
  }
}

@media (max-width: 720px) {
  .tablet-hint { display: block; }
  .odontogram-board-scroll {
    overflow-x: visible;
  }
  .tooth-card {
    min-height: 98px;
  }
  .tooth-editor-card .form-stack {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .odontogram-board-card,
  .tooth-editor-card,
  .arch-group {
    padding: 14px;
  }

  .tooth-row {
    grid-template-columns: repeat(3, minmax(64px, 1fr));
  }

  .arch-group:nth-of-type(3) .tooth-row,
  .arch-group:nth-of-type(4) .tooth-row {
    grid-template-columns: repeat(2, minmax(70px, 1fr));
  }

  .tooth-card {
    min-height: 96px;
  }

  .sticky-actions .btn {
    width: 100%;
  }
}

select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 13px 14px;
  font: inherit;
  color: var(--text);
  background: white;
}
select:focus { outline: 3px solid rgba(82,128,147,.18); border-color: var(--teal); }
.brand-title {
  display: block;
  color: var(--navy);
  font-size: 13px;
  margin-top: 10px;
  text-align: center;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.user-box small { color: rgba(255,255,255,.75); }
.nav-link + .nav-link { margin-top: 8px; }
.clinic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 22px;
}
.clinic-card { display: grid; gap: 16px; align-content: start; }
.clinic-card-header {
  display: flex;
  gap: 16px;
  align-items: center;
}
.clinic-logo-box {
  width: 96px;
  height: 72px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 18px;
  display: grid;
  place-items: center;
  overflow: hidden;
  flex: 0 0 auto;
}
.clinic-logo-box.large {
  width: 140px;
  height: 96px;
}
.clinic-logo-box img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 8px;
}
.stat-row {
  display: grid;
  gap: 10px;
}
.stat-row.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stat-row div {
  background: var(--soft);
  border-radius: 16px;
  padding: 12px;
}
.stat-row span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .06em;
}
.stat-row strong { color: var(--navy); }
.logo-preview-block {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}
.logo-preview-block img {
  max-width: 260px;
  max-height: 110px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: white;
  padding: 10px;
}
.stacked-mobile select { min-width: 260px; }

@media (max-width: 720px) {
  .clinic-card-header { align-items: flex-start; }
  .stat-row.three { grid-template-columns: 1fr; }
  .stacked-mobile { grid-template-columns: 1fr; }
  .stacked-mobile select { min-width: 0; }
}

.alert.success { background: #d1fae5; color: #065f46; }
.alert.warning { background: #fef3c7; color: #92400e; }
.detail-list.two-columns { grid-template-columns: 220px 1fr 220px 1fr; }
@media (max-width: 980px) {
  .detail-list.two-columns { grid-template-columns: 150px 1fr; }
}
.quick-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.field-caption { font-weight: 900; color: var(--navy); margin: 0 0 8px; }
.compact-checkboxes { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 760px) { .compact-checkboxes { grid-template-columns: 1fr; } }
.capture-summary { background: var(--soft); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; color: var(--navy-2); }

.alert.success { background: #def7ec; color: var(--success); }
.row-actions.compact-actions { gap: 8px; flex-wrap: wrap; }
.row-actions form { margin: 0; display: inline-flex; }
.btn.ghost.danger { color: var(--danger); border-color: #f4b4ad; }
.header-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
