/* =========================================================================
   Traceo PWA — Styles
   Mobile-first, system fonts, CSS custom properties
   ========================================================================= */

:root {
    --color-primary: #1E40AF;
    --color-primary-light: #3B82F6;
    --color-primary-dark: #1E3A8A;
    --color-success: #059669;
    --color-warning: #D97706;
    --color-danger: #DC2626;
    --color-info: #0284C7;

    --color-bg: #F1F5F9;
    --color-surface: #FFFFFF;
    --color-surface-alt: #F8FAFC;
    --color-border: #E2E8F0;
    --color-text: #1E293B;
    --color-text-secondary: #64748B;
    --color-text-muted: #94A3B8;

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 2px rgba(0,0,0,.05);
    --shadow-md: 0 4px 12px rgba(0,0,0,.08);
    --shadow-lg: 0 8px 24px rgba(0,0,0,.12);

    --header-height: 56px;
    --bottom-nav-height: 64px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; -webkit-text-size-adjust: 100%; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    color: var(--color-text);
    background: var(--color-bg);
    height: 100%;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
}
a { color: var(--color-primary); text-decoration: none; }
button { font: inherit; cursor: pointer; border: none; background: none; }
input, textarea, select { font: inherit; }
img { max-width: 100%; display: block; }

/* ---- App shell ---- */
#app {
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.view {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.view-scroll {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 16px;
    padding-bottom: calc(16px + var(--safe-bottom));
}

/* ---- Status bar ---- */
.status-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 16px;
    font-size: 12px;
    font-weight: 500;
    background: var(--color-warning);
    color: white;
    text-align: center;
    justify-content: center;
}
.status-bar.online { background: var(--color-success); }
.status-bar.offline { background: var(--color-danger); }
.status-bar.hidden { display: none; }

#status-indicator {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: currentColor;
}

/* ---- Header ---- */
.header {
    display: flex;
    align-items: center;
    height: var(--header-height);
    padding: 0 16px;
    background: var(--color-primary);
    color: white;
    flex-shrink: 0;
    gap: 12px;
    z-index: 10;
}
.header-title {
    flex: 1;
    font-size: 18px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.header-subtitle {
    font-size: 12px;
    opacity: .8;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.header-back, .header-action {
    width: 40px; height: 40px;
    display: flex; align-items: center; justify-content: center;
    color: white;
    border-radius: var(--radius-full);
    flex-shrink: 0;
}
.header-back:active, .header-action:active { background: rgba(255,255,255,.15); }

/* ---- Bottom navigation ---- */
.bottom-nav {
    display: flex;
    height: var(--bottom-nav-height);
    padding-bottom: var(--safe-bottom);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    flex-shrink: 0;
    z-index: 10;
}
.nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--color-text-muted);
    font-size: 11px;
    font-weight: 500;
    transition: color .15s;
}
.nav-item.active { color: var(--color-primary); }
.nav-item svg { width: 24px; height: 24px; }

/* ---- Cards ---- */
.card {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--color-border);
    overflow: hidden;
}
.card + .card { margin-top: 10px; }
.card-body { padding: 14px 16px; }
.card-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-bottom: 1px solid var(--color-border);
    cursor: pointer;
}
.card-row:last-child { border-bottom: none; }
.card-row:active { background: var(--color-surface-alt); }

/* ---- Buttons ---- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 20px;
    border-radius: var(--radius-sm);
    font-size: 15px;
    font-weight: 600;
    transition: opacity .15s;
    white-space: nowrap;
}
.btn:active { opacity: .8; }
.btn-primary {
    background: var(--color-primary);
    color: white;
}
.btn-secondary {
    background: var(--color-bg);
    color: var(--color-text);
    border: 1px solid var(--color-border);
}
.btn-danger {
    background: var(--color-danger);
    color: white;
}
.btn-success {
    background: var(--color-success);
    color: white;
}
.btn-block { width: 100%; }
.btn-sm {
    padding: 8px 14px;
    font-size: 13px;
}
.btn-lg {
    padding: 14px 24px;
    font-size: 16px;
}
.btn:disabled {
    opacity: .5;
    pointer-events: none;
}

/* ---- Forms ---- */
.form-group { margin-bottom: 16px; }
.form-label {
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text-secondary);
    margin-bottom: 6px;
}
.form-input, .form-select, .form-textarea {
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    font-size: 15px;
    color: var(--color-text);
    transition: border-color .15s;
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(30,64,175,.1);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-error {
    font-size: 12px;
    color: var(--color-danger);
    margin-top: 4px;
}

/* ---- Badges ---- */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: var(--radius-full);
    font-size: 12px;
    font-weight: 600;
}
.badge-draft { background: #E2E8F0; color: #475569; }
.badge-in_progress, .badge-in-progress { background: #DBEAFE; color: #1E40AF; }
.badge-completed { background: #D1FAE5; color: #065F46; }
.badge-cancelled { background: #FEE2E2; color: #991B1B; }
.badge-invoiced { background: #FEF3C7; color: #92400E; }
.badge-on_hold, .badge-on-hold { background: #FED7AA; color: #9A3412; }
.badge-emergency { background: var(--color-danger); color: white; }

/* ---- Lists ---- */
.list-empty {
    text-align: center;
    padding: 48px 24px;
    color: var(--color-text-muted);
}
.list-empty svg { margin: 0 auto 12px; opacity: .4; }
.list-empty p { font-size: 14px; }

/* ---- Avatars / Icons ---- */
.avatar {
    width: 40px; height: 40px;
    border-radius: var(--radius-full);
    background: var(--color-primary-light);
    color: white;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; font-weight: 700;
    flex-shrink: 0;
}
.icon-circle {
    width: 36px; height: 36px;
    border-radius: var(--radius-full);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}

/* ---- Note type icons ---- */
.note-icon { font-size: 18px; }
.note-audio .note-icon { color: var(--color-danger); }
.note-photo .note-icon { color: var(--color-info); }
.note-text .note-icon { color: var(--color-text-secondary); }
.note-measure .note-icon { color: var(--color-warning); }
.note-material .note-icon { color: var(--color-success); }
.note-time .note-icon { color: #7C3AED; }

/* ---- Timeline ---- */
.phase-section { margin-bottom: 16px; }
.phase-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 0;
    cursor: pointer;
    user-select: none;
}
.phase-header .phase-arrow {
    transition: transform .2s;
    color: var(--color-text-muted);
}
.phase-header.collapsed .phase-arrow { transform: rotate(-90deg); }
.phase-title { flex: 1; font-weight: 600; font-size: 15px; }
.phase-notes { display: flex; flex-direction: column; gap: 8px; }

.note-card {
    background: var(--color-surface);
    border-radius: var(--radius-md);
    border: 1px solid var(--color-border);
    padding: 12px 14px;
    cursor: pointer;
}
.note-card:active { background: var(--color-surface-alt); }
.note-card-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}
.note-card-header .note-title { flex: 1; font-weight: 600; font-size: 14px; }
.note-card-header .note-time { font-size: 12px; color: var(--color-text-muted); }
.note-card-preview {
    font-size: 13px;
    color: var(--color-text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.note-card-thumbs {
    display: flex;
    gap: 6px;
    margin-top: 8px;
}
.note-card-thumbs img {
    width: 56px; height: 56px;
    object-fit: cover;
    border-radius: 6px;
    background: var(--color-bg);
}

/* ---- Bottom sheet ---- */
.bottom-sheet-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 100;
    opacity: 0;
    transition: opacity .2s;
    pointer-events: none;
}
.bottom-sheet-overlay.active { opacity: 1; pointer-events: auto; }

.bottom-sheet {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: var(--color-surface);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    z-index: 101;
    transform: translateY(100%);
    transition: transform .3s cubic-bezier(.32,.72,0,1);
    max-height: 85vh;
    overflow-y: auto;
    padding-bottom: var(--safe-bottom);
}
.bottom-sheet.active { transform: translateY(0); }
.bottom-sheet-handle {
    width: 36px; height: 4px;
    border-radius: 2px;
    background: var(--color-border);
    margin: 10px auto 6px;
}
.bottom-sheet-title {
    font-size: 17px;
    font-weight: 700;
    padding: 8px 20px 16px;
}
.bottom-sheet-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    padding: 0 20px 20px;
}
.sheet-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 18px 12px;
    border-radius: var(--radius-md);
    background: var(--color-surface-alt);
    border: 1px solid var(--color-border);
    font-size: 13px;
    font-weight: 600;
    color: var(--color-text);
}
.sheet-btn:active { background: var(--color-bg); }
.sheet-btn .sheet-icon { font-size: 28px; }

/* ---- FAB (Floating Action Button) ---- */
.fab {
    position: fixed;
    right: 20px;
    bottom: calc(var(--bottom-nav-height) + 16px + var(--safe-bottom));
    width: 56px; height: 56px;
    border-radius: var(--radius-full);
    background: var(--color-primary);
    color: white;
    display: flex; align-items: center; justify-content: center;
    box-shadow: var(--shadow-lg);
    z-index: 20;
    font-size: 28px;
}
.fab:active { transform: scale(.92); }

/* ---- Sticky action bar ---- */
.sticky-bottom {
    display: flex;
    gap: 10px;
    padding: 12px 16px;
    padding-bottom: calc(12px + var(--safe-bottom));
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    flex-shrink: 0;
}

/* ---- Search bar ---- */
.search-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 14px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    margin-bottom: 12px;
}
.search-bar input {
    flex: 1;
    border: none;
    outline: none;
    background: none;
    font-size: 15px;
}
.search-bar svg { color: var(--color-text-muted); flex-shrink: 0; }

/* ---- Toast ---- */
#toast-container {
    position: fixed;
    bottom: calc(var(--bottom-nav-height) + 16px + var(--safe-bottom));
    left: 16px; right: 16px;
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}
.toast {
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    color: white;
    font-size: 14px;
    font-weight: 500;
    box-shadow: var(--shadow-md);
    animation: toast-in .3s ease;
    pointer-events: auto;
}
.toast-success { background: var(--color-success); }
.toast-error { background: var(--color-danger); }
.toast-info { background: var(--color-info); }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ---- Loading spinner ---- */
.spinner {
    width: 24px; height: 24px;
    border: 3px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin .6s linear infinite;
}
.spinner-center {
    display: flex;
    justify-content: center;
    padding: 32px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ---- Login view ---- */
.login-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 32px 24px;
    min-height: 100%;
}
.login-logo {
    width: 72px; height: 72px;
    margin-bottom: 8px;
}
.login-title {
    font-size: 28px;
    font-weight: 800;
    color: var(--color-primary);
    margin-bottom: 4px;
}
.login-subtitle {
    font-size: 14px;
    color: var(--color-text-muted);
    margin-bottom: 32px;
}
.login-form { width: 100%; max-width: 360px; }
.login-error {
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    background: #FEE2E2;
    color: var(--color-danger);
    font-size: 13px;
    margin-bottom: 16px;
    display: none;
}

/* ---- Utilities ---- */
.text-muted { color: var(--color-text-muted); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.font-bold { font-weight: 700; }
.flex { display: flex; }
.flex-1 { flex: 1; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.p-16 { padding: 16px; }
.hidden { display: none !important; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
