:root {
  --teal: #0d6e6e;
  --teal-dark: #0a5757;
  --teal-light: #e6f2f2;
  --bg: #f4f6f7;
  --card: #ffffff;
  --card-alt: #fafbfb;
  --text: #1c2526;
  --muted: #6b7b7c;
  --border: #dfe6e6;
  --input-bg: #ffffff;
  --hover: #f0f3f3;
  --code-bg: #eef1f1;
  --warn-bg: #fffaf0;
  --warn-border: #f0c674;
  --danger: #c0392b;
  --overlay: rgba(0, 0, 0, .5);
  --nav-bg: #ffffff;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --radius: 14px;
}

/* Modo oscuro automático (sigue el ajuste del sistema) salvo que el usuario fije uno */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --teal: #19a3a3;
    --teal-dark: #63d6d6;
    --teal-light: #123a3a;
    --bg: #0e1516;
    --card: #172221;
    --card-alt: #1b2726;
    --text: #e7eeee;
    --muted: #93a3a3;
    --border: #2b3838;
    --input-bg: #1d2a29;
    --hover: #223130;
    --code-bg: #223130;
    --warn-bg: #2a2416;
    --warn-border: #6b5a1e;
    --danger: #e05a4a;
    --overlay: rgba(0, 0, 0, .6);
    --nav-bg: #131d1c;
    --shadow: 0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
  }
}

/* Modo oscuro forzado por el usuario */
:root[data-theme="dark"] {
  --teal: #19a3a3;
  --teal-dark: #63d6d6;
  --teal-light: #123a3a;
  --bg: #0e1516;
  --card: #172221;
  --card-alt: #1b2726;
  --text: #e7eeee;
  --muted: #93a3a3;
  --border: #2b3838;
  --input-bg: #1d2a29;
  --hover: #223130;
  --code-bg: #223130;
  --warn-bg: #2a2416;
  --warn-border: #6b5a1e;
  --danger: #e05a4a;
  --overlay: rgba(0, 0, 0, .6);
  --nav-bg: #131d1c;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 1px 2px rgba(0,0,0,.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  transition: background .2s, color .2s;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

/* Iconos SVG */
.icon-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.ic {
  width: 1.2em; height: 1.2em; flex-shrink: 0;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
  vertical-align: -.15em;
}

/* Topbar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: var(--teal);
  color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.15rem; }
.brand .logo .ic { width: 1.5rem; height: 1.5rem; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(255,255,255,.15); border: none; border-radius: 50%;
  width: 36px; height: 36px; cursor: pointer; color: #fff; padding: 0;
}
.icon-btn .ic { width: 1.25rem; height: 1.25rem; }
.icon-btn:hover { background: rgba(255,255,255,.28); }
.badge { font-size: .7rem; padding: 2px 8px; border-radius: 20px; text-transform: uppercase; font-weight: 700; letter-spacing: .04em; }
.badge-demo { background: #ffd24d; color: #6b4e00; }
.badge-online { background: #2ecc71; color: #0a3d1f; }

/* Layout */
.view { max-width: 720px; margin: 0 auto; padding: 16px; }
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}

h1 { font-size: 1.4rem; margin: 0 0 6px; }
h2 { font-size: 1.25rem; margin: 0 0 8px; }
h3 { font-size: 1.02rem; margin: 0 0 10px; }
.head-with-icon { display: flex; align-items: center; gap: 8px; }
.head-with-icon .ic { color: var(--teal); width: 1.15rem; height: 1.15rem; }

/* Forms */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 12px; }
input, select, textarea {
  width: 100%;
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--input-bg);
  color: var(--text);
  margin-top: 5px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--teal-light); border-color: var(--teal); }

/* Row card: patient + lang */
.row-card { display: grid; grid-template-columns: 1.4fr 1fr auto; gap: 14px; align-items: end; }
.row-card > * { min-width: 0; }
.field label { margin-bottom: 5px; }
.field { margin: 0; min-width: 0; }
.inline { display: flex; gap: 6px; min-width: 0; }
.inline select { margin-top: 0; flex: 1 1 auto; min-width: 0; }
.field-out { text-align: center; }
.static-pill {
  margin-top: 5px; padding: 10px 12px; border-radius: 10px;
  background: var(--teal-light); color: var(--teal-dark); font-weight: 700;
  white-space: nowrap;
}

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); background: var(--input-bg); color: var(--text);
  padding: 10px 16px; border-radius: 10px; transition: .12s;
}
.btn:hover { background: var(--hover); }
.btn:active { transform: translateY(1px); }
.btn .ic { width: 1.05em; height: 1.05em; }
.btn-primary { background: var(--teal); color: #fff; border-color: var(--teal); }
.btn-primary:hover { background: var(--teal-dark); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--hover); }
.btn-sm { padding: 7px 12px; font-size: .85rem; }
.btn-icon { padding: 8px; }
.block { width: 100%; margin-top: 8px; }

/* Mic — botón protagonista */
.record-card { text-align: center; }
.mic-wrap {
  display: flex; align-items: center; justify-content: center;
  min-height: 150px; margin: 6px 0 4px;
}
.mic-btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center;
  width: 116px; height: 116px; border-radius: 50%;
  border: none; cursor: pointer;
  background: var(--teal); color: #fff;
  box-shadow: 0 6px 18px rgba(13,110,110,.35);
  transition: transform .15s, background .15s, box-shadow .15s;
}
.mic-btn:hover { transform: scale(1.04); }
.mic-btn:active { transform: scale(.97); }
.mic-btn .mic-icon { width: 3rem; height: 3rem; }
.mic-btn[aria-pressed="true"] {
  background: var(--danger);
  box-shadow: 0 6px 20px rgba(192,57,43,.45);
}
.mic-btn[aria-pressed="true"]::before,
.mic-btn[aria-pressed="true"]::after {
  content: ''; position: absolute; inset: 0; border-radius: 50%;
  border: 2px solid var(--danger);
  animation: waves 1.8s ease-out infinite;
}
.mic-btn[aria-pressed="true"]::after { animation-delay: .9s; }
@keyframes waves {
  0%   { transform: scale(1);   opacity: .55; }
  100% { transform: scale(1.7); opacity: 0; }
}
.mic-timer {
  display: inline-flex; align-items: center; gap: 8px;
  font-weight: 700; font-variant-numeric: tabular-nums;
  color: var(--danger); font-size: 1.1rem; margin: 2px 0 6px;
}
.rec-dot {
  width: 10px; height: 10px; border-radius: 50%; background: var(--danger);
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: .25; } }
.mic-label { color: var(--muted); font-weight: 600; margin-bottom: 12px; }
.transcript-wrap { text-align: left; }
.record-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }

/* Questions */
.questions-card { border-color: var(--warn-border); background: var(--warn-bg); }
.q-item { margin-bottom: 12px; }
.q-item label { font-weight: 600; margin-bottom: 4px; }

/* SOAP */
.soap-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.soap-title-wrap { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.soap-title-wrap h3 { margin: 0; }
.soap-head-actions { display: flex; gap: 8px; }
.soap-field { margin-bottom: 14px; }
.soap-tag {
  display: inline-block; font-weight: 700; font-size: .8rem;
  color: var(--teal-dark); background: var(--teal-light);
  padding: 3px 10px; border-radius: 6px; margin-bottom: 4px;
}
.soap-field textarea { font-family: "SFMono-Regular", ui-monospace, Menlo, Consolas, monospace; font-size: .9rem; }

/* Chip de contexto detectado (editable) */
.context-chip {
  --chip: var(--teal);
  width: auto; margin: 0; padding: 4px 30px 4px 12px;
  font-size: .78rem; font-weight: 700; letter-spacing: .01em;
  border: 1px solid color-mix(in srgb, var(--chip) 45%, transparent);
  color: var(--chip);
  background-color: color-mix(in srgb, var(--chip) 14%, var(--card));
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center;
  border-radius: 20px; cursor: pointer; -webkit-appearance: none; appearance: none;
}
.context-chip:focus { outline: 2px solid var(--teal-light); }
.context-chip.ctx-HOSPI        { --chip: #0d8f8f; }
.context-chip.ctx-NEURO        { --chip: #7c5cd6; }
.context-chip.ctx-RESP_SI      { --chip: #d9534f; }
.context-chip.ctx-PEDIA_NEONAT { --chip: #d6699a; }
.context-chip.ctx-AMBU_MSK     { --chip: #3d7fd6; }
.context-chip.ctx-GYNECO_PELVI { --chip: #c0559b; }
.context-chip.ctx-GENERIQUE    { --chip: var(--muted); }

/* Esqueleto de carga (placeholder pulsante) */
.skel {
  border-radius: 8px;
  background: linear-gradient(90deg, var(--card-alt) 25%, var(--hover) 50%, var(--card-alt) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s ease-in-out infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }
.skel-block { margin-bottom: 18px; }
.skel-tag { width: 120px; height: 22px; margin-bottom: 8px; border-radius: 6px; }
.skel-line { height: 12px; margin-bottom: 8px; }
.skel-line.w90 { width: 90%; }
.skel-line.w70 { width: 70%; }
.skel-line.w50 { width: 50%; }

/* History */
.history-head { display: flex; align-items: center; justify-content: space-between; }
.history-head h3 { margin: 0; }
.history-patient { font-weight: 700; color: var(--teal-dark); }
.history-item {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; margin-top: 10px; cursor: pointer; transition: .12s;
}
.history-item:hover { background: var(--hover); }
.history-item .h-date { font-weight: 700; font-size: .9rem; }
.history-item .h-preview { color: var(--muted); font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.history-empty { color: var(--muted); font-size: .9rem; padding: 8px 0; }

/* Login */
.login-card { max-width: 380px; margin: 8vh auto 0; }
.error { color: var(--danger); font-size: .9rem; }

/* Settings */
.settings-head { display: flex; align-items: center; justify-content: space-between; }
.glossary-text { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; }
hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: .85em; }

/* Modal */
.modal {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none !important; }
.modal-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: var(--overlay);
}
.modal-card {
  position: relative; z-index: 101;
  background: var(--card); border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  max-width: 480px; width: 90%; max-height: 90vh;
  overflow-y: auto; padding: 16px;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.modal-head h2 { margin: 0; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }

/* Terms list */
.terms-list { margin: 12px 0; }
.term-item {
  display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; margin-bottom: 10px; background: var(--card-alt);
}
.term-item input[type="checkbox"] {
  flex-shrink: 0; width: auto; margin: 0;
  cursor: pointer; accent-color: var(--teal);
}
.term-content { flex: 1; }
.term-content .term-label { font-weight: 600; font-size: .95rem; margin-bottom: 4px; }
.term-content .term-definition { font-size: .85rem; color: var(--muted); }
.term-content input {
  width: 100%; margin-top: 6px; padding: 6px 8px; font-size: .85rem;
}

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #1c2526; color: #fff; padding: 12px 18px; border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,.25); z-index: 150; font-size: .9rem;
  max-width: 90%;
}

/* Autocomplete */
.autocomplete-wrapper {
  position: fixed; z-index: 200;
  pointer-events: none;
}
.autocomplete-dropdown {
  position: fixed;
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow);
  min-width: 200px; max-width: 400px;
  max-height: 300px; overflow-y: auto;
  z-index: 200; pointer-events: all;
}
.autocomplete-dropdown.hidden {
  display: none;
}
.autocomplete-item {
  padding: 10px 12px; cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: .1s;
}
.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover,
.autocomplete-item.selected {
  background: var(--teal-light);
}
.autocomplete-item .ac-term {
  display: block; font-weight: 700; color: var(--teal-dark);
  font-size: .95rem; margin-bottom: 3px;
}
.autocomplete-item .ac-def {
  display: block; font-size: .8rem; color: var(--muted);
}

/* Tabs */
.tabs-header {
  display: flex; gap: 10px; margin-bottom: 16px;
  border-bottom: 1px solid var(--border); padding-bottom: 0;
}
.tab-btn {
  flex: 1; padding: 10px 12px; border: none; background: transparent;
  font-weight: 600; cursor: pointer; color: var(--muted);
  border-bottom: 3px solid transparent; transition: .2s;
}
.tab-btn.active {
  color: var(--teal); border-bottom-color: var(--teal);
}
.tab-btn:hover { color: var(--teal-dark); }

/* Glossary controls */
.glossary-controls {
  display: grid; grid-template-columns: 2fr 1fr; gap: 12px;
  margin-bottom: 16px;
}
.search-field { position: relative; }
.search-field .search-ic {
  position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
  color: var(--muted); pointer-events: none;
}
.search-field input { padding-left: 36px; margin-top: 0; }

/* Tab content */
.tab-content {
  display: none;
}
.tab-content.active {
  display: block;
}

/* Term cards grid */
.terms-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}
.term-card {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; background: var(--card-alt);
  transition: .2s;
}
.term-card:hover {
  border-color: var(--teal); background: var(--teal-light);
  box-shadow: 0 2px 8px rgba(13,110,110,.1);
}
.term-card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.term-label {
  font-weight: 700; font-size: .95rem; color: var(--teal-dark);
}
.term-category {
  font-size: .7rem; background: var(--teal-light); color: var(--teal-dark);
  padding: 2px 8px; border-radius: 4px; white-space: nowrap;
}
.term-definition {
  font-size: .85rem; color: var(--muted); margin-bottom: 10px;
  line-height: 1.35;
}

/* Suggestion cards */
.suggestions-list, .rejected-list {
  display: flex; flex-direction: column; gap: 12px;
}
.suggestion-card {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; background: var(--warn-bg);
  border-left: 3px solid var(--warn-border);
}
.suggestion-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.suggestion-date {
  font-size: .75rem; color: var(--muted);
}
.suggestion-content .definition {
  font-weight: 600; margin: 6px 0; padding: 8px 10px;
  background: var(--input-bg); border-radius: 6px; border-left: 2px solid var(--warn-border);
}
.suggestion-content .mt-2 {
  margin-top: 8px;
}
.suggestion-actions {
  display: flex; gap: 8px; margin-top: 10px;
}

.rejected-card {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px; background: var(--card-alt);
  border-left: 3px solid var(--danger);
}

/* Empty state */
.empty-state {
  text-align: center; color: var(--muted);
  padding: 32px 16px; font-size: .95rem;
}

/* Audit history */
.audit-history {
  background: var(--card-alt); border-radius: 6px; padding: 10px 12px;
  margin-top: 10px;
}
.audit-history strong {
  display: block; margin-bottom: 8px; color: var(--teal-dark);
}
.audit-history ul {
  list-style: none; padding: 0; margin: 0;
}
.audit-history li {
  font-size: .82rem; color: var(--muted); padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.audit-history li:last-child {
  border-bottom: none;
}
.audit-history .date {
  font-weight: 600; color: var(--text);
}

/* Spinner */
.spinner {
  display: inline-block; width: 12px; height: 12px;
  border: 2px solid var(--border); border-top-color: var(--teal);
  border-radius: 50%; animation: spin .8s linear infinite;
}
.btn-primary .spinner { border-color: rgba(255,255,255,.4); border-top-color: #fff; }
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Navegación inferior tipo app */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 60;
  display: flex; justify-content: space-around; align-items: stretch;
  background: var(--nav-bg); border-top: 1px solid var(--border);
  box-shadow: 0 -1px 6px rgba(0,0,0,.06);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
  border: none; background: transparent; cursor: pointer;
  padding: 9px 4px 8px; color: var(--muted);
  font-size: .7rem; font-weight: 600; transition: color .15s;
}
.nav-item .ic { width: 22px; height: 22px; }
.nav-item.active { color: var(--teal); }
.nav-item:hover { color: var(--teal-dark); }

/* Responsive */
@media (max-width: 560px) {
  .row-card { grid-template-columns: 1fr; }
  .field-out { text-align: left; }
  .static-pill { display: inline-block; }
  .modal-card { width: 95%; max-width: none; }
  .modal-actions { flex-direction: column; }
  .modal-actions .btn { width: 100%; }
  .autocomplete-dropdown { max-width: 90vw; }
  .glossary-controls { grid-template-columns: 1fr; }
  .terms-grid { grid-template-columns: 1fr; }
  .tabs-header { gap: 4px; }
  .tab-btn { font-size: .85rem; padding: 8px 10px; }
}

/* Menos animación si el usuario lo pide */
@media (prefers-reduced-motion: reduce) {
  .mic-btn[aria-pressed="true"]::before,
  .mic-btn[aria-pressed="true"]::after,
  .skel, .rec-dot, .spinner { animation: none; }
}
