/* ─── Design tokens ─────────────────────────────────────────────────────────── */
:root {
  --navy:       #1B2B4B;
  --teal:       #2A8C8C;
  --teal-light: #3AABAB;
  --teal-dim:   rgba(42,140,140,0.12);
  --green:      #22C55E;
  --red:        #EF4444;
  --amber:      #F59E0B;
  --bg:         #F7F8FA;
  --surface:    #FFFFFF;
  --border:     #E2E8F0;
  --text-primary:   #1B2B4B;
  --text-secondary: #6B7280;
  --text-inverse:   #FFFFFF;
  --radius-sm:  8px;
  --radius-md:  14px;
  --radius-lg:  22px;
  --radius-pill: 999px;
  --shadow-card:  0 2px 8px rgba(27,43,75,0.08);
  --shadow-sheet: 0 -4px 24px rgba(27,43,75,0.12);
  --nav-height:    68px;
  --header-height: 58px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: 'DM Sans', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

/* ─── Header ─────────────────────────────────────────────────────────────────── */
.app-header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-height);
  background: var(--navy);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  padding-top: env(safe-area-inset-top, 0px);
  z-index: 100;
}
.app-logo {
  font-family: 'DM Serif Display', Georgia, serif;
  font-size: 1.25rem; color: var(--text-inverse); letter-spacing: 0.01em;
}
.header-right { display: flex; align-items: center; gap: 10px; }
.connection-pill {
  display: flex; align-items: center; gap: 6px;
  background: rgba(255,255,255,0.1); border-radius: var(--radius-pill);
  padding: 5px 12px; font-size: 0.75rem; color: rgba(255,255,255,0.85); font-weight: 500;
}
.connection-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-secondary); transition: background 0.3s;
}
.connection-dot.online  { background: var(--green); }
.connection-dot.offline { background: var(--red); }
.connection-dot.checking { background: var(--amber); animation: blink 1s ease-in-out infinite; }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.3} }

.header-gear {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: rgba(255,255,255,0.1); display: flex; align-items: center;
  justify-content: center; cursor: pointer; color: rgba(255,255,255,0.85);
  -webkit-tap-highlight-color: transparent; transition: background 0.2s;
}
.header-gear svg { width: 18px; height: 18px; }
.header-gear:active { background: rgba(255,255,255,0.2); }

/* ─── Tab panels ──────────────────────────────────────────────────────────────── */
.tab-panel {
  position: fixed; top: var(--header-height); left: 0; right: 0;
  bottom: calc(var(--nav-height) + var(--safe-bottom));
  overflow-y: auto; -webkit-overflow-scrolling: touch; display: none;
}
.tab-panel.active { display: block; }

/* ─── Record tab ──────────────────────────────────────────────────────────────── */
.record-scene {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; min-height: 100%;
  padding: 24px 24px 32px; gap: 28px;
}

/* Patient name + format fields */
.field-group { width: 100%; max-width: 360px; }
.field-label {
  display: block; font-size: 0.78rem; font-weight: 600;
  color: var(--text-secondary); text-transform: uppercase;
  letter-spacing: 0.06em; margin-bottom: 8px;
}
.field-input {
  width: 100%; padding: 12px 14px;
  border: 1.5px solid var(--border); border-radius: var(--radius-sm);
  font-family: 'DM Sans', sans-serif; font-size: 1rem;
  color: var(--text-primary); background: var(--surface); outline: none;
  transition: border-color 0.2s;
}
.field-input:focus { border-color: var(--teal); }

/* Checkboxes */
.checkbox-row { display: flex; gap: 20px; }
.checkbox-label {
  display: flex; align-items: center; gap: 10px;
  cursor: pointer; font-size: 0.95rem; font-weight: 500;
  color: var(--text-primary); user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.checkbox-label input[type="checkbox"] { display: none; }
.checkbox-box {
  width: 22px; height: 22px; border-radius: 6px;
  border: 2px solid var(--border); background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, border-color 0.15s; flex-shrink: 0;
}
.checkbox-label input:checked + .checkbox-box {
  background: var(--teal); border-color: var(--teal);
}
.checkbox-label input:checked + .checkbox-box::after {
  content: ''; display: block;
  width: 5px; height: 9px;
  border: 2px solid white; border-top: none; border-left: none;
  transform: rotate(45deg) translateY(-1px);
}

/* Record button */
.record-stage {
  position: relative; width: 180px; height: 180px;
  display: flex; align-items: center; justify-content: center;
}
.pulse-ring {
  position: absolute; inset: 0; border-radius: 50%;
  border: 2.5px solid var(--teal); opacity: 0; transform: scale(1);
  pointer-events: none;
}
.pulse-ring.active { animation: breathe 2.4s ease-in-out infinite; }
@keyframes breathe {
  0%   { transform: scale(1);    opacity: 0.6; }
  50%  { transform: scale(1.18); opacity: 0.15; }
  100% { transform: scale(1);    opacity: 0.6; }
}
.record-btn {
  width: 136px; height: 136px; border-radius: 50%; border: none;
  background: var(--teal); color: var(--text-inverse);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; box-shadow: 0 6px 24px rgba(42,140,140,0.35);
  transition: background 0.25s, transform 0.15s, box-shadow 0.25s;
  -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.record-btn:active { transform: scale(0.96); }
.record-btn.recording {
  background: var(--red); box-shadow: 0 6px 24px rgba(239,68,68,0.35);
}
.icon-mic  { width: 48px; height: 48px; }
.icon-stop { width: 40px; height: 40px; }
.hidden    { display: none !important; }

.record-status { text-align: center; }
.timer {
  font-size: 2.5rem; font-weight: 600; letter-spacing: 0.04em;
  color: var(--text-primary); line-height: 1; margin-bottom: 10px;
  font-variant-numeric: tabular-nums;
}
.status-text { font-size: 0.9rem; color: var(--text-secondary); font-weight: 500; }

/* ─── History tab ─────────────────────────────────────────────────────────────── */
.history-header { padding: 24px 20px 12px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.history-header h2 { font-size: 1.25rem; font-weight: 600; }
.pull-history-btn {
  padding: 7px 14px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--teal); background: var(--surface); color: var(--teal);
  font-family: 'DM Sans', sans-serif; font-size: 0.82rem; font-weight: 600;
  cursor: pointer; flex-shrink: 0; transition: background 0.2s, color 0.2s, opacity 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.pull-history-btn:active { background: var(--teal); color: var(--text-inverse); }
.pull-history-btn:disabled { opacity: 0.55; cursor: default; }
.history-banner {
  margin: 0 16px 10px; padding: 10px 14px; border-radius: var(--radius-sm);
  background: var(--teal-dim); color: var(--teal); font-size: 0.82rem; font-weight: 500;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.history-banner.hidden { display: none; }
.history-banner button {
  border: none; background: var(--teal); color: var(--text-inverse);
  border-radius: var(--radius-pill); padding: 5px 12px; font-size: 0.78rem; font-weight: 600;
  cursor: pointer; flex-shrink: 0;
}
.history-filters {
  margin: 0 16px 12px; display: flex; flex-direction: column; gap: 8px;
}
.history-filters.hidden { display: none; }
.history-filters select { display: none; }
.history-filters.server select { display: block; }
.history-filters input,
.history-filters select {
  width: 100%; padding: 10px 14px; border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); font-family: 'DM Sans', sans-serif;
  font-size: 0.9rem; color: var(--text-primary); background: var(--surface);
  outline: none; transition: border-color 0.2s; -webkit-appearance: none; appearance: none;
}
.history-filters select {
  background-image: linear-gradient(45deg, transparent 50%, var(--text-secondary) 50%),
                    linear-gradient(135deg, var(--text-secondary) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat;
  padding-right: 32px;
}
.history-filters input:focus,
.history-filters select:focus { border-color: var(--teal); }
.job-list { padding: 0 16px 24px; }

.empty-state {
  display: flex; flex-direction: column; align-items: center;
  gap: 8px; padding: 64px 24px; text-align: center; color: var(--text-secondary);
}
.empty-state svg { width: 56px; height: 56px; margin-bottom: 8px; opacity: 0.5; }
.empty-state p   { font-size: 1rem; font-weight: 500; }
.empty-state .empty-sub { font-size: 0.85rem; font-weight: 400; }

/* Job card */
.job-card {
  background: var(--surface); border-radius: var(--radius-md);
  border: 1.5px solid var(--border); padding: 14px 16px;
  margin-bottom: 10px; cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-tap-highlight-color: transparent;
}
.job-card:active { border-color: var(--teal); box-shadow: var(--shadow-card); }
.job-card-top {
  display: flex; align-items: flex-start;
  justify-content: space-between; margin-bottom: 6px; gap: 8px;
}
.job-patient-name {
  font-size: 1rem; font-weight: 600; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.job-status {
  font-size: 0.8rem; font-weight: 500;
  display: flex; align-items: center; gap: 5px; flex-shrink: 0;
}
.job-status.complete   { color: var(--green); }
.job-status.failed     { color: var(--red); }
.job-status.pending,
.job-status.processing { color: var(--amber); }

.job-card-bottom { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.job-format-badge {
  font-size: 0.7rem; font-weight: 600; letter-spacing: 0.06em;
  text-transform: uppercase; background: var(--teal-dim); color: var(--teal);
  border-radius: var(--radius-pill); padding: 2px 9px;
}
.job-time { font-size: 0.8rem; color: var(--text-secondary); }

/* ─── Bottom nav ──────────────────────────────────────────────────────────────── */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface); border-top: 1.5px solid var(--border);
  display: flex; z-index: 100;
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 4px; border: none; background: transparent;
  color: var(--text-secondary); font-family: 'DM Sans', sans-serif;
  font-size: 0.72rem; font-weight: 500; cursor: pointer;
  -webkit-tap-highlight-color: transparent; transition: color 0.2s;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--teal); }

/* ─── Sheets ──────────────────────────────────────────────────────────────────── */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(27,43,75,0.4);
  z-index: 200; backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px); animation: fadeIn 0.2s ease;
}
@keyframes fadeIn { from{opacity:0} to{opacity:1} }

.result-sheet, .settings-sheet {
  position: fixed; bottom: 0; left: 0; right: 0; max-height: 88vh;
  background: var(--surface); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  z-index: 201; display: flex; flex-direction: column;
  box-shadow: var(--shadow-sheet);
  animation: slideUp 0.3s cubic-bezier(0.32,0.72,0,1);
  padding-bottom: var(--safe-bottom);
}
@keyframes slideUp { from{transform:translateY(100%)} to{transform:translateY(0)} }

.sheet-handle {
  width: 40px; height: 4px; border-radius: 2px;
  background: var(--border); margin: 12px auto 4px; flex-shrink: 0;
}
.sheet-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.sheet-header h3  { font-size: 1.05rem; font-weight: 600; }
.sheet-subtitle   { font-size: 0.8rem; color: var(--text-secondary); margin-top: 2px; }
.sheet-actions    { display: flex; align-items: center; gap: 10px; }

/* Result tabs (shown only for 'both' format) */
.result-tabs {
  display: flex; border-bottom: 1.5px solid var(--border); flex-shrink: 0;
}
.result-tab {
  flex: 1; padding: 11px; border: none; background: transparent;
  font-family: 'DM Sans', sans-serif; font-size: 0.88rem; font-weight: 500;
  color: var(--text-secondary); cursor: pointer; border-bottom: 2.5px solid transparent;
  margin-bottom: -1.5px; transition: color 0.2s, border-color 0.2s;
}
.result-tab.active { color: var(--teal); border-bottom-color: var(--teal); }

.action-btn {
  padding: 7px 18px; border-radius: var(--radius-pill);
  border: 1.5px solid var(--teal); background: transparent; color: var(--teal);
  font-family: 'DM Sans', sans-serif; font-size: 0.85rem; font-weight: 600;
  cursor: pointer; transition: background 0.2s, color 0.2s;
}
.action-btn:active { background: var(--teal); color: var(--text-inverse); }

.icon-btn {
  width: 34px; height: 34px; border-radius: 50%; border: none;
  background: var(--bg); display: flex; align-items: center;
  justify-content: center; cursor: pointer; color: var(--text-secondary);
  -webkit-tap-highlight-color: transparent;
}
.icon-btn svg { width: 16px; height: 16px; }

.sheet-body {
  padding: 20px; overflow-y: auto;
  -webkit-overflow-scrolling: touch; flex: 1;
}
#sheetBody pre {
  white-space: pre-wrap; font-family: 'DM Sans', sans-serif;
  font-size: 0.92rem; line-height: 1.7; color: var(--text-primary);
}

/* Settings */
.settings-label {
  display: block; font-size: 0.8rem; font-weight: 600;
  color: var(--text-secondary); text-transform: uppercase;
  letter-spacing: 0.06em; margin-bottom: 6px; margin-top: 20px;
}
.settings-label:first-child { margin-top: 0; }
.settings-input {
  width: 100%; padding: 12px 14px; border: 1.5px solid var(--border);
  border-radius: var(--radius-sm); font-family: 'DM Sans', sans-serif;
  font-size: 0.95rem; color: var(--text-primary); background: var(--bg);
  outline: none; transition: border-color 0.2s;
}
.settings-input:focus { border-color: var(--teal); }
.save-btn {
  width: 100%; margin-top: 28px; padding: 14px; border-radius: var(--radius-md);
  border: none; background: var(--navy); color: var(--text-inverse);
  font-family: 'DM Sans', sans-serif; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: opacity 0.2s;
}
.save-btn:active { opacity: 0.85; }

/* ─── Toast ───────────────────────────────────────────────────────────────────── */
.toast-container {
  position: fixed; top: calc(var(--header-height) + 12px);
  left: 16px; right: 16px; z-index: 300;
  display: flex; flex-direction: column; gap: 8px; pointer-events: none;
}
.toast {
  background: var(--navy); color: var(--text-inverse);
  border-radius: var(--radius-md); padding: 13px 18px;
  font-size: 0.88rem; font-weight: 500;
  box-shadow: 0 4px 16px rgba(27,43,75,0.2);
  animation: toastIn 0.25s cubic-bezier(0.32,0.72,0,1);
  pointer-events: auto;
}
.toast.error   { background: var(--red); }
.toast.success { background: #166534; }
@keyframes toastIn  { from{opacity:0;transform:translateY(-8px)} to{opacity:1;transform:translateY(0)} }
@keyframes toastOut { from{opacity:1} to{opacity:0;transform:translateY(-4px)} }

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

@media (prefers-reduced-motion: reduce) {
  .pulse-ring.active { animation: none; opacity: 0.4; }
  .toast, .result-sheet, .settings-sheet, .sheet-backdrop { animation: none; }
}
