:root {
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --danger: #dc2626;
  --ok: #059669;
  --navy: #0f172a;
  --radius: 12px;
  --tap: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 16px;
  min-height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  color: var(--ink);
  background: var(--bg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

a {
  color: var(--accent);
}

.container-app {
  width: min(1280px, calc(100% - 1.25rem));
  margin: 0 auto;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 1040;
  padding-top: var(--safe-top);
  background: #0b1220;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.header-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 64px;
  padding: 0.55rem 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  color: #fff;
  text-decoration: none;
  min-width: 0;
}

.brand:hover {
  color: #fff;
}

.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  object-fit: cover;
  flex-shrink: 0;
  background: #000;
}

.brand-copy {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}

.brand-copy strong {
  font-size: 0.95rem;
  letter-spacing: 0.01em;
}

.brand-copy small {
  color: rgba(255, 255, 255, 0.55);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.header-nav {
  display: flex;
  align-items: center;
  margin-left: 0.25rem;
}

.nav-pill {
  color: rgba(255, 255, 255, 0.72);
  text-decoration: none;
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9rem;
}

.nav-pill.is-active,
.nav-pill:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}

.header-user {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}

.user-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #1d4ed8;
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.72rem;
  font-weight: 700;
}

.user-name {
  display: none;
  color: rgba(255, 255, 255, 0.88);
  font-size: 0.88rem;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-signout {
  min-height: 36px;
  padding: 0 0.8rem;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.04);
  color: #fff;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  touch-action: manipulation;
}

.btn-signout:hover {
  background: rgba(255, 255, 255, 0.1);
}

@media (max-width: 480px) {
  .header-nav {
    display: none;
  }

  .brand-copy small {
    display: none;
  }
}

.app-main {
  padding: 1.1rem 0 calc(1.25rem + var(--safe-bottom));
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

.page-head h1 {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 700;
}

.page-sub {
  margin: 0.15rem 0 0;
  color: var(--muted);
}

.count-badge {
  min-width: 42px;
  height: 36px;
  padding: 0 0.7rem;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #eef2ff;
  color: #3730a3;
  font-weight: 700;
  font-size: 0.9rem;
}

.data-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}

.list-toolbar {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.85rem;
  border-bottom: 1px solid var(--line);
  background: #fafafa;
}

.toolbar-search .form-control {
  width: 100%;
}

.toolbar-filters {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.form-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: #374151;
  margin-bottom: 0.3rem;
}

.form-control,
.form-select,
.btn {
  min-height: var(--tap);
  font-size: 16px;
  border-radius: 10px;
  touch-action: manipulation;
}

.form-control,
.form-select {
  border-color: #d1d5db;
}

.form-control:focus,
.form-select:focus,
.form-check-input:focus,
.btn:focus {
  box-shadow: 0 0 0 0.18rem rgba(37, 99, 235, 0.18);
  border-color: var(--accent);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
}

.btn-ghost {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
}

.btn-ghost.disabled {
  opacity: 0.4;
  pointer-events: none;
}

.data-head {
  display: none;
}

.data-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.35rem 0.7rem;
  padding: 0.85rem;
  border-bottom: 1px solid var(--line);
  align-items: start;
}

.data-row:last-child {
  border-bottom: 0;
}

.col-client {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  min-width: 0;
  grid-column: 1;
}

.avatar {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  border-radius: 10px;
  background: #eef2ff;
  color: #3730a3;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

.row-title {
  font-weight: 600;
  line-height: 1.3;
}

.row-sub {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.mobile-only {
  display: inline;
}

.col-type,
.col-dates,
.col-machines {
  display: none;
}

.col-status {
  grid-column: 2;
  justify-self: end;
}

.col-actions {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.35rem;
}

.type-chip {
  display: inline-block;
  background: #f3f4f6;
  color: #374151;
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
  font-size: 0.75rem;
  font-weight: 600;
}

.status-dot {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  font-weight: 600;
}

.status-dot::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

.status-dot.is-on {
  color: var(--ok);
}

.status-dot.is-off {
  color: var(--danger);
}

.btn-row {
  min-height: 40px;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 9px;
}

.btn-row-danger {
  background: #fff;
  border: 1px solid #fecaca;
  color: #b91c1c;
}

.btn-row-primary {
  background: #fff;
  border: 1px solid #bfdbfe;
  color: #1d4ed8;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 2.4rem 1rem;
}

.list-footer {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.8rem 0.85rem calc(0.8rem + var(--safe-bottom));
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.9rem;
}

.pager-controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.5rem;
  align-items: center;
}

.pager-page {
  text-align: center;
  font-weight: 700;
  color: var(--ink);
}

.app-modal .modal-content {
  border: 0;
  border-radius: 16px;
}

.app-modal .modal-header,
.app-modal .modal-footer {
  border: 0;
}

.app-modal .modal-title {
  font-size: 1.1rem;
  margin: 0;
}

.modal-client,
.modal-help,
.loading-state {
  color: var(--muted);
  margin: 0.2rem 0 0;
}

.modal-form {
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

.field {
  margin-bottom: 0.85rem;
}

.app-modal .modal-footer {
  position: sticky;
  bottom: 0;
  background: #fff;
  padding: 0.85rem 1rem calc(0.85rem + var(--safe-bottom));
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: 0.6rem;
}

.modal-kicker {
  margin: 0 0 0.15rem;
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.machines-header {
  align-items: flex-start;
  gap: 0.75rem;
  padding: 1rem 1rem 0.85rem;
  background: #f8fafc;
  border-bottom: 1px solid var(--line) !important;
}

.machines-header-copy {
  min-width: 0;
}

.machines-header-meta {
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.machines-header .count-badge {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--ink);
}

.machines-body {
  padding: 0 !important;
}

.machine-list-wrap {
  min-height: 120px;
}

.machine-head {
  display: none;
}

.machine-list {
  display: flex;
  flex-direction: column;
}

.machine-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.45rem 0.75rem;
  padding: 0.9rem 1rem;
  border-bottom: 1px solid var(--line);
  align-items: start;
}

.machine-row:last-child {
  border-bottom: 0;
}

.machine-main {
  display: flex;
  align-items: flex-start;
  gap: 0.7rem;
  min-width: 0;
}

.machine-title-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.machine-details {
  grid-column: 1;
  padding-left: 2.7rem;
}

.detail-line {
  font-size: 0.88rem;
}

.allow-toggle {
  grid-column: 2;
  grid-row: 1 / span 2;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  gap: 0.35rem;
  margin: 0;
  min-width: 92px;
}

.allow-toggle .form-check-input {
  width: 2.9rem;
  height: 1.5rem;
  margin: 0;
  cursor: pointer;
}

.machines-modal .modal-footer {
  display: flex;
  justify-content: stretch;
}

.machines-modal .modal-footer .btn {
  width: 100%;
}

.login-body {
  min-height: 100dvh;
  margin: 0;
  background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
}

.login-shell {
  min-height: 100dvh;
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: calc(1rem + var(--safe-top)) 1rem calc(1rem + var(--safe-bottom));
}

.login-card {
  width: 100%;
  max-width: 420px;
  margin: auto;
  padding: 1.4rem 1.15rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
}

.login-brand {
  text-align: center;
  margin-bottom: 1.2rem;
}

.login-logo {
  width: 72px;
  height: 72px;
  margin: 0 auto 0.85rem;
  border-radius: 16px;
  object-fit: cover;
  background: #000;
  display: block;
}

.login-brand h1 {
  font-size: 1.35rem;
  margin-bottom: 0.2rem;
}

.login-brand p {
  color: var(--muted);
  margin: 0;
}

.password-wrap {
  position: relative;
}

.password-wrap .form-control {
  padding-right: 4.2rem;
}

.password-toggle {
  position: absolute;
  right: 0.3rem;
  top: 50%;
  transform: translateY(-50%);
  border: 0;
  background: transparent;
  color: var(--accent);
  min-height: 40px;
  padding: 0 0.65rem;
  font-weight: 700;
}

.remember-check {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--tap);
  margin: 0.15rem 0 0.9rem;
}

.remember-check .form-check-input {
  width: 1.15rem;
  height: 1.15rem;
  margin: 0;
}

.btn-block {
  width: 100%;
}

@media (min-width: 768px) {
  .container-app {
    width: min(1280px, calc(100% - 2rem));
  }

  .nav-user {
    padding-top: 0;
    justify-content: flex-end;
  }

  .user-name {
    display: inline;
  }

  .list-toolbar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
  }

  .toolbar-search {
    flex: 1 1 240px;
  }

  .toolbar-filters {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    flex: 1 1 520px;
  }

  .toolbar-filters .form-select {
    min-height: 42px;
    width: auto;
    min-width: 130px;
    flex: 1 1 130px;
  }

  .toolbar-filters .btn {
    min-height: 42px;
  }

  .col-actions {
    max-width: 240px;
  }

  .login-card {
    padding: 1.8rem 1.6rem 1.5rem;
  }

  .app-modal .modal-footer {
    display: flex;
    justify-content: flex-end;
  }

  .machines-modal .modal-footer .btn {
    width: auto;
    min-width: 120px;
  }

  .machine-head,
  .machine-row {
    display: grid;
    grid-template-columns: minmax(220px, 1.4fr) 1fr 120px;
    gap: 0.75rem;
    align-items: center;
  }

  .machine-head {
    padding: 0.55rem 1rem;
    background: #f8fafc;
    color: #6b7280;
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--line);
  }

  .machine-details {
    grid-column: auto;
    padding-left: 0;
  }

  .allow-toggle {
    grid-column: auto;
    grid-row: auto;
    flex-direction: row;
    justify-content: flex-end;
    min-width: 0;
  }
}

@media (min-width: 1100px) {
  .page-head h1 {
    font-size: 1.6rem;
  }

  .mobile-only {
    display: none;
  }

  .data-head,
  .data-row {
    display: grid;
    grid-template-columns: minmax(220px, 1.6fr) 150px 110px 170px 140px 220px;
    gap: 0.75rem;
    align-items: center;
    padding: 0.7rem 1rem;
  }

  .data-head .sort-link {
    color: inherit;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
  }

  .data-head .sort-link::after {
    content: "↕";
    font-size: 0.7rem;
    opacity: 0.35;
  }

  .data-head .sort-link.is-active {
    color: var(--ink);
  }

  .data-head .sort-link.is-active::after {
    content: "↑";
    opacity: 1;
  }

  .data-head .sort-link.is-active.is-desc::after {
    content: "↓";
  }

  .data-head .sort-link:hover {
    color: var(--accent);
  }

  .data-row {
    min-height: 64px;
  }

  .data-row:hover {
    background: #f8fafc;
  }

  .col-client,
  .col-type,
  .col-status,
  .col-dates,
  .col-machines,
  .col-actions {
    display: block;
    grid-column: auto;
    justify-self: stretch;
    margin: 0;
  }

  .col-client {
    display: flex;
  }

  .col-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.4rem;
    max-width: none;
  }

  .btn-row {
    min-height: 34px;
    min-width: auto;
    padding: 0 0.75rem;
    font-size: 0.78rem;
    white-space: nowrap;
  }

  .list-footer {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  .pager-controls {
    width: 240px;
  }
}
