:root {
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #22c55e;
  --warning: #f59e0b;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-300: #d1d5db;
  --gray-400: #9ca3af;
  --gray-500: #6b7280;
  --gray-600: #4b5563;
  --gray-700: #374151;
  --gray-800: #1f2937;
  --gray-900: #111827;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,.1), 0 4px 6px -2px rgba(0,0,0,.05);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gray-50);
  color: var(--gray-800);
  line-height: 1.6;
  min-height: 100vh;
}

/* Login page */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }
.login-card { background: #fff; padding: 2.5rem; border-radius: 12px; box-shadow: var(--shadow-lg); width: 100%; max-width: 400px; text-align: center; }
.login-logo { color: var(--primary); margin-bottom: .5rem; }
.login-card h1 { font-size: 1.5rem; margin-bottom: .25rem; }
.login-subtitle { color: var(--gray-500); margin-bottom: 1.5rem; font-size: .9rem; }

/* Forms */
.form-group { margin-bottom: 1rem; text-align: left; }
.form-group label { display: block; font-size: .875rem; font-weight: 500; margin-bottom: .375rem; color: var(--gray-700); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: .625rem .75rem; border: 1px solid var(--gray-300); border-radius: var(--radius);
  font-size: .875rem; transition: border-color .15s;
}
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79,70,229,.1); }

.error-msg { background: #fef2f2; color: var(--danger); padding: .75rem; border-radius: var(--radius); margin-bottom: 1rem; font-size: .875rem; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: .5rem; padding: .625rem 1.25rem;
  border: none; border-radius: var(--radius); font-size: .875rem; font-weight: 500;
  cursor: pointer; transition: all .15s; text-decoration: none;
}
.btn:disabled { opacity: .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(--gray-200); color: var(--gray-700); }
.btn-secondary:hover:not(:disabled) { background: var(--gray-300); }
.btn-sm { padding: .375rem .75rem; font-size: .8125rem; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon { padding: .5rem; }

/* Layout */
.app-layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; background: var(--gray-900); color: #fff; padding: 1.5rem 0;
  display: flex; flex-direction: column; position: fixed; top: 0; bottom: 0; left: 0; z-index: 50;
}
.sidebar-brand { padding: 0 1.25rem 1.5rem; font-size: 1.125rem; font-weight: 700; border-bottom: 1px solid var(--gray-700); margin-bottom: 1rem; display: flex; align-items: center; gap: .5rem; }
.sidebar-brand svg { flex-shrink: 0; }
.sidebar-brand .sidebar-logo { height: 32px; width: auto; display: block; flex-shrink: 0; }
.sidebar-brand .sidebar-brand-name { white-space: nowrap; }
.sidebar-nav { flex: 1; }
.sidebar-nav a {
  display: flex; align-items: center; gap: .75rem; padding: .75rem 1.25rem; color: var(--gray-400);
  text-decoration: none; font-size: .875rem; transition: all .15s;
}
.sidebar-nav a:hover, .sidebar-nav a.active { color: #fff; background: rgba(255,255,255,.08); }
.sidebar-nav a.active { border-right: 3px solid var(--primary); }
.sidebar-footer { padding: 1rem 1.25rem; border-top: 1px solid var(--gray-700); }

.main-content { flex: 1; margin-left: 240px; padding: 2rem; }
.page-header { margin-bottom: 1.5rem; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 1rem; }
.page-header h1 { font-size: 1.5rem; }

/* Cards */
.card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.card-body { padding: 1.25rem; }
.card-header { padding: 1rem 1.25rem; border-bottom: 1px solid var(--gray-200); font-weight: 600; display: flex; align-items: center; justify-content: space-between; }

/* Stats grid */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.25rem; display: flex; align-items: center; gap: 1rem; }
.stat-icon { width: 48px; height: 48px; border-radius: 10px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.stat-icon.media { background: #ede9fe; color: #7c3aed; }
.stat-icon.playlists { background: #dbeafe; color: #2563eb; }
.stat-icon.screens { background: #d1fae5; color: #059669; }
.stat-icon.online { background: #fef3c7; color: #d97706; }
.stat-value { font-size: 1.75rem; font-weight: 700; line-height: 1; }
.stat-label { font-size: .8125rem; color: var(--gray-500); margin-top: .125rem; }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .75rem 1rem; text-align: left; font-size: .875rem; }
th { background: var(--gray-50); color: var(--gray-600); font-weight: 600; text-transform: uppercase; font-size: .75rem; letter-spacing: .05em; }
td { border-top: 1px solid var(--gray-200); }
tr:hover td { background: var(--gray-50); }

/* Status indicator */
.status { display: inline-flex; align-items: center; gap: .375rem; font-size: .8125rem; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.status-dot.online { background: var(--success); box-shadow: 0 0 6px rgba(34,197,94,.4); }
.status-dot.offline { background: var(--danger); }
.status-dot.never { background: var(--gray-400); }

/* Media grid */
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.media-card { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; transition: transform .15s; }
.media-card:hover { transform: translateY(-2px); }
.media-thumb { width: 100%; height: 160px; background: var(--gray-100); display: flex; align-items: center; justify-content: center; overflow: hidden; }
.media-thumb img { width: 100%; height: 100%; object-fit: cover; }
.media-thumb .video-icon { color: var(--gray-400); }
.media-info { padding: .75rem; }
.media-name { font-size: .8125rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.media-meta { font-size: .75rem; color: var(--gray-500); margin-top: .25rem; display: flex; justify-content: space-between; }

/* Upload zone */
.upload-zone {
  border: 2px dashed var(--gray-300); border-radius: var(--radius); padding: 2rem; text-align: center;
  cursor: pointer; transition: all .15s; margin-bottom: 1.5rem;
}
.upload-zone:hover, .upload-zone.dragover { border-color: var(--primary); background: rgba(79,70,229,.04); }
.upload-zone p { color: var(--gray-500); margin-top: .5rem; }
.upload-zone input { display: none; }
.progress-bar { height: 6px; background: var(--gray-200); border-radius: 3px; overflow: hidden; margin-top: .75rem; }
.progress-bar-fill { height: 100%; background: var(--primary); border-radius: 3px; transition: width .2s; width: 0; }

/* Playlist editor */
.playlist-items { list-style: none; min-height: 60px; }
.playlist-item {
  display: flex; align-items: center; gap: .75rem; padding: .75rem; margin-bottom: .5rem;
  background: var(--gray-50); border: 1px solid var(--gray-200); border-radius: var(--radius);
  cursor: grab; transition: box-shadow .15s;
}
.playlist-item:active { cursor: grabbing; }
.playlist-item.dragging { opacity: .5; box-shadow: var(--shadow-lg); }
.playlist-item .drag-handle { color: var(--gray-400); cursor: grab; }
.playlist-item .item-thumb { width: 48px; height: 36px; border-radius: 4px; object-fit: cover; background: var(--gray-200); flex-shrink: 0; }
.playlist-item .item-name { flex: 1; font-size: .875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.playlist-item .item-duration { width: 70px; }
.playlist-item .item-duration input { width: 100%; padding: .25rem .5rem; border: 1px solid var(--gray-300); border-radius: 4px; font-size: .8125rem; text-align: center; }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 100; opacity: 0; visibility: hidden; transition: all .2s; }
.modal-overlay.active { opacity: 1; visibility: visible; }
.modal { background: #fff; border-radius: 12px; box-shadow: var(--shadow-lg); width: 90%; max-width: 600px; max-height: 80vh; display: flex; flex-direction: column; }
.modal-header { padding: 1.25rem; border-bottom: 1px solid var(--gray-200); display: flex; align-items: center; justify-content: space-between; }
.modal-header h2 { font-size: 1.125rem; }
.modal-body { padding: 1.25rem; overflow-y: auto; flex: 1; }
.modal-footer { padding: 1rem 1.25rem; border-top: 1px solid var(--gray-200); display: flex; gap: .5rem; justify-content: flex-end; }
.close-btn { background: none; border: none; font-size: 1.5rem; cursor: pointer; color: var(--gray-400); line-height: 1; }

/* Media select grid in modal */
.media-select-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .75rem; }
.media-select-item { border: 2px solid var(--gray-200); border-radius: var(--radius); overflow: hidden; cursor: pointer; transition: border-color .15s; position: relative; display: block; }
.media-select-item:hover:not(.media-select-item-disabled) { border-color: var(--primary); }
.media-select-item.media-select-item-disabled { opacity: .65; cursor: default; }
.media-select-item .media-select-checkbox { position: absolute; top: .35rem; left: .35rem; z-index: 1; width: 1.1rem; height: 1.1rem; cursor: pointer; }
.media-select-item img { width: 100%; height: 80px; object-fit: cover; }
.media-select-item .name { padding: .375rem; font-size: .75rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Toast */
.toast { position: fixed; bottom: 1.5rem; right: 1.5rem; padding: .75rem 1.25rem; border-radius: var(--radius); color: #fff; font-size: .875rem; z-index: 200; transform: translateY(100%); opacity: 0; transition: all .3s; }
.toast.show { transform: translateY(0); opacity: 1; }
.toast-info { background: var(--primary); }
.toast-success { background: var(--success); }
.toast-error { background: var(--danger); }

/* Mobile header (injected by admin-mobile.js) */
.mobile-header { display: none; }

/* Mobile: hamburger toggle (injected by admin-mobile.js) */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 60;
  width: 48px;
  height: 48px;
  align-items: center;
  justify-content: center;
  background: var(--gray-900);
  border: none;
  color: #fff;
  cursor: pointer;
  padding: 0;
  border-radius: 0;
  flex-direction: column;
  gap: 5px;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-toggle-bar {
  width: 20px;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  transition: transform .2s, opacity .2s;
}
body.sidebar-open .sidebar-toggle-bar:nth-child(1) { transform: translateY(7px) rotate(45deg); }
body.sidebar-open .sidebar-toggle-bar:nth-child(2) { opacity: 0; }
body.sidebar-open .sidebar-toggle-bar:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 55;
  -webkit-tap-highlight-color: transparent;
}
body.sidebar-open .sidebar-backdrop { display: block; }

/* Responsive: mobile first breakpoint 768px */
@media (max-width: 768px) {
  .sidebar-toggle { display: flex; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 85vw);
    z-index: 56;
    transform: translateX(-100%);
    transition: transform .25s ease-out;
    box-shadow: 4px 0 20px rgba(0,0,0,.2);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open { overflow: hidden; }

  .mobile-header {
    display: flex;
    align-items: center;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--gray-900);
    z-index: 60;
    padding: 0;
    box-shadow: 0 1px 3px rgba(0,0,0,.2);
  }
  .mobile-header .sidebar-toggle { position: static; flex-shrink: 0; }
  .mobile-header .mobile-brand {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: 0 .75rem;
    min-width: 0;
  }
  .mobile-header .mobile-brand img { height: 28px; width: auto; display: block; }
  .mobile-header .mobile-brand span { color: #fff; font-weight: 700; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

  .main-content { margin-left: 0; padding: 1rem; padding-top: 72px; }
  .app-layout { flex-direction: column; }

  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: .75rem;
    margin-bottom: 1rem;
    padding-top: .25rem;
  }
  .page-header h1 { font-size: 1.25rem; }
  .page-header .btn { min-height: 44px; justify-content: center; }

  .stats-grid { grid-template-columns: 1fr 1fr; gap: .75rem; }
  .stat-card { padding: 1rem; }
  .stat-icon { width: 42px; height: 42px; }
  .stat-value { font-size: 1.5rem; }

  .card-body { padding: 1rem; }
  .card-header { padding: .75rem 1rem; flex-wrap: wrap; gap: .5rem; }

  .table-wrap {
    margin: 0 -1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-wrap table { min-width: 600px; }
  th, td { padding: .5rem .75rem; font-size: .8125rem; }

  /* Экраны: карточный вид на мобиле — ровные карточки и сетка кнопок */
  .table-wrap .screens-table { min-width: 0; }
  .table-wrap:has(.screens-table) { margin-left: 0; margin-right: 0; }
  .screens-table thead { display: none; }
  .screens-table tbody { display: flex; flex-direction: column; gap: .75rem; }
  .screens-table tr {
    display: block;
    padding: 1rem;
    border-radius: var(--radius);
    background: #fff;
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow);
  }
  .screens-table td {
    display: block;
    padding: .5rem 0;
    border: none;
    font-size: .8125rem;
  }
  .screens-table td:first-child { padding-top: 0; }
  .screens-table td:last-child { padding-bottom: 0; }
  .screens-table td[colspan]::before { display: none; }
  .screens-table td.empty-state { padding: 1.5rem; text-align: center; }
  .screens-table td::before {
    content: attr(data-label);
    display: block;
    font-size: .6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--gray-500);
    margin-bottom: .25rem;
  }
  .screens-table td.screens-actions {
    padding-top: .75rem;
    margin-top: .5rem;
    border-top: 1px solid var(--gray-200);
  }
  .screens-table td.screens-actions::before { margin-bottom: .5rem; }
  .screens-table .screens-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: 1fr;
    gap: .5rem;
  }
  .screens-table .screens-actions .btn,
  .screens-table .screens-actions a.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-width: 0;
    margin: 0;
    box-sizing: border-box;
  }
  .screens-table .screens-actions .btn:nth-child(1) { grid-column: 1; grid-row: 1; }
  .screens-table .screens-actions .btn:nth-child(2) { grid-column: 2; grid-row: 1; }
  .screens-table .screens-actions .btn:nth-child(3) { grid-column: 1; grid-row: 2; }
  .screens-table .screens-actions .btn:nth-child(4) { grid-column: 2; grid-row: 2; }
  .screens-table .screens-actions .btn:nth-child(5) { grid-column: 1 / -1; grid-row: 3; }

  .media-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .75rem; }
  .media-thumb { height: 120px; }
  .media-info { padding: .5rem; }
  .media-name { font-size: .75rem; }

  .upload-zone { padding: 1.25rem; margin-bottom: 1rem; min-height: 120px; }
  .upload-zone p { font-size: .875rem; }

  .playlist-item { padding: .5rem .75rem; gap: .5rem; }
  .playlist-item .item-thumb { width: 40px; height: 30px; }
  .playlist-item .item-duration { width: 60px; }

  .modal { width: calc(100% - 2rem); max-width: none; max-height: 90vh; margin: 1rem; }
  .modal-header, .modal-body, .modal-footer { padding: 1rem; }
  .media-select-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
  .media-select-item .name { font-size: .7rem; }

  .btn { min-height: 44px; padding: .75rem 1rem; }
  .btn-icon { min-width: 44px; min-height: 44px; }
  .sidebar-nav a { min-height: 48px; padding: .75rem 1rem; }
  .sidebar-footer .btn { min-height: 44px; }

  .toast { left: 1rem; right: 1rem; bottom: 1rem; text-align: center; }

  .login-page { padding: 1rem; align-items: flex-start; padding-top: 2rem; }
  .login-card { padding: 1.5rem; margin: 0; max-width: none; }
  .login-card h1 { font-size: 1.25rem; }
  .form-group input { min-height: 48px; font-size: 16px; }
  .login-page .btn { min-height: 48px; }
}

@media (max-width: 480px) {
  .stats-grid { grid-template-columns: 1fr; }
  .main-content { padding: .75rem; padding-top: 72px; }
  .sidebar { width: min(260px, 92vw); }
}

/* Empty state */
.empty-state { text-align: center; padding: 3rem 1.5rem; color: var(--gray-500); }
.empty-state svg { margin-bottom: 1rem; color: var(--gray-300); }
.empty-state h3 { color: var(--gray-700); margin-bottom: .5rem; }

/* Spinner */
.spinner { width: 28px; height: 28px; border: 3px solid var(--gray-200); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
