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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f7fa;
  color: #333;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

/* --- NAV --- */
.nav {
  background: #2c3e50;
  padding: 0 20px;
  display: flex;
  align-items: center;
  height: 56px;
  position: relative;
}

/* Desktop: nav-top is just the brand, inline */
.nav-top {
  display: contents;
}

/* Desktop: nav-collapse shown inline */
.nav-collapse {
  display: contents;
}

.nav-brand {
  color: #ecf0f1;
  font-size: 18px;
  font-weight: 700;
  text-decoration: none;
  flex-shrink: 0;
  margin-right: auto;
}

.nav-links {
  display: flex;
  gap: 4px;
}

.nav-links a {
  color: #bdc3c7;
  text-decoration: none;
  padding: 8px 14px;
  border-radius: 4px;
  font-size: 14px;
  transition: background 0.2s, color 0.2s;
}

.nav-links a:hover,
.nav-links a.active {
  background: #34495e;
  color: #fff;
}

.nav-user {
  color: #95a5a6;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.nav-user a {
  color: #e74c3c;
  text-decoration: none;
  font-size: 13px;
}

/* Burger button — hidden on desktop */
.nav-burger {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  flex-shrink: 0;
}

.nav-burger span {
  display: block;
  width: 22px;
  height: 2px;
  background: #ecf0f1;
  margin: 5px 0;
  border-radius: 1px;
  transition: transform 0.3s, opacity 0.3s;
}

/* --- CONTAINER --- */
.container {
  max-width: 1200px;
  margin: 24px auto;
  padding: 0 20px;
}

h1 {
  font-size: 24px;
  margin-bottom: 20px;
  color: #2c3e50;
}

/* --- CARD --- */
.card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  padding: 24px;
  margin-bottom: 20px;
}

.card h2 {
  font-size: 18px;
  margin-bottom: 16px;
  color: #2c3e50;
}

/* --- TABLE --- */
table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid #eee;
  font-size: 14px;
}

th {
  background: #f8f9fa;
  font-weight: 600;
  color: #555;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

tr:hover {
  background: #f8f9fa;
}

/* Responsive table wrapper for horizontal scroll on small screens */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -24px;
  padding: 0 24px;
}

/* --- FORMS --- */
.form-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: #555;
  text-transform: uppercase;
}

input[type="text"],
input[type="password"],
input[type="number"],
input[type="date"] {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px; /* 16px prevents zoom on iOS */
  outline: none;
  transition: border-color 0.2s;
  max-width: 100%;
}

input:focus {
  border-color: #3498db;
}

/* --- BUTTONS --- */
.btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.2;
  box-sizing: border-box;
  cursor: pointer;
  text-decoration: none;
  transition: opacity 0.2s;
  white-space: nowrap;
}

.btn:hover {
  opacity: 0.85;
}

.btn-primary {
  background: #3498db;
  color: #fff;
}

.btn-success {
  background: #27ae60;
  color: #fff;
}

.btn-warning {
  background: #f39c12;
  color: #fff;
}

.btn-danger {
  background: #e74c3c;
  color: #fff;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

/* --- FLASH --- */
.flash {
  padding: 12px 16px;
  border-radius: 4px;
  margin-bottom: 16px;
  font-size: 14px;
  word-break: break-word;
}

.flash-success {
  background: #d4edda;
  color: #155724;
  border: 1px solid #c3e6cb;
}

.flash-error {
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
}

/* --- LOGIN --- */
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #2c3e50;
  padding: 16px;
}

.login-card {
  background: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  width: 100%;
  max-width: 360px;
}

.login-card h1 {
  text-align: center;
  margin-bottom: 24px;
}

.login-card .form-group {
  margin-bottom: 16px;
}

.login-card input {
  width: 100%;
}

.login-card .btn {
  width: 100%;
  justify-content: center;
  padding: 10px;
  font-size: 16px;
}

/* --- BADGE --- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-active {
  background: #d4edda;
  color: #155724;
}

.badge-inactive {
  background: #f8d7da;
  color: #721c24;
}

/* --- ACTIONS --- */
.actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.actions form {
  margin: 0;
  display: flex;
}

/* --- PAGINATION --- */
.pagination {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  justify-content: center;
  flex-wrap: wrap;
}

.pagination a {
  padding: 6px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  text-decoration: none;
  color: #333;
  font-size: 14px;
}

.pagination a:hover {
  background: #3498db;
  color: #fff;
  border-color: #3498db;
}

.pagination a.active {
  background: #3498db;
  color: #fff;
  border-color: #3498db;
}

/* --- COUNTER BAR --- */
.counter-bar {
  background: #ecf0f1;
  border-radius: 4px;
  height: 20px;
  width: 120px;
  overflow: hidden;
  display: inline-block;
  vertical-align: middle;
}

.counter-bar-fill {
  height: 100%;
  background: #27ae60;
  transition: width 0.3s;
}

.counter-bar-fill.warning {
  background: #f39c12;
}

.counter-bar-fill.danger {
  background: #e74c3c;
}

/* --- MODAL --- */
.modal-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.5);
  z-index: 1000;
  justify-content: center;
  align-items: center;
  padding: 16px;
}

.modal-overlay:target {
  display: flex;
}

.modal {
  background: #fff;
  border-radius: 8px;
  padding: 24px;
  width: 100%;
  max-width: 480px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
}

.modal h2 {
  margin-bottom: 16px;
  word-break: break-word;
}

.modal-close {
  float: right;
  text-decoration: none;
  font-size: 20px;
  color: #999;
  padding: 4px 8px;
}

/* --- MONO --- */
.mono {
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
  font-size: 12px;
  background: #f0f0f0;
  padding: 2px 6px;
  border-radius: 3px;
  word-break: break-all;
}

/* --- CHART --- */
.chart-container {
  position: relative;
  width: 100%;
  margin-bottom: 16px;
  overflow: hidden;
}

.chart-container canvas {
  display: block;
  max-width: 100%;
  cursor: crosshair;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 8px;
}

.chart-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 13px;
  color: #333;
  user-select: none;
  padding: 4px 0;
}

.chart-legend-item input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin: 0;
  cursor: pointer;
  flex-shrink: 0;
}

.chart-legend-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}

.chart-legend-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 200px;
}

.chart-legend-item.legend-disabled {
  opacity: 0.4;
}

.chart-legend-item.legend-disabled .chart-legend-swatch {
  background-color: #ccc !important;
}

/* --- FOOTER --- */
.app-footer {
  max-width: 1200px;
  margin: 24px auto 12px;
  padding: 12px 20px;
  text-align: center;
  font-size: 12px;
  color: #95a5a6;
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ============================
   RESPONSIVE: Tablets (<=768px)
   ============================ */
@media (max-width: 768px) {
  /* Show burger button */
  .nav-burger {
    display: block;
  }

  .nav {
    flex-direction: column;
    height: auto;
    padding: 0;
    gap: 0;
    align-items: stretch;
  }

  .nav-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
  }

  /* Collapsible nav links + user */
  .nav-collapse {
    display: none;
    flex-direction: column;
    gap: 4px;
    padding: 0 16px 12px;
    border-top: 1px solid #34495e;
  }

  .nav-collapse.open {
    display: flex;
  }

  .nav-links {
    flex-direction: column;
    gap: 2px;
    padding-top: 8px;
  }

  .nav-links a {
    padding: 10px 14px;
    font-size: 15px;
  }

  .nav-user {
    padding: 10px 14px;
    border-top: 1px solid #34495e;
    margin-top: 4px;
    padding-top: 12px;
  }

  /* Container */
  .container {
    padding: 0 12px;
    margin: 16px auto;
  }

  h1 {
    font-size: 20px;
    margin-bottom: 16px;
  }

  /* Cards */
  .card {
    padding: 16px;
    margin-bottom: 16px;
  }

  /* Forms */
  .form-row {
    flex-direction: column;
    gap: 12px;
  }

  .form-group {
    width: 100%;
  }

  .form-group input[type="text"],
  .form-group input[type="password"],
  .form-group input[type="number"],
  .form-group input[type="date"] {
    width: 100%;
  }

  .form-group .btn {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
  }

  /* No-label spacer: hide on mobile */
  .form-group label:empty,
  .form-group label:only-child {
    display: none;
  }

  /* Table: horizontal scroll */
  .table-wrap {
    margin: 0 -16px;
    padding: 0 16px;
  }

  table {
    font-size: 13px;
  }

  th, td {
    padding: 8px 10px;
  }

  /* Make action buttons stack on small screens */
  .actions {
    flex-direction: column;
    gap: 4px;
    align-items: stretch;
  }

  .actions .btn {
    width: 100%;
    justify-content: center;
    font-size: 13px;
    padding: 8px 12px;
  }

  /* Modal */
  .modal {
    padding: 20px;
    max-width: 100%;
  }

  .modal h2 {
    font-size: 16px;
  }

  /* Counter bar */
  .counter-bar {
    width: 80px;
  }

  /* Chart legend */
  .chart-legend {
    gap: 6px 12px;
  }

  .chart-legend-item {
    font-size: 12px;
  }

  .chart-legend-text {
    max-width: 150px;
  }

  /* Login */
  .login-card {
    padding: 24px;
  }
}

/* ============================
   RESPONSIVE: Small phones (<=480px)
   ============================ */
@media (max-width: 480px) {
  .container {
    padding: 0 8px;
    margin: 12px auto;
  }

  .card {
    padding: 12px;
    border-radius: 6px;
  }

  h1 {
    font-size: 18px;
  }

  .card h2 {
    font-size: 16px;
  }

  /* Table card-layout for tasks/apikeys */
  .responsive-cards thead {
    display: none;
  }

  .responsive-cards tbody tr {
    display: block;
    background: #fff;
    border: 1px solid #eee;
    border-radius: 6px;
    padding: 12px;
    margin-bottom: 12px;
  }

  .responsive-cards tbody tr:hover {
    background: #fff;
  }

  .responsive-cards tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border-bottom: 1px solid #f5f5f5;
    font-size: 13px;
  }

  .responsive-cards tbody td:last-child {
    border-bottom: none;
    padding-top: 8px;
  }

  .responsive-cards tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 11px;
    text-transform: uppercase;
    color: #555;
    flex-shrink: 0;
    margin-right: 12px;
  }

  /* Hide modal-overlay row in card mode */
  .responsive-cards tbody tr.modal-row {
    display: none;
    border: none;
    padding: 0;
    margin: 0;
  }

  .responsive-cards tbody tr.modal-row td {
    display: block;
    padding: 0;
    border: none;
  }

  .responsive-cards tbody tr.modal-row td::before {
    display: none;
  }

  /* Actions in card layout */
  .responsive-cards .actions {
    flex-direction: row;
    justify-content: flex-end;
  }

  /* Counter bar */
  .counter-bar {
    width: 60px;
    height: 16px;
  }

  /* Chart legend */
  .chart-legend {
    flex-direction: column;
    gap: 4px;
  }

  .chart-legend-text {
    max-width: none;
  }

  /* Pagination smaller */
  .pagination {
    gap: 4px;
  }

  .pagination a {
    padding: 6px 10px;
    font-size: 13px;
  }

  /* Login */
  .login-card {
    padding: 20px;
  }

  .login-card h1 {
    font-size: 20px;
  }

  /* Footer */
  .app-footer {
    flex-direction: column;
    gap: 2px;
    padding: 12px;
  }
}

/* --- Touch-friendly: larger tap targets on all mobile --- */
@media (pointer: coarse) {
  .btn {
    min-height: 44px;
    min-width: 44px;
    padding: 10px 16px;
  }

  .btn-sm {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 13px;
  }

  .nav-links a {
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  .pagination a {
    min-height: 44px;
    display: flex;
    align-items: center;
  }
}
