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

:root {
    --primary-color: #0088cc;
    --secondary-color: #6c757d;
    --danger-color: #dc3545;
    --success-color: #28a745;
    /* AMOLED ультра черные цвета для максимальной экономии батареи */
    --bg-color: #000000;
    --bg-secondary: #020202;
    --text-color: #ffffff;
    --text-secondary: #888888;
    --border-color: #0a0a0a;
    --shadow: 0 2px 4px rgba(0,0,0,1);
    --shadow-lg: 0 4px 8px rgba(0,0,0,1);
}

/* AMOLED темная тема - применяется по умолчанию */
body,
body.dark-theme,
body.dark-theme .page,
body.dark-theme .form,
body.dark-theme .table-container,
body.dark-theme .modal-content,
body.dark-theme .health-container,
body.dark-theme .toast,
body .page,
body .form,
body .table-container,
body .modal-content,
body .health-container,
body .toast {
    --bg-color: #000000;
    --bg-secondary: #020202;
    --text-color: #ffffff;
    --text-secondary: #888888;
    --border-color: #0a0a0a;
    --shadow: 0 2px 4px rgba(0,0,0,1);
    --shadow-lg: 0 4px 8px rgba(0,0,0,1);
}

/* AMOLED темная тема - применяется везде */
body .header,
body.dark-theme .header {
    background: #000000;
    border-bottom: 1px solid var(--border-color);
}

body .nav,
body.dark-theme .nav {
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
}

body .table thead,
body.dark-theme .table thead {
    background: var(--bg-secondary);
}

body .table tbody tr:hover,
body.dark-theme .table tbody tr:hover {
    background: var(--bg-secondary);
}

body .input,
body .select,
body .textarea,
body.dark-theme .input,
body.dark-theme .select,
body.dark-theme .textarea {
    background: var(--bg-secondary);
    color: var(--text-color);
    border-color: var(--border-color);
}

body .input:focus,
body .select:focus,
body .textarea:focus,
body.dark-theme .input:focus,
body.dark-theme .select:focus,
body.dark-theme .textarea:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 136, 204, 0.2);
    outline: none;
}

body .email-view-header,
body .email-view-body,
body .email-view-attachments,
body .attachment-item,
body.dark-theme .email-view-header,
body.dark-theme .email-view-body,
body.dark-theme .email-view-attachments,
body.dark-theme .attachment-item {
    background: var(--bg-secondary);
}

body .email-view-attachment,
body.dark-theme .email-view-attachment {
    background: var(--bg-color);
}

body code,
body.dark-theme code {
    background: var(--bg-secondary);
    color: var(--primary-color);
    padding: 0.2rem 0.4rem;
    border-radius: 3px;
}

body .loader,
body.dark-theme .loader {
    background: rgba(0, 0, 0, 1);
}

body .modal,
body.dark-theme .modal {
    background: rgba(0, 0, 0, 1);
}

body .health-status.ok,
body.dark-theme .health-status.ok {
    color: var(--success-color);
}

body .health-status.error,
body.dark-theme .health-status.error {
    color: var(--danger-color);
}

body .email-view-header-label,
body.dark-theme .email-view-header-label {
    color: var(--text-secondary);
}

/* Улучшения для AMOLED темной темы */
body .btn-primary:hover,
body.dark-theme .btn-primary:hover {
    background: #0077b3;
    filter: brightness(1.1);
}

body .btn-secondary:hover,
body.dark-theme .btn-secondary:hover {
    background: #5a6268;
    filter: brightness(1.1);
}

body .btn-danger:hover,
body.dark-theme .btn-danger:hover {
    background: #c82333;
    filter: brightness(1.1);
}

/* Применяем тёмную тему по умолчанию */
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
    overflow-x: hidden;
}

/* Header */
.header {
    background: #000000;
    color: var(--text-color);
    padding: 1rem;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 100;
    border-bottom: 1px solid var(--border-color);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

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

/* Navigation */
.nav {
    display: flex;
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
    overflow-x: auto;
    position: sticky;
    top: 60px;
    z-index: 99;
}

.nav-btn {
    flex: 1;
    padding: 0.75rem 0.5rem;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    border-bottom: 2px solid transparent;
}

.nav-btn:hover {
    background: var(--bg-secondary);
    color: var(--text-color);
}

.nav-btn.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
    font-weight: 600;
    background: var(--bg-secondary);
}

/* Main Content */
.main {
    padding: 1rem;
    max-width: 1200px;
    margin: 0 auto;
}

.page {
    display: none;
}

.page.active {
    display: block;
}

.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.page-header h2 {
    font-size: 1.5rem;
    font-weight: 600;
}

/* Buttons */
.btn-primary, .btn-secondary, .btn-danger, .btn-icon, .btn-small {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

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

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

.btn-primary:hover {
    background: #0077b3;
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
}

.btn-secondary:hover {
    background: #5a6268;
}

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

.btn-danger:hover {
    background: #c82333;
}

.btn-icon {
    background: transparent;
    padding: 0.25rem 0.5rem;
    font-size: 1.25rem;
    color: var(--text-color);
}

.btn-icon:hover {
    background: var(--bg-secondary);
    color: var(--primary-color);
}

/* Actions Bar */
.actions-bar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

/* Tables */
.table-container {
    background: var(--bg-color);
    border-radius: 8px;
    box-shadow: var(--shadow);
    overflow: hidden;
    overflow-x: auto;
}

.table {
    width: 100%;
    border-collapse: collapse;
}

.table thead {
    background: var(--bg-secondary);
}

.table th {
    padding: 0.75rem;
    text-align: left;
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--text-secondary);
    border-bottom: 2px solid var(--border-color);
}

.table td {
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.table tbody tr:hover {
    background: var(--bg-secondary);
}

.table tbody tr:last-child td {
    border-bottom: none;
}

.table .loading {
    text-align: center;
    color: var(--text-secondary);
    padding: 2rem;
}

/* Forms */
.form {
    background: var(--bg-color);
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.875rem;
}

.input, .select, .textarea {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    font-size: 0.875rem;
    font-family: inherit;
    background: var(--bg-secondary);
    color: var(--text-color);
}

.input:focus, .select:focus, .textarea:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(0, 136, 204, 0.2);
}

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

.form-actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 1.5rem;
}

/* Modals */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-color);
    border-radius: 8px;
    box-shadow: var(--shadow-lg);
    max-width: 500px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-large {
    max-width: 800px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 1.25rem;
    font-weight: 600;
}

.modal-body {
    padding: 1rem;
}

.modal-close {
    cursor: pointer;
}

/* Health Container */
.health-container {
    background: var(--bg-color);
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: var(--shadow);
}

.health-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

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

.health-status {
    font-weight: 600;
}

.health-status.ok {
    color: var(--success-color);
}

.health-status.error {
    color: var(--danger-color);
}

/* Toast Notifications */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.toast {
    background: var(--bg-color);
    padding: 1rem;
    border-radius: 4px;
    box-shadow: var(--shadow-lg);
    min-width: 300px;
    animation: slideIn 0.3s ease;
}

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

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

.toast.info {
    border-left: 4px solid var(--primary-color);
}

@keyframes slideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }
    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* Loader */
.loader {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 3000;
    align-items: center;
    justify-content: center;
}

.loader.active {
    display: flex;
}

.loader-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Responsive */
@media (max-width: 768px) {
    .main {
        padding: 0.5rem;
    }
    
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
    
    .page-header h2 {
        font-size: 1.25rem;
    }

    .actions-bar {
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .actions-bar button {
        width: 100%;
    }

    .table {
        font-size: 0.875rem;
    }

    .table th,
    .table td {
        padding: 0.5rem;
        font-size: 0.8125rem;
    }
    
    .form {
        padding: 1rem;
    }
    
    .btn-primary, .btn-secondary, .btn-danger {
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    .form-actions {
        flex-direction: column;
    }
    
    .modal-content {
        margin: 0.5rem;
        max-height: 95vh;
    }
    
    .toast {
        min-width: auto;
        width: calc(100% - 2rem);
        max-width: 400px;
    }
    
    .toast-container {
        left: 1rem;
        right: 1rem;
        top: auto;
        bottom: 1rem;
    }
    
    .nav {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    
    .nav::-webkit-scrollbar {
        display: none;
    }
    
    .nav-btn {
        min-width: fit-content;
        padding: 0.625rem 0.75rem;
        font-size: 0.8125rem;
    }
    
    .header h1 {
        font-size: 1.125rem;
    }
    
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .input[type="file"] {
        font-size: 16px; /* Предотвращает масштабирование на iOS */
        padding: 0.75rem;
    }
    
    .input[type="file"]::-webkit-file-upload-button {
        padding: 0.625rem 1rem;
        font-size: 0.875rem;
    }
    
    .attachment-item {
        flex-wrap: wrap;
    }
    
    .attachment-item button {
        flex-shrink: 0;
    }
}

/* Email View */
.email-view {
    line-height: 1.8;
}

.email-view-header {
    background: var(--bg-secondary);
    padding: 1rem;
    border-radius: 4px;
    margin-bottom: 1rem;
}

.email-view-header-item {
    margin-bottom: 0.5rem;
}

.email-view-header-item:last-child {
    margin-bottom: 0;
}

.email-view-header-label {
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.email-view-body {
    margin-top: 1rem;
    padding: 1rem;
    background: var(--bg-secondary);
    border-radius: 4px;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.email-view-attachments {
    margin-top: 1rem;
    padding: 1rem;
    background: var(--bg-secondary);
    border-radius: 4px;
}

.email-view-attachment {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    margin-bottom: 0.5rem;
    background: var(--bg-color);
    border-radius: 4px;
}

.email-view-attachment:last-child {
    margin-bottom: 0;
}

/* Attachments List */
#attachmentsList {
    margin-top: 0.5rem;
}

.attachment-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem;
    background: var(--bg-secondary);
    border-radius: 4px;
    margin-bottom: 0.5rem;
    gap: 0.5rem;
}

.attachment-item:last-child {
    margin-bottom: 0;
}

.attachment-item span {
    flex: 1;
    min-width: 0;
    word-break: break-word;
    font-size: 0.875rem;
}

.attachment-item small {
    color: var(--text-secondary);
    font-size: 0.75rem;
}

/* File input styling for mobile */
.input[type="file"] {
    padding: 0.625rem;
    cursor: pointer;
    font-size: 0.875rem;
}

.input[type="file"]::-webkit-file-upload-button {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 0.5rem;
    font-size: 0.875rem;
}

.input[type="file"]::-webkit-file-upload-button:hover {
    background: #0077b3;
}

/* Logs Container */
.logs-container {
    background: var(--bg-color);
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: var(--shadow);
    max-height: calc(100vh - 300px);
    overflow-y: auto;
}

.logs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.logs-file-info {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.logs-file-info code {
    background: var(--bg-secondary);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.875rem;
    margin-left: 0.5rem;
}

.logs-count {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

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

.logs-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.log-entry {
    display: grid;
    grid-template-columns: 160px 80px 120px 1fr;
    gap: 0.75rem;
    padding: 0.75rem;
    background: #020202;
    border-radius: 4px;
    border-left: 3px solid var(--border-color);
    font-size: 0.8125rem;
    transition: all 0.2s;
}

.log-entry:hover {
    background: #050505;
    box-shadow: var(--shadow);
}

.log-entry.log-debug {
    border-left-color: var(--text-secondary);
    opacity: 0.7;
}

.log-entry.log-info {
    border-left-color: var(--primary-color);
}

.log-entry.log-warning {
    border-left-color: #ffc107;
}

.log-entry.log-error {
    border-left-color: var(--danger-color);
    background: rgba(220, 53, 69, 0.1);
}

.log-entry.log-critical {
    border-left-color: #dc3545;
    background: rgba(220, 53, 69, 0.2);
    font-weight: 600;
}

.log-timestamp {
    color: var(--text-secondary);
    font-size: 0.75rem;
    white-space: nowrap;
}

.log-level {
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
}

.log-module {
    color: var(--text-secondary);
    font-size: 0.75rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.log-message {
    color: var(--text-color);
    word-wrap: break-word;
    font-family: 'Courier New', monospace;
    font-size: 0.8125rem;
    line-height: 1.4;
}

/* AMOLED темная тема для логов */
body.dark-theme .log-entry {
    background: #0a0a0a;
    border-left-color: #1a1a1a;
}

body.dark-theme .log-entry:hover {
    background: #151515;
}

body.dark-theme .log-entry.log-error {
    background: rgba(220, 53, 69, 0.1);
}

body.dark-theme .log-entry.log-critical {
    background: rgba(220, 53, 69, 0.15);
}

/* Responsive для логов */
@media (max-width: 768px) {
    .log-entry {
        grid-template-columns: 1fr;
        gap: 0.25rem;
    }
    
    .log-timestamp,
    .log-level,
    .log-module {
        font-size: 0.7rem;
    }
    
    .page-header-actions {
        flex-direction: column;
        align-items: stretch;
    }
    
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
}

/* Улучшения для мобильных устройств */
@media (max-width: 480px) {
    .main {
        padding: 0.5rem;
    }
    
    .nav-btn {
        font-size: 0.75rem;
        padding: 0.5rem 0.5rem;
        white-space: nowrap;
    }
    
    .page-header h2 {
        font-size: 1.125rem;
    }
    
    .header h1 {
        font-size: 1rem;
    }
    
    .table {
        font-size: 0.75rem;
    }
    
    .table th,
    .table td {
        padding: 0.375rem;
        font-size: 0.75rem;
    }
    
    .form-group {
        margin-bottom: 0.75rem;
    }
    
    .input, .select, .textarea {
        font-size: 16px; /* Предотвращает масштабирование на iOS */
        padding: 0.625rem;
    }
    
    .textarea {
        min-height: 120px;
    }
    
    .btn-primary, .btn-secondary, .btn-danger, .btn-small {
        padding: 0.625rem 1rem;
        font-size: 0.875rem;
    }
    
    .modal-content {
        margin: 0.25rem;
        border-radius: 8px;
    }
    
    .modal-header {
        padding: 0.75rem;
    }
    
    .modal-body {
        padding: 0.75rem;
    }
    
    .email-view-header,
    .email-view-body,
    .email-view-attachments {
        padding: 0.75rem;
    }
    
    .attachment-item {
        padding: 0.625rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .attachment-item span {
        flex: 1;
        min-width: 0;
        word-break: break-word;
    }
    
    .email-view-attachment {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
    
    .email-view-attachment button {
        width: 100%;
    }
    
    .page-header-actions {
        flex-direction: column;
        width: 100%;
    }
    
    .page-header-actions .select,
    .page-header-actions .input {
        width: 100%;
    }
    
    .logs-container {
        padding: 1rem;
        max-height: calc(100vh - 250px);
    }
    
    .log-entry {
        padding: 0.5rem;
        gap: 0.5rem;
    }
    
    .log-timestamp,
    .log-level,
    .log-module {
        font-size: 0.6875rem;
    }
    
    .log-message {
        font-size: 0.75rem;
    }
    
    .health-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
    
    .health-item span {
        width: 100%;
    }
}

/* Logs Container */
.logs-container {
    background: var(--bg-color);
    padding: 1.5rem;
    border-radius: 8px;
    box-shadow: var(--shadow);
    max-height: calc(100vh - 300px);
    overflow-y: auto;
}

.logs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--border-color);
}

.logs-file-info {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

.logs-file-info code {
    background: var(--bg-secondary);
    padding: 0.25rem 0.5rem;
    border-radius: 4px;
    font-size: 0.875rem;
    margin-left: 0.5rem;
}

.logs-count {
    font-size: 0.875rem;
    color: var(--text-secondary);
}

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

.logs-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.log-entry {
    display: grid;
    grid-template-columns: 160px 80px 120px 1fr;
    gap: 0.75rem;
    padding: 0.75rem;
    background: #020202;
    border-radius: 4px;
    border-left: 3px solid var(--border-color);
    font-size: 0.8125rem;
    transition: all 0.2s;
}

.log-entry:hover {
    background: #050505;
    box-shadow: var(--shadow);
}

.log-entry.log-debug {
    border-left-color: var(--text-secondary);
    opacity: 0.7;
}

.log-entry.log-info {
    border-left-color: var(--primary-color);
}

.log-entry.log-warning {
    border-left-color: #ffc107;
}

.log-entry.log-error {
    border-left-color: var(--danger-color);
    background: rgba(220, 53, 69, 0.1);
}

.log-entry.log-critical {
    border-left-color: #dc3545;
    background: rgba(220, 53, 69, 0.2);
    font-weight: 600;
}

.log-timestamp {
    color: var(--text-secondary);
    font-size: 0.75rem;
    white-space: nowrap;
}

.log-level {
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
}

.log-module {
    color: var(--text-secondary);
    font-size: 0.75rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.log-message {
    color: var(--text-color);
    word-wrap: break-word;
    font-family: 'Courier New', monospace;
    font-size: 0.8125rem;
    line-height: 1.4;
}

/* AMOLED темная тема для логов */
body .log-entry,
body.dark-theme .log-entry {
    background: #020202;
    border-left-color: #0a0a0a;
}

body .log-entry:hover,
body.dark-theme .log-entry:hover {
    background: #050505;
}

body .log-entry.log-error,
body.dark-theme .log-entry.log-error {
    background: rgba(220, 53, 69, 0.15);
}

body .log-entry.log-critical,
body.dark-theme .log-entry.log-critical {
    background: rgba(220, 53, 69, 0.25);
}

/* Responsive для логов */
@media (max-width: 768px) {
    .log-entry {
        grid-template-columns: 1fr;
        gap: 0.25rem;
    }
    
    .log-timestamp,
    .log-level,
    .log-module {
        font-size: 0.7rem;
    }
    
    .page-header-actions {
        flex-direction: column;
        align-items: stretch;
    }
    
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }
}
