*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #f5f5f5;
    --card-bg: #fff;
    --text: #222;
    --text-muted: #777;
    --border: #ddd;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --secondary: #6b7280;
    --secondary-hover: #4b5563;
    --ecg-bg: #fff8f7;
    --ecg-grid-sm: #f1dedf;
    --ecg-grid-md: #fdbeb9;
    --ecg-grid-lg: #e0514b;
    --ecg-line: #111;
    --success: #16a34a;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    padding: 1rem;
    max-width: 1200px;
    margin: 0 auto;
}

header {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    margin-bottom: 1.5rem;
}

header h1 {
    font-size: 1.6rem;
    font-weight: 700;
}

header .subtitle {
    font-weight: 300;
    color: var(--text-muted);
}

.btn-logout {
    position: absolute;
    right: 0;
    font-size: 0.78rem;
}

/* Cards */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 1rem;
    overflow: hidden;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: #fafafa;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.card-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.card-body {
    padding: 1rem;
}

.hidden {
    display: none !important;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 6px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary);
    color: #fff;
}
.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-danger {
    background: var(--danger);
    color: #fff;
}
.btn-danger:hover:not(:disabled) {
    background: var(--danger-hover);
}

.btn-secondary {
    background: var(--secondary);
    color: #fff;
}
.btn-secondary:hover:not(:disabled) {
    background: var(--secondary-hover);
}

.btn-sm {
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
}

/* Status badge */
.status {
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
}

.status.disconnected {
    background: #fee2e2;
    color: var(--danger);
}

.status.connecting {
    background: #fef3c7;
    color: #d97706;
}

.status.connected {
    background: #dcfce7;
    color: var(--success);
}

/* Device info grid */
.info-grid {
    display: flex;
    gap: 2rem;
    flex-wrap: wrap;
}

.info-item {
    display: flex;
    flex-direction: column;
}

.info-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.info-value {
    font-size: 1.1rem;
    font-weight: 600;
}

/* File list */
#file-list {
    list-style: none;
}

#file-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0;
    border-bottom: 1px solid #eee;
}

#file-list li:last-child {
    border-bottom: none;
}

.file-name {
    font-weight: 500;
}

.file-meta {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-left: 0.5rem;
}

.empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 1rem;
    font-style: italic;
}

/* Progress bar */
.progress-bar-container {
    width: 100%;
    height: 8px;
    background: #e5e7eb;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 0.5rem;
}

.progress-bar {
    height: 100%;
    background: var(--primary);
    border-radius: 4px;
    transition: width 0.2s;
}

#download-status {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* ECG controls */
.ecg-controls {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    flex-wrap: wrap;
}

.ecg-controls label {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.ecg-controls select {
    font-size: 0.8rem;
    padding: 0.2rem 0.4rem;
    border: 1px solid var(--border);
    border-radius: 4px;
}

/* ECG info bar */
.ecg-info {
    display: flex;
    gap: 1.5rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
    flex-wrap: wrap;
}

/* ECG canvas container */
.ecg-canvas-container {
    position: relative;
    overflow-x: auto;
    overflow-y: hidden;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--ecg-bg);
}

#ecg-canvas {
    display: block;
}

#marker-canvas {
    position: absolute;
    top: 0;
    left: 0;
    display: block;
    pointer-events: auto;
    cursor: crosshair;
}

/* Device metrics bar */
.ecg-metrics {
    display: flex;
    gap: 1.2rem;
    flex-wrap: wrap;
    margin-bottom: 0.5rem;
    padding: 0.5rem 0.75rem;
    background: #f0f9ff;
    border: 1px solid #bae6fd;
    border-radius: 6px;
}

.metric {
    display: flex;
    align-items: baseline;
    gap: 0.25rem;
}

.metric-label {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.metric-value {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
}

.metric-unit {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.metric-result {
    flex-basis: 100%;
    border-top: 1px solid #bae6fd;
    padding-top: 0.3rem;
    margin-top: 0.1rem;
}

.metric-result .metric-value {
    font-size: 0.85rem;
    font-weight: 600;
    color: #0369a1;
}

/* Marker measurement panel */
.marker-panel {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    margin-bottom: 0.5rem;
    padding: 0.4rem 0.75rem;
    background: #fefce8;
    border: 1px solid #fde68a;
    border-radius: 6px;
    font-size: 0.82rem;
}

.marker-info {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    flex-wrap: wrap;
    font-variant-numeric: tabular-nums;
}

.marker-a-color {
    display: inline-block;
    width: 16px;
    text-align: center;
    font-weight: 700;
    color: #fff;
    background: #dc2626;
    border-radius: 3px;
    font-size: 0.75rem;
    line-height: 1.4;
}

.marker-b-color {
    display: inline-block;
    width: 16px;
    text-align: center;
    font-weight: 700;
    color: #fff;
    background: #2563eb;
    border-radius: 3px;
    font-size: 0.75rem;
    line-height: 1.4;
}

.marker-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-top: 0.1rem;
}

.snap-btn {
    padding: 2px 8px;
    font-size: 0.72rem;
    line-height: 1.4;
    background: #fff;
    border: 1px solid #d1d5db;
    color: #374151;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.snap-btn:hover {
    background: #f3f4f6;
    border-color: #9ca3af;
}

.projected-label {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.75rem;
    color: #374151;
    cursor: pointer;
    user-select: none;
}

.projected-label input[type="checkbox"] {
    cursor: pointer;
    accent-color: #22c55e;
}

.marker-hint {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-style: italic;
}

/* ─── Badges ─────────────────────────────────────────────── */

.badge {
    display: inline-block;
    padding: 0.1em 0.5em;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    vertical-align: middle;
    line-height: 1.5;
}

.badge-cached {
    background: #dcfce7;
    color: #166534;
    border: 1px solid #bbf7d0;
}

.badge-new {
    background: #fef3c7;
    color: #92400e;
    border: 1px solid #fde68a;
}

.badge-hr {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}

.badge-count {
    background: #e0e7ff;
    color: #3730a3;
    border: 1px solid #c7d2fe;
    margin-left: 0.4rem;
    font-size: 0.75rem;
}

/* ─── File list item layout ──────────────────────────────── */

.file-info {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.file-actions {
    display: flex;
    gap: 0.3rem;
    flex-shrink: 0;
}

.db-saved {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-left: auto;
    white-space: nowrap;
}

/* ─── DB history list ────────────────────────────────────── */

#db-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.6rem 0;
    border-bottom: 1px solid #eee;
}

#db-list li:last-child {
    border-bottom: none;
}

#db-section .card-body {
    max-height: 360px;
    overflow-y: auto;
}

/* ─── Delete icon button ─────────────────────────────────── */

.btn-icon {
    padding: 0.2rem 0.55rem;
    font-size: 0.75rem;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1.4;
}

.btn-icon:hover {
    background: #fee2e2;
    border-color: #fca5a5;
    color: #dc2626;
}

/* ─── Auto-sync progress bar ─────────────────────────────── */

.auto-sync-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.5rem;
    padding: 0.45rem 0.75rem;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: 6px;
    font-size: 0.82rem;
    color: #1d4ed8;
}

.sync-spinner {
    display: inline-block;
    animation: spin 1.2s linear infinite;
    font-size: 1.1rem;
    line-height: 1;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* ─── Auth overlay ───────────────────────────────────────────── */

.auth-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.72);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.auth-overlay.hidden {
    display: none;
}

.auth-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    padding: 2rem 2.5rem;
    width: 100%;
    max-width: 360px;
}

.auth-logo {
    text-align: center;
    margin-bottom: 1.5rem;
}

.auth-logo h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0 0 0.2rem;
}

.auth-subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin: 0;
}

.auth-card h2 {
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0 0 1.1rem;
    text-align: center;
}

.auth-hint {
    font-size: 0.82rem;
    color: var(--text-muted);
    text-align: center;
    margin: -0.5rem 0 1rem;
}

.auth-label {
    display: block;
    font-size: 0.82rem;
    font-weight: 500;
    margin-bottom: 0.3rem;
    color: var(--text-muted);
}

.auth-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.95rem;
    background: var(--bg);
    color: var(--text);
    margin-bottom: 1rem;
    outline: none;
    transition: border-color 0.15s;
}

.auth-input:focus {
    border-color: var(--primary);
}

.auth-btn {
    width: 100%;
    padding: 0.6rem;
    font-size: 0.95rem;
}

.auth-error {
    margin-top: 0.6rem;
    padding: 0.4rem 0.65rem;
    background: #fee2e2;
    border: 1px solid #fca5a5;
    border-radius: 6px;
    color: #b91c1c;
    font-size: 0.82rem;
    text-align: center;
}

/* Log */
.log-toggle {
    cursor: pointer;
    user-select: none;
}

.log-toggle:hover {
    background: #f0f0f0;
}

.toggle-icon {
    font-size: 0.7rem;
    margin-left: 0.3rem;
    transition: transform 0.2s;
    display: inline-block;
}

.toggle-icon.open {
    transform: rotate(90deg);
}

.log-collapsed {
    display: none;
}

.log-output {
    max-height: 200px;
    overflow-y: auto;
    font-family: 'Consolas', 'Courier New', monospace;
    font-size: 0.75rem;
    line-height: 1.6;
    padding: 0.5rem;
    background: #1a1a2e;
    color: #a8b2d1;
    border-radius: 4px;
}

.log-output .log-entry {
    white-space: pre-wrap;
    word-break: break-all;
}

.log-output .log-time {
    color: #5a6a8a;
}

.log-output .log-tx {
    color: #e06c75;
}

.log-output .log-rx {
    color: #61afef;
}

.log-output .log-info {
    color: #98c379;
}

.log-output .log-error {
    color: #e06c75;
    font-weight: bold;
}

/* Responsive */
@media (max-width: 600px) {
    body {
        padding: 0.5rem;
    }
    .card-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .ecg-controls {
        width: 100%;
    }
    .info-grid {
        gap: 1rem;
    }
    .modal-dialog {
        width: 98vw;
        height: 96vh;
        margin: 2vh 1vw;
    }
}

/* ─── ECG Modal ─────────────────────────────────────────── */

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(2px);
}

.modal-dialog {
    background: var(--card-bg);
    border-radius: 10px;
    width: 94vw;
    max-width: 1400px;
    height: 90vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: #fafafa;
    flex-shrink: 0;
}

.modal-header h2 {
    font-size: 1rem;
    font-weight: 600;
}

.modal-close-btn {
    background: none;
    border: none;
    font-size: 1.6rem;
    line-height: 1;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0 0.3rem;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
}

.modal-close-btn:hover {
    background: #fee2e2;
    color: var(--danger);
}

.modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.modal-progress-wrap {
    text-align: center;
    padding: 2rem 0;
}

.modal-progress-wrap .progress-bar-container {
    max-width: 500px;
    margin: 0 auto 0.75rem;
}

.modal-progress-wrap #download-status {
    font-size: 0.9rem;
    color: var(--text-muted);
}
