/* Reset & Base */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
}

body {
    font-family: var(--font-sans);
    background-color: var(--background);
    color: var(--foreground);
    line-height: 1.6;
    min-height: 100vh;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Login Overlay */
.login-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--background);
    z-index: 9999;
}

.login-overlay.hidden {
    display: none;
}

.login-card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    width: 100%;
    max-width: 360px;
    text-align: center;
    box-shadow: var(--shadow-lg);
}

.login-card h1 {
    font-family: var(--font-mono);
    font-size: 1.75rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 0.25rem;
}

.login-card > p {
    color: var(--muted-foreground);
    font-size: 0.875rem;
    margin-bottom: 1.5rem;
}

.login-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.login-error {
    color: var(--destructive);
    font-size: 0.875rem;
    margin-top: 0.5rem;
}

/* App Container */
.app-container {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app-container.hidden {
    display: none;
}

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 2rem;
    background-color: var(--card);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
}

.app-header h1 {
    font-family: var(--font-mono);
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--primary);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: var(--muted);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    color: var(--foreground);
    transition: all 0.2s ease;
}

.theme-toggle:hover {
    background: var(--accent);
    color: var(--accent-foreground);
}

.theme-toggle .icon-moon {
    display: none;
}

.dark .theme-toggle .icon-sun {
    display: none;
}

.dark .theme-toggle .icon-moon {
    display: block;
}

/* Header Stats */
.header-stats {
    display: flex;
    gap: 1.5rem;
    font-family: var(--font-mono);
    font-size: 0.875rem;
    font-weight: 700;
}

.header-stats span {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.header-stats strong {
    font-weight: 700;
}

.stat-item {
    padding: 0.25rem 0.5rem;
    border-radius: var(--radius);
    transition: background-color 0.2s;
}

.stat-item:hover {
    background-color: var(--muted);
}

.stat-item.active {
    background-color: var(--accent);
}

.stat-open {
    color: #f59e0b;
}

.stat-followup {
    color: #3b82f6;
}

.stat-closed {
    color: #ef4444;
}

.stat-goal {
    color: #22c55e;
}

@media (max-width: 768px) {
    .header-stats {
        gap: 1rem;
        font-size: 0.75rem;
    }
}

/* Main Content */
.main-content {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1rem 2rem;
    flex: 1;
    overflow: hidden;
}

/* Table Toolbar */
.table-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    flex-wrap: wrap;
}

/* View Toggle */
.view-toggle {
    display: flex;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.view-btn {
    padding: 0.5rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 500;
    background: var(--card);
    border: none;
    color: var(--muted-foreground);
    cursor: pointer;
    transition: all 0.15s ease;
}

.view-btn:not(:last-child) {
    border-right: 1px solid var(--border);
}

.view-btn:hover {
    background: var(--muted);
    color: var(--foreground);
}

.view-btn.active {
    background: var(--primary);
    color: var(--primary-foreground);
}

/* Timeframe Picker */
.timeframe-picker {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.timeframe-buttons {
    display: flex;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.timeframe-btn {
    padding: 0.375rem 0.625rem;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    font-weight: 500;
    background: var(--card);
    border: none;
    color: var(--muted-foreground);
    cursor: pointer;
    transition: all 0.15s ease;
}

.timeframe-btn:not(:last-child) {
    border-right: 1px solid var(--border);
}

.timeframe-btn:hover {
    background: var(--muted);
    color: var(--foreground);
}

.timeframe-btn.active {
    background: var(--secondary);
    color: var(--secondary-foreground);
}

.date-picker-wrapper {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.date-picker-wrapper span {
    color: var(--muted-foreground);
    font-size: 0.75rem;
}

.date-picker-wrapper input[type="date"] {
    padding: 0.375rem 0.5rem;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--foreground);
    cursor: pointer;
    width: 110px;
}

.date-picker-wrapper input[type="date"]:focus {
    outline: none;
    border-color: var(--ring);
}

.date-field-toggle {
    display: flex;
    gap: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

.date-field-btn {
    padding: 0.375rem 0.5rem;
    font-family: var(--font-mono);
    font-size: 0.625rem;
    font-weight: 500;
    background: var(--card);
    border: none;
    color: var(--muted-foreground);
    cursor: pointer;
    transition: all 0.15s ease;
}

.date-field-btn:first-child {
    border-right: 1px solid var(--border);
}

.date-field-btn:hover {
    background: var(--muted);
    color: var(--foreground);
}

.date-field-btn.active {
    background: var(--accent);
    color: var(--accent-foreground);
}

.search-box {
    flex: 1;
    max-width: 300px;
}

.search-box input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    background: var(--card);
    border: 1px solid var(--input);
    border-radius: var(--radius);
    color: var(--foreground);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.search-box input:focus {
    outline: none;
    border-color: var(--ring);
    box-shadow: 0 0 0 2px color-mix(in oklch, var(--ring) 25%, transparent);
}

.list-stats {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    color: var(--muted-foreground);
}

/* Table Container */
.table-container {
    flex: 1;
    overflow: auto;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

/* Contacts Table */
.contacts-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8125rem;
}

.contacts-table thead {
    position: sticky;
    top: 0;
    background: var(--muted);
    z-index: 10;
}

.contacts-table th {
    padding: 0.625rem 0.75rem;
    text-align: left;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted-foreground);
    border-bottom: 1px solid var(--border);
}

.contacts-table td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.contacts-table tbody tr:hover {
    background: color-mix(in oklch, var(--primary) 5%, var(--card));
}

.contacts-table tbody tr.expanded {
    background: color-mix(in oklch, var(--primary) 10%, var(--card));
}

/* Column widths */
.col-id { width: 60px; }
.col-created { width: 90px; font-size: 0.75rem; color: var(--muted-foreground); }
.col-name { width: 160px; }
.col-company { width: 160px; }
.col-role { width: 110px; }
.col-email { width: 170px; }
.col-phone { width: 130px; }
.col-updated { width: 120px; }
.col-actions { width: 130px; text-align: right; }

/* Sortable column header */
.sortable {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.sortable:hover {
    background-color: var(--muted);
}

.sortable .sort-icon {
    margin-left: 0.25rem;
    opacity: 0.4;
    vertical-align: middle;
}

.sortable.sort-asc .sort-icon,
.sortable.sort-desc .sort-icon {
    opacity: 1;
}

.sortable.sort-asc .sort-icon path:last-child {
    opacity: 0.3;
}

.sortable.sort-desc .sort-icon path:first-child {
    opacity: 0.3;
}

/* Editable phone input */
.phone-input {
    width: 100%;
    padding: 0.25rem 0.5rem;
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    background: var(--background);
    border: 1px solid var(--input);
    border-radius: var(--radius);
    color: var(--foreground);
}

.phone-input:focus {
    outline: none;
    border-color: var(--ring);
}

.phone-input[readonly] {
    background: transparent;
    border-color: transparent;
}

.phone-input[readonly]:focus {
    border-color: transparent;
}

/* Phone field with edit/save buttons */
.phone-field {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.phone-field .phone-input {
    flex: 1;
    min-width: 0;
}

.phone-field .btn-icon {
    flex-shrink: 0;
    width: 24px;
    height: 24px;
    padding: 0;
}

.phone-field .btn-edit-phone {
    color: var(--muted-foreground);
}

.phone-field .btn-save-phone {
    color: var(--primary);
}

.phone-field .btn-search-phone {
    color: #3b82f6;
}

.phone-field .btn-search-phone:hover {
    background: #3b82f6;
    color: white;
}

/* Spinning animation */
@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.spin {
    animation: spin 1s linear infinite;
}

/* Action buttons in table */
.row-actions {
    display: flex;
    gap: 0.375rem;
    justify-content: flex-end;
}

.row-actions .btn-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    background: var(--muted);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--foreground);
    cursor: pointer;
    transition: all 0.15s ease;
}

.row-actions .btn-icon:hover {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--primary-foreground);
}

.row-actions .btn-icon.btn-call {
    background: var(--secondary);
    border-color: var(--secondary);
    color: var(--secondary-foreground);
}

.row-actions .btn-icon.btn-call.disabled {
    opacity: 0.4;
    pointer-events: none;
    cursor: not-allowed;
}

.row-actions .btn-icon.btn-followup {
    background: transparent;
    border-color: #3b82f6;
    color: #3b82f6;
}

.row-actions .btn-icon.btn-followup:hover {
    background: #3b82f6;
    color: white;
}

.row-actions .btn-icon.btn-replied {
    background: transparent;
    border-color: var(--secondary);
    color: var(--secondary);
}

.row-actions .btn-icon.btn-replied:hover {
    background: var(--secondary);
    color: var(--secondary-foreground);
}

.row-actions .btn-icon.btn-close {
    background: transparent;
    border-color: var(--destructive);
    color: var(--destructive);
}

.row-actions .btn-icon.btn-close:hover {
    background: var(--destructive);
    color: var(--destructive-foreground);
}

.row-actions .btn-icon.btn-revert {
    background: transparent;
    border-color: var(--primary);
    color: var(--primary);
}

.row-actions .btn-icon.btn-revert:hover {
    background: var(--primary);
    color: var(--primary-foreground);
}

/* Expanded row details */
.row-details {
    background: var(--background);
}

.row-details td {
    padding: 1rem;
}

.details-content {
    display: flex;
    gap: 1.5rem;
    align-items: stretch;
}

.details-notes {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.details-notes label,
.details-activity label {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted-foreground);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.notes-input-row {
    display: flex;
    gap: 0.5rem;
    align-items: flex-start;
    flex: 1;
}

.details-notes textarea {
    flex: 1;
    min-height: 80px;
    padding: 0.5rem;
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    background: var(--card);
    border: 1px solid var(--input);
    border-radius: var(--radius);
    color: var(--foreground);
    resize: vertical;
}

.details-notes .btn-save-notes {
    flex-shrink: 0;
}

/* Activity Log */
.details-activity {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 350px;
}

.activity-log {
    background: var(--card);
    border: 1px solid var(--input);
    border-radius: var(--radius);
    max-height: 150px;
    overflow-y: auto;
    padding: 0.5rem;
}

.activity-item {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.375rem 0;
    border-bottom: 1px solid var(--border);
    font-size: 0.75rem;
}

.activity-item:last-child {
    border-bottom: none;
}

.activity-icon {
    flex-shrink: 0;
    font-size: 0.875rem;
}

.activity-text {
    flex: 1;
    color: var(--foreground);
    word-break: break-word;
}

.activity-time {
    flex-shrink: 0;
    color: var(--muted-foreground);
    font-size: 0.625rem;
    font-family: var(--font-mono);
}

.activity-empty {
    color: var(--muted-foreground);
    font-size: 0.75rem;
    text-align: center;
    padding: 1rem;
}

.details-audio {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.details-actions {
    display: flex;
    gap: 0.5rem;
}

.contact-card.active {
    border-color: var(--primary);
    background: color-mix(in oklch, var(--primary) 10%, var(--card));
}

.contact-card .name {
    font-family: var(--font-mono);
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--foreground);
}

.contact-card .company {
    font-size: 0.8125rem;
    color: var(--muted-foreground);
}

.contact-card .position {
    font-size: 0.75rem;
    color: var(--muted-foreground);
    opacity: 0.8;
}

/* Detail Section */
.detail-section {
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 2rem;
    height: fit-content;
}

.contact-detail {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    min-height: 400px;
}

/* Detail Header */
.detail-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.detail-header .name {
    font-family: var(--font-mono);
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--primary);
}

.detail-header .company {
    font-size: 0.9375rem;
    color: var(--foreground);
    margin-top: 0.25rem;
}

.detail-header .position {
    font-size: 0.8125rem;
    color: var(--muted-foreground);
}

/* Contact Actions */
.contact-actions {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1.5rem;
}

.action-link {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    text-decoration: none;
    background: var(--muted);
    color: var(--foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    transition: all 0.2s ease;
}

.action-link:hover {
    background: var(--primary);
    color: var(--primary-foreground);
    border-color: var(--primary);
}

.action-link.phone-link {
    background: var(--secondary);
    color: var(--secondary-foreground);
    border-color: var(--secondary);
}

.action-link.phone-link:hover {
    opacity: 0.9;
}

/* Form Groups */
.form-group {
    background: var(--background);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 1rem;
}

.group-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--foreground);
    background: var(--muted);
    cursor: pointer;
    user-select: none;
    transition: background 0.2s ease;
}

.group-header:hover {
    background: var(--popover);
}

.toggle-icon {
    transition: transform 0.2s ease;
}

.form-group:not(.open) .toggle-icon {
    transform: rotate(-90deg);
}

.group-content {
    display: none;
    padding: 1rem;
    flex-direction: column;
    gap: 0.75rem;
}

.form-group.open .group-content {
    display: flex;
}

/* Form Fields */
.form-field {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.form-field label {
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--muted-foreground);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.form-field input[type="text"],
.form-field input[type="email"],
.form-field input[type="tel"],
.form-field input[type="url"],
.form-field input[type="date"],
.form-field select,
.form-field textarea {
    padding: 0.625rem 0.75rem;
    font-family: var(--font-sans);
    font-size: 0.875rem;
    background: var(--card);
    border: 1px solid var(--input);
    border-radius: var(--radius);
    color: var(--foreground);
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.form-field input:focus,
.form-field select:focus,
.form-field textarea:focus {
    outline: none;
    border-color: var(--ring);
    box-shadow: 0 0 0 2px color-mix(in oklch, var(--ring) 25%, transparent);
}

.form-field input::placeholder,
.form-field textarea::placeholder {
    color: var(--muted-foreground);
    opacity: 0.6;
}

.form-field textarea {
    resize: vertical;
    min-height: 100px;
}

/* Audio Recorder */
.audio-section {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

.audio-controls {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.record-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    background: var(--destructive);
    color: var(--destructive-foreground);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.2s ease;
}

.record-btn:hover {
    opacity: 0.9;
    transform: scale(1.05);
}

.record-btn.recording {
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

.recording-indicator {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-family: var(--font-mono);
    font-size: 0.875rem;
    color: var(--destructive);
}

.recording-indicator .dot {
    width: 8px;
    height: 8px;
    background: var(--destructive);
    border-radius: 50%;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

.audio-playback {
    width: 100%;
    margin-top: 0.5rem;
}

.audio-playback audio {
    width: 100%;
}

/* Detail Actions */
.detail-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}

/* Buttons */
.btn-primary,
.btn-secondary,
.btn-danger {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.875rem;
    font-weight: 500;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-primary {
    background: var(--primary);
    color: var(--primary-foreground);
}

.btn-primary:hover {
    opacity: 0.9;
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: var(--card);
    color: var(--foreground);
    border-color: var(--border);
}

.btn-secondary:hover {
    background: var(--muted);
}

.btn-danger {
    background: var(--destructive);
    color: var(--destructive-foreground);
}

.btn-danger:hover {
    opacity: 0.9;
}

.btn-small {
    padding: 0.375rem 0.75rem;
    font-size: 0.75rem;
}

/* Toast Notification */
.toast {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    padding: 0.75rem 1.25rem;
    background: var(--card);
    color: var(--foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    z-index: 1000;
    opacity: 1;
    transform: translateY(0);
    transition: all 0.3s ease;
}

.toast.hidden {
    opacity: 0;
    transform: translateY(1rem);
    pointer-events: none;
}

.toast.success {
    border-left: 4px solid var(--secondary);
}

.toast.error {
    border-left: 4px solid var(--destructive);
}

/* Empty State */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
    text-align: center;
    color: var(--muted-foreground);
}

.empty-state p {
    margin: 0.25rem 0;
}

.text-muted {
    font-size: 0.8125rem;
    opacity: 0.7;
}

/* Utility */
.hidden {
    display: none !important;
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .app-header {
        padding: 1rem;
    }

    .app-header h1 {
        font-size: 1.25rem;
    }

    .main-content {
        padding: 1rem;
        gap: 1rem;
    }

    .list-section {
        max-height: none;
    }

    .detail-section {
        position: static;
    }

    .contact-actions {
        flex-wrap: wrap;
    }

    .detail-actions {
        flex-direction: column;
    }

    .btn-primary,
    .btn-secondary,
    .btn-danger {
        width: 100%;
        justify-content: center;
    }
}
