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

html,
body {
  margin: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #f6f6f6;
  color: #1a1a1a;
  line-height: 1.4;
  font-size: 14px;
}

a {
  color: inherit;
  text-decoration: none;
}

.layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: #fff;
  border-bottom: 1px solid #e0e0e0;
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  max-width: 1200px;
  margin: 0 auto;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

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

.btn-icon {
  min-width: 40px;
  padding: 8px 10px;
  font-size: 1.1rem;
  line-height: 1;
}

.brand {
  font-weight: 700;
  font-size: 1rem;
  color: #111;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  color: #444;
}

.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 35;
}

.sidebar {
  display: none;
}

.bottom-nav {
  position: sticky;
  bottom: 0;
  z-index: 25;
  display: flex;
  justify-content: space-around;
  gap: 2px;
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom, 0));
  background: #fff;
  border-top: 1px solid #e0e0e0;
}

.bottom-link {
  flex: 1;
  text-align: center;
  padding: 8px 2px;
  border-radius: 8px;
  font-size: 0.7rem;
  font-weight: 600;
  color: #666;
}

.bottom-link.active,
.bottom-link:hover {
  background: #eee;
  color: #111;
}

.bottom-accent {
  flex: 0 0 44px;
  max-width: 44px;
  background: #111;
  color: #fff !important;
  font-size: 1.2rem;
}

.bottom-more {
  border: none;
  background: transparent;
  font: inherit;
  cursor: pointer;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 14px 12px 72px;
}

.page-head,
.hero {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.page-head h1,
.hero h1,
.login-card h1,
.h2 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
}

.hint,
.empty,
.tz-hint {
  color: #777;
  font-size: 0.82rem;
  margin: 0 0 10px;
}

.tz-footer {
  margin-top: 20px;
  padding-top: 8px;
  text-align: center;
  border-top: 1px solid #e8e8e8;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.dash-section {
  margin-bottom: 12px;
}

.dash-title {
  margin: 0 0 8px;
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #666;
}

.cards-health .health-card {
  border-left: 3px solid #d1d5db;
}

.health-card.health-ok {
  border-left-color: #22c55e;
}

.health-card.health-warn {
  border-left-color: #f59e0b;
  background: #fffbeb;
}

.health-card.health-danger {
  border-left-color: #ef4444;
  background: #fef2f2;
}

.host-load-panel,
.klipper-load-panel {
  padding: 14px 16px;
}

.host-load-panel.health-warn,
.klipper-load-panel.health-warn {
  border-left: 3px solid #f59e0b;
  background: #fffbeb;
}

.host-load-panel.health-danger,
.klipper-load-panel.health-danger {
  border-left: 3px solid #ef4444;
  background: #fef2f2;
}

.host-load-panel.health-ok,
.klipper-load-panel.health-ok {
  border-left: 3px solid #22c55e;
}

.load-meters {
  display: grid;
  gap: 14px;
}

.load-meter-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.load-meter-label {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #555;
}

.load-meter-value {
  font-size: 1.05rem;
  font-weight: 700;
  color: #111;
}

.load-bar {
  height: 10px;
  border-radius: 999px;
  background: #e5e7eb;
  overflow: hidden;
}

.load-bar > span {
  display: block;
  height: 100%;
  border-radius: 999px;
  background: #22c55e;
  transition: width 0.25s ease;
}

.load-meter.load-warn .load-bar > span {
  background: #f59e0b;
}

.load-meter.load-danger .load-bar > span {
  background: #ef4444;
}

.load-meter-scale {
  margin-top: 4px;
  font-size: 0.72rem;
  color: #777;
}

.host-load-extra,
.host-load-meta,
.klipper-load-extra,
.klipper-load-meta {
  margin-top: 12px;
}

.host-load-detail {
  color: #888;
  font-size: 0.72rem;
}

@media (min-width: 720px) {
  .load-meters {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (min-width: 960px) {
  .dash-host-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    align-items: start;
    margin-bottom: 12px;
  }

  .dash-host-grid .dash-section {
    margin-bottom: 0;
  }
}

.quick-search {
  margin-bottom: 12px;
}

.dash-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.dash-panel {
  padding: 12px;
}

.dash-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.dash-panel-head .dash-title {
  margin: 0;
}

.dash-empty {
  margin: 0;
}

.mini-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mini-item {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.mini-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.mini-main {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-weight: 600;
  font-size: 0.88rem;
  color: inherit;
}

a.mini-main:hover {
  color: #2563eb;
}

.mini-sub {
  display: block;
  margin-top: 4px;
  font-size: 0.75rem;
  color: #777;
  word-break: break-word;
}

.card,
.form,
.table-wrap,
.toolbar {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
}

.card {
  padding: 12px;
}

.card-link:active {
  opacity: 0.85;
}

.label {
  font-size: 0.72rem;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.value {
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.2;
  margin-top: 4px;
}

.value-warn {
  color: #b45309;
}

.value-danger {
  color: #b42318;
}

.card-sub {
  margin-top: 4px;
  font-size: 0.78rem;
  color: #777;
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font: inherit;
  white-space: nowrap;
}

.btn:hover {
  border-color: #999;
}

.btn-primary {
  background: #111;
  border-color: #111;
  color: #fff;
}

.tg-cell .tg-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

.tg-cell .tg-line:last-child {
  margin-bottom: 0;
}

.tg-user {
  font-weight: 600;
  color: #2563eb;
}

.tg-users-table tr.tg-hwid-extra td {
  border-top: none;
  padding-top: 2px;
}

.tg-users-table tr.tg-hwid-extra td:first-child {
  padding-top: 0;
}

.tg-same-user {
  padding-left: 4px;
  font-size: 0.9rem;
}

.page-sub {
  margin: 4px 0 0;
  color: #666;
  font-size: 0.9rem;
}

.muted {
  color: #888;
  font-size: 0.85rem;
}

.btn-danger {
  border-color: #e8b4b0;
  color: #b42318;
}

.btn-warn {
  border-color: #e5c9a0;
  color: #b45309;
}

.btn-sm {
  min-height: 32px;
  padding: 4px 8px;
  font-size: 0.8rem;
}

.btn-block {
  width: 100%;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px;
  margin-bottom: 10px;
  align-items: center;
}

.toolbar input,
.toolbar select,
.form input,
.form select {
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: #fff;
  font: inherit;
}

.toolbar .grow,
.form input,
.form select {
  flex: 1 1 160px;
  width: 100%;
}

.form {
  padding: 14px;
  display: grid;
  gap: 8px;
  max-width: 480px;
}

.form label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #555;
}

.message {
  padding: 10px 12px;
  margin-bottom: 10px;
  background: #f0f0f0;
  border-radius: 8px;
}

.error {
  padding: 10px 12px;
  background: #fff1ef;
  color: #b42318;
  border: 1px solid #f2c4bd;
  border-radius: 8px;
  margin-bottom: 10px;
}

.created-key-card {
  margin-bottom: 12px;
}

.created-code {
  font-size: 1rem;
  word-break: break-all;
  margin: 6px 0 10px;
}

.table-wrap {
  overflow-x: auto;
}

.table-wrap table {
  width: 100%;
  border-collapse: collapse;
  min-width: 560px;
}

.table-wrap th,
.table-wrap td {
  padding: 8px 8px;
  border-bottom: 1px solid #eee;
  text-align: left;
  vertical-align: top;
  font-size: 0.85rem;
}

.table-wrap th {
  position: sticky;
  top: 0;
  background: #fafafa;
  z-index: 1;
  font-size: 0.72rem;
  font-weight: 600;
  color: #666;
}

.table-wrap th.th-sortable {
  cursor: pointer;
  user-select: none;
}

.table-wrap th.th-sortable:hover {
  background: #efefef;
  color: #333;
}

.table-wrap th.sort-asc::after,
.table-wrap th.sort-desc::after {
  font-size: 0.65em;
  opacity: 0.75;
}

.table-wrap th.sort-asc::after {
  content: " ▲";
}

.table-wrap th.sort-desc::after {
  content: " ▼";
}

.pill {
  display: inline-flex;
  padding: 2px 8px;
  border-radius: 4px;
  background: #eee;
  font-size: 0.72rem;
  font-weight: 600;
}

.pill-unused,
.pill-active {
  background: #e8f5e9;
  color: #1b5e20;
}

.pill-used {
  background: #e3f2fd;
  color: #1565c0;
}

.pill-revoked {
  background: #f5f5f5;
  color: #666;
}

.pill-expired {
  background: #fff3e0;
  color: #e65100;
}

.mono {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.8rem;
  word-break: break-all;
}

.key-cell,
.hwid-cell {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.note-cell .inline-note-form {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.note-input {
  flex: 1 1 120px;
  min-width: 100px;
  max-width: 220px;
  padding: 6px 8px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 0.85rem;
}

.note-hint {
  font-size: 0.72rem;
  color: #6b7280;
  margin-top: 4px;
}

.col-num {
  width: 2.5rem;
  text-align: center;
  color: #6b7280;
  font-variant-numeric: tabular-nums;
}

.actions-cell {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

.inline-form {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  margin: 0;
}

.input-days,
.input-delta {
  width: 44px;
  min-height: 32px !important;
  padding: 4px 4px !important;
  text-align: center;
}

.input-datetime {
  width: min(168px, 42vw);
  min-height: 32px !important;
  padding: 4px 6px !important;
  font-size: 0.78rem;
}

.delta-unit {
  font-size: 0.75rem;
  color: var(--muted, #888);
  margin-right: 2px;
}

.license-expiry-tools {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
}

.row-fail {
  background: #fffaf9;
}

.err-cell {
  max-width: 200px;
  word-break: break-word;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(64px + env(safe-area-inset-bottom, 0));
  transform: translateX(-50%) translateY(120%);
  z-index: 50;
  padding: 10px 14px;
  background: #222;
  color: #fff;
  border-radius: 8px;
  font-size: 0.85rem;
  opacity: 0;
  transition: transform 0.2s, opacity 0.2s;
  pointer-events: none;
  max-width: min(92vw, 320px);
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}

.login-shell {
  width: min(380px, 100%);
}

.login-card {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 20px;
}

.login-card .form {
  max-width: none;
}

@media (min-width: 700px) {
  .dash-grid {
    grid-template-columns: 1fr 1fr;
  }
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: 180px 1fr;
    grid-template-rows: auto 1fr;
  }

  .topbar {
    grid-column: 1 / -1;
  }

  .menu-btn {
    display: none;
  }

  .sidebar {
    display: block;
    position: sticky;
    top: 53px;
    align-self: start;
    height: calc(100vh - 53px);
    padding: 12px 10px;
    border-right: 1px solid #e0e0e0;
    background: #fff;
  }

  .nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .nav-link {
    display: block;
    padding: 10px 12px;
    border-radius: 8px;
    font-weight: 500;
    color: #444;
  }

  .nav-link:hover,
  .nav-link.active {
    background: #f0f0f0;
    color: #111;
  }

  .nav-accent {
    background: #111;
    color: #fff !important;
  }

  .bottom-nav {
    display: none;
  }

  .container {
    padding: 16px 20px 24px;
  }
}

@media (max-width: 899px) {
  .layout.menu-open .overlay {
    display: block;
  }

  .layout.menu-open .sidebar {
    display: block;
    position: fixed;
    top: 53px;
    left: 0;
    bottom: 0;
    width: min(260px, 86vw);
    z-index: 40;
    background: #fff;
    border-right: 1px solid #e0e0e0;
    padding: 12px;
    overflow-y: auto;
  }

  .layout.menu-open .nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .layout.menu-open .nav-link {
    padding: 12px 10px;
    border-radius: 8px;
    font-weight: 500;
  }

  .container {
    padding: 12px 10px calc(76px + env(safe-area-inset-bottom, 0));
  }

  .page-head h1,
  .hero h1 {
    font-size: 1.1rem;
  }

  .page-head .pill {
    width: 100%;
    text-align: center;
  }

  .form,
  .created-key-card {
    max-width: none;
  }

  .icon-btn,
  .btn,
  .toolbar input,
  .toolbar select,
  .form input,
  .form select {
    min-height: 44px;
  }

  .login-card .form input {
    min-height: 44px;
  }

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

  .toolbar .grow,
  .toolbar input[type="search"],
  .toolbar select,
  .toolbar .btn {
    width: 100%;
  }

  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .card {
    padding: 10px;
  }

  .value {
    font-size: 1.25rem;
  }

  .btn-row .btn {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    white-space: normal;
    text-align: center;
    font-size: 0.78rem;
    padding: 8px 6px;
  }

  .table-sort-mobile {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px;
    border-bottom: 1px solid #eee;
    background: #fafafa;
  }

  .sort-mobile-label {
    flex: 0 0 auto;
    font-size: 0.72rem;
    font-weight: 600;
    color: #666;
    white-space: nowrap;
  }

  .table-sort-mobile .sort-select {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 40px;
  }

  .table-sort-mobile .sort-dir-btn {
    flex: 0 0 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    font-size: 1rem;
  }

  .table-responsive thead {
    display: none;
  }

  .table-responsive table,
  .table-responsive tbody,
  .table-responsive tr,
  .table-responsive td {
    display: block;
    width: 100%;
    min-width: 0;
  }

  .table-responsive tr {
    margin-bottom: 8px;
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 8px;
    background: #fff;
  }

  .table-responsive td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 8px;
    padding: 6px 0;
    border: none;
  }

  .table-responsive td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #888;
    flex: 0 0 38%;
    font-size: 0.72rem;
    line-height: 1.3;
  }

  .table-responsive td.empty {
    display: block;
    text-align: center;
  }

  .table-responsive td.empty::before {
    display: none;
  }

  .note-cell {
    flex-direction: column;
    align-items: stretch !important;
  }

  .note-cell::before {
    margin-bottom: 2px;
  }

  .inline-note-form {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
  }

  .note-input {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
    min-height: 32px;
    max-height: 32px;
    padding: 4px 8px;
    font-size: 0.78rem;
  }

  .note-cell .btn-sm {
    flex: 0 0 auto;
    min-height: 32px;
    padding: 4px 8px;
    font-size: 0.72rem;
  }

  .note-hint {
    margin-top: 2px;
    font-size: 0.68rem;
  }

  .hwid-cell,
  .key-cell {
    flex-direction: column;
    align-items: stretch !important;
  }

  .hwid-cell .btn,
  .key-cell .btn {
    align-self: flex-start;
  }

  .actions-cell {
    flex-direction: column;
    align-items: stretch;
  }

  .actions-cell::before {
    margin-bottom: 4px;
  }

  .actions-cell .btn,
  .actions-cell .inline-form {
    width: 100%;
  }

  .inline-form {
    flex-wrap: wrap;
  }

  .err-cell {
    word-break: break-word;
  }
}

@media (min-width: 900px) {
  .table-sort-mobile {
    display: none;
  }
}

.tester-cell {
  min-width: 7rem;
  white-space: nowrap;
}

.tester-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.tester-toggle .tester-cb {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: #7c3aed;
  cursor: pointer;
  flex-shrink: 0;
}

.tester-toggle .tester-lbl {
  font-size: 0.9rem;
  color: var(--text-muted, #9ca3af);
}

.tester-toggle .tester-cb:checked + .tester-lbl {
  color: #a78bfa;
  font-weight: 600;
}

.stats-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  margin-bottom: 16px;
}

.stats-period-label {
  font-weight: 600;
  color: #555;
}

.input-select {
  min-width: 120px;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
}

.stats-hint {
  color: #666;
  font-size: 0.85rem;
}

.stats-summary {
  margin-bottom: 20px;
}

.stats-section {
  margin-bottom: 28px;
}

.stats-section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 14px;
  margin-bottom: 6px;
}

.stats-section-head .dash-title {
  margin: 0;
}

.stats-chart-toggle {
  display: inline-flex;
  gap: 6px;
  padding: 3px;
  border-radius: 10px;
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
}

.stats-mode-btn.is-active {
  background: #2563eb;
  color: #fff;
  border-color: #2563eb;
}

.stats-note,
.stats-empty,
.stats-error {
  color: #666;
  font-size: 0.9rem;
  margin: 0 0 12px;
}

.stats-error {
  color: #b91c1c;
}

.chart-wrap {
  position: relative;
  height: min(360px, 55vw);
  min-height: 220px;
  padding: 12px;
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 12px;
}
