:root {
  --bg: #f4f6f9;
  --card: #fff;
  --surface: #f8fafc;
  --text: #1a1d26;
  --muted: #5c6370;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --border: #e2e8f0;
  --input-bg: #fff;
  --btn-bg: #e2e8f0;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  --alert-success-bg: #dcfce7;
  --alert-success-text: #166534;
  --alert-error-bg: #fee2e2;
  --alert-error-text: #991b1b;
  --badge-ok-bg: #dcfce7;
  --badge-ok-text: #166534;
  --badge-warn-bg: #fef3c7;
  --badge-warn-text: #92400e;
  --bar-muted: #64748b;
  --radius: 8px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --touch-min: 44px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  padding-bottom: var(--safe-bottom);
}

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem max(1rem, var(--safe-left)) calc(2rem + var(--safe-bottom)) max(1rem, var(--safe-right));
}

.header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.5rem;
  padding-top: var(--safe-top);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem max(1rem, var(--safe-left)) 0.75rem max(1rem, var(--safe-right));
  flex-wrap: wrap;
}

.header-nav {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.05rem;
  line-height: 1.25;
  text-decoration: none;
}

.logo-title {
  font-weight: 700;
  color: var(--primary);
  text-decoration: none;
  font-size: 1.05rem;
  white-space: nowrap;
}

.logo-by {
  font-weight: 400;
  font-size: 0.65rem;
  color: var(--muted);
  text-decoration: none;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.logo-by:hover {
  color: var(--primary);
}

.user {
  color: var(--muted);
  font-size: 0.85rem;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

h1 {
  font-size: clamp(1.25rem, 4vw, 1.5rem);
  margin: 0 0 1rem;
  word-break: break-word;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}

.card h2 {
  font-size: 1rem;
  margin: 0 0 1rem;
  color: var(--muted);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.45rem 0.9rem;
  min-height: var(--touch-min);
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.875rem;
  cursor: pointer;
  text-decoration: none;
  background: var(--btn-bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-hover); }

.btn-outline {
  background: transparent;
  border-color: var(--border);
}

.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }

.btn-sm {
  padding: 0.35rem 0.65rem;
  font-size: 0.8rem;
  min-height: 38px;
}

.btn-touch { min-height: var(--touch-min); }

.btn-block { width: 100%; }

label {
  display: block;
  margin-bottom: 0.75rem;
  font-size: 0.875rem;
  font-weight: 500;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="file"] {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.65rem 0.75rem;
  min-height: var(--touch-min);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  background: var(--input-bg);
  color: var(--text);
}

input[type="file"] {
  padding: 0.5rem;
  min-height: auto;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem 1rem;
}

.form-grid .full { grid-column: 1 / -1; }

.checkbox {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  font-weight: normal;
}

.checkbox input {
  width: 1.25rem;
  height: 1.25rem;
  margin: 0.15rem 0 0;
  flex-shrink: 0;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.table th,
.table td {
  text-align: left;
  padding: 0.6rem 0.5rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.table code {
  font-size: 0.75rem;
  word-break: break-all;
}

.url-input {
  width: 100%;
  max-width: 100%;
  font-size: 0.8rem;
  display: block;
  margin-bottom: 0.5rem;
}

.url-row {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: flex-start;
}

.actions .btn { margin: 0; }

.inline-form { display: inline; }

.alert {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.875rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.alert-success { background: var(--alert-success-bg); color: var(--alert-success-text); }
.alert-error { background: var(--alert-error-bg); color: var(--alert-error-text); }

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0.5rem 0 0;
}

.empty { color: var(--muted); }

.breadcrumb {
  font-size: 0.875rem;
  margin-bottom: 1rem;
  color: var(--muted);
  word-break: break-word;
}

.breadcrumb a { color: var(--primary); }

.link { color: var(--primary); }

.login-page {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(1rem, var(--safe-top)) max(1rem, var(--safe-right)) max(1rem, var(--safe-bottom)) max(1rem, var(--safe-left));
}

.login-card {
  background: var(--card);
  padding: 1.5rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow);
}

.login-brand {
  text-align: center;
  margin-bottom: 0.5rem;
}

.login-brand-title {
  margin: 0;
  font-size: 1.35rem;
  color: var(--primary);
}

.login-brand-by {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.68rem;
  color: var(--muted);
  text-decoration: none;
  letter-spacing: 0.02em;
}

.login-brand-by:hover {
  color: var(--primary);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  text-align: center;
}

.subtitle {
  text-align: center;
  color: var(--muted);
  font-size: 0.875rem;
  margin: 0 0 1.5rem;
}

.action-details summary {
  list-style: none;
  cursor: pointer;
}

.action-details summary::-webkit-details-marker { display: none; }

.inline-details { display: inline-block; vertical-align: top; }

.delete-panel,
.action-panel {
  padding: 0.75rem;
  background: var(--bg);
  border-radius: 8px;
  margin-top: 0.5rem;
  max-width: 100%;
}

.delete-panel .checkbox { font-weight: normal; margin: 0.5rem 0; }

.mini-form { margin-bottom: 0.75rem; }

.mini-form input[type="text"] { margin-bottom: 0.35rem; }

.mini-form .btn { width: 100%; }

.small { font-size: 0.7rem; }

.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 600;
}

.badge-ok { background: var(--badge-ok-bg); color: var(--badge-ok-text); }
.badge-warn { background: var(--badge-warn-bg); color: var(--badge-warn-text); }

.hint-list {
  font-size: 0.85rem;
  color: var(--muted);
  margin: 0 0 1rem;
  padding-left: 1.25rem;
}

.hint-list li { margin-bottom: 0.35rem; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

.stat-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
}

.stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

.stat-value {
  display: block;
  font-size: 1.1rem;
  font-weight: 600;
  word-break: break-word;
}

.bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  margin-top: 0.5rem;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: var(--primary);
  border-radius: 3px;
  max-width: 100%;
}

.geo-map {
  height: 420px;
  width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin-top: 1rem;
  z-index: 0;
}

.geo-map .leaflet-popup-content {
  font-size: 0.85rem;
  line-height: 1.4;
}

.period-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0 1rem;
}

.period-tab {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.4rem 0.85rem;
  min-height: 38px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
  text-decoration: none;
}

.period-tab:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.period-tab--active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  font-weight: 600;
}

.menu-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 1rem;
  margin: 1rem 0;
}

.menu-stats-panel h3 {
  font-size: 0.9rem;
  margin: 0 0 0.75rem;
  color: var(--muted);
}

.stats-bar-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.stats-bar-list li + li {
  margin-top: 0.75rem;
}

.stats-bar-row {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
  font-size: 0.85rem;
}

.stats-bar-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats-bar-value {
  color: var(--muted);
  flex-shrink: 0;
}

.bar-fill--muted {
  background: var(--bar-muted);
}

.timeline-chart {
  display: flex;
  align-items: flex-end;
  gap: 0.25rem;
  min-height: 150px;
  padding: 0.5rem 0;
  overflow-x: auto;
  margin-bottom: 1rem;
}

.timeline-bar {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  min-width: 28px;
  flex: 1 1 28px;
}

.timeline-bar-fill {
  width: 100%;
  max-width: 36px;
  background: var(--primary);
  border-radius: 4px 4px 0 0;
  min-height: 4px;
}

.timeline-bar-label {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 0.35rem;
  white-space: nowrap;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #232830;
    --card: #2b313a;
    --surface: #323843;
    --text: #e6eaf0;
    --muted: #a3adba;
    --primary: #5b9aff;
    --primary-hover: #7db0ff;
    --danger: #ff8f87;
    --border: #3f4653;
    --input-bg: #1e2229;
    --btn-bg: #3a4150;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
    --alert-success-bg: #1e3a2f;
    --alert-success-text: #8fd9a8;
    --alert-error-bg: #3d2528;
    --alert-error-text: #ffb4ab;
    --badge-ok-bg: #1e3a2f;
    --badge-ok-text: #8fd9a8;
    --badge-warn-bg: #3d3420;
    --badge-warn-text: #f0d080;
    --bar-muted: #7d8a9a;
  }

  .btn-outline:hover {
    background: var(--surface);
    border-color: var(--primary);
    color: var(--primary);
  }

  .geo-map .leaflet-popup-content-wrapper,
  .geo-map .leaflet-popup-tip {
    background: var(--card);
    color: var(--text);
  }
}

@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr; }
  .geo-map { height: 300px; }
}

.ssl-spinner {
  font-size: 1rem;
  padding: 1rem 0;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  50% { opacity: 0.5; }
}

.upload-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.upload-form .btn { width: 100%; }

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

.page-actions .btn { flex: 1 1 auto; min-width: min(100%, 140px); }

.info-list {
  margin: 0;
  display: grid;
  gap: 0.5rem 1rem;
  grid-template-columns: minmax(120px, 38%) 1fr;
}

.info-list dt {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.info-list dd {
  margin: 0;
  word-break: break-word;
}

@media (max-width: 480px) {
  .info-list {
    grid-template-columns: 1fr;
  }

  .info-list dt { margin-top: 0.5rem; }

  .info-list dt:first-child { margin-top: 0; }
}

/* ——— Планшеты и телефоны (iPhone 15 Pro Max ≈ 430px) ——— */
@media (max-width: 768px) {
  .form-grid { grid-template-columns: 1fr; }

  .header-inner {
    flex-direction: column;
    align-items: stretch;
  }

  .header-nav {
    justify-content: space-between;
    width: 100%;
  }

  .user { max-width: none; flex: 1; }
}

@media (max-width: 480px) {
  .container { padding-left: max(0.75rem, var(--safe-left)); padding-right: max(0.75rem, var(--safe-right)); }

  .card { padding: 1rem; }

  /* Таблицы → карточки */
  .table--stack thead { display: none; }

  .table--stack tbody tr {
    display: block;
    margin-bottom: 1rem;
    padding: 0.75rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }

  .table--stack tbody td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.75rem;
    padding: 0.5rem 0;
    border: none;
    border-bottom: 1px solid var(--border);
    text-align: right;
  }

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

  .table--stack tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 0.75rem;
    color: var(--muted);
    text-align: left;
    flex-shrink: 0;
    max-width: 42%;
  }

  .table--stack tbody td.actions-cell,
  .table--stack tbody td[data-label="Действия"] {
    flex-direction: column;
    align-items: stretch;
  }

  .table--stack tbody td.actions-cell::before,
  .table--stack tbody td[data-label="Действия"]::before {
    margin-bottom: 0.35rem;
  }

  .table--stack .actions {
    width: 100%;
    flex-direction: column;
  }

  .table--stack .actions .btn,
  .table--stack .actions a.btn {
    width: 100%;
    text-align: center;
  }

  .table--stack .inline-details {
    display: block;
    width: 100%;
  }

  .table--stack .action-panel,
  .table--stack .delete-panel {
    width: 100%;
    min-width: 0;
  }

  .table--stack code { text-align: left; word-break: break-all; }

  .url-row .copy-btn { width: 100%; }

  .page-actions .btn { width: 100%; }
}

/* Крупные iPhone (15 Pro Max, 16 Pro Max) и новее — чуть больше отступы */
@media (min-width: 390px) and (max-width: 480px) {
  h1 { font-size: 1.35rem; }

  .btn-sm { min-height: 40px; font-size: 0.85rem; }
}

/* Ландшафт на телефоне */
@media (max-height: 500px) and (orientation: landscape) {
  .login-page { align-items: flex-start; padding-top: 1rem; }

  .header { margin-bottom: 0.75rem; }

  .header-inner { flex-direction: row; }
}
