:root {
  --brand: #C0392B;
  --brand-dark: #922B21;
  --brand-soft: #FEF2F2;
  --accent: #E74C3C;
  --bg: #F3F4F6;
  --card: #FFFFFF;
  --text: #1A1A2E;
  --muted: #6B7280;
  --line: #E5E7EB;
  --ok: #059669;
  --warn: #D97706;
  --danger: #DC2626;
  --shadow: 0 6px 20px rgba(26, 26, 46, 0.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Cairo", sans-serif;
  color: var(--text);
  background: var(--bg);
}

.view[hidden] { display: none !important; }
.boot-view[hidden] { display: none !important; }
body[data-view="boot"] #loginView,
body[data-view="boot"] #appView { display: none !important; }
body[data-view="login"] #bootView,
body[data-view="login"] #appView { display: none !important; }
body[data-view="admin"] #bootView,
body[data-view="admin"] #loginView { display: none !important; }
body[data-view="admin"] #appView { display: flex !important; }

.boot-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, #FEF2F2 0%, #F3F4F6 45%, #fff 100%);
}
.boot-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--text);
}
.boot-card strong {
  letter-spacing: 0.06em;
  font-size: 0.95rem;
}
.boot-card span {
  color: var(--muted);
  font-size: 0.84rem;
}
.boot-spinner {
  width: 28px;
  height: 28px;
  margin-top: 6px;
  border-radius: 50%;
  border: 3px solid #F5B7B1;
  border-top-color: var(--brand);
  animation: spin .7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== LOGIN ===== */
.login-view {
  min-height: 100vh;
  display: grid;
  grid-template-rows: minmax(200px, 30vh) 1fr;
  background: var(--bg);
}

.login-hero {
  position: relative;
  background: linear-gradient(145deg, var(--brand) 0%, var(--brand-dark) 60%, #7B241C 100%);
  color: #fff;
  display: grid;
  place-items: center;
  overflow: hidden;
  border-radius: 0 0 28px 28px;
  box-shadow: 0 12px 28px rgba(192, 57, 43, 0.22);
}

.login-hero-glow {
  position: absolute;
  width: 240px;
  height: 240px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,0.2), transparent 70%);
  top: -40px;
  left: -20px;
}

.brand-mark { text-align: center; z-index: 1; padding: 16px; }
.brand-mark h1 {
  margin: 10px 0 4px;
  letter-spacing: 0.08em;
  font-size: clamp(1.3rem, 3vw, 1.75rem);
}
.brand-mark p { margin: 0; opacity: 0.92; font-size: 0.9rem; }

.logo-disc {
  width: 64px;
  height: 64px;
  margin: 0 auto;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 1.2rem;
  color: var(--brand);
  background: #fff;
  box-shadow: 0 8px 16px rgba(0,0,0,0.14);
}
.logo-disc.sm {
  width: 38px;
  height: 38px;
  margin: 0;
  border-radius: 11px;
  font-size: 0.78rem;
  color: #fff;
  background: linear-gradient(145deg, var(--brand), var(--brand-dark));
  box-shadow: none;
}

.login-sheet {
  width: min(400px, calc(100% - 32px));
  margin: -36px auto 36px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px 22px;
  box-shadow: var(--shadow);
  border: 1px solid var(--line);
  position: relative;
  z-index: 2;
}

.login-sheet h2 { margin: 0 0 4px; font-size: 1.2rem; text-align: center; }
.login-sheet .sub { margin: 0 0 18px; color: var(--muted); font-size: 0.88rem; text-align: center; }

.stack { display: grid; gap: 12px; }
label { display: grid; gap: 6px; font-size: 0.86rem; font-weight: 600; }
label > span { color: var(--text); }

.password-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.password-wrap input {
  padding-left: 44px;
}
.password-toggle {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 34px;
  height: 34px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 8px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}
.password-toggle:hover { color: var(--brand); background: var(--brand-soft); }

.remember-row {
  display: flex !important;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  cursor: pointer;
}
.remember-row input {
  width: 17px;
  height: 17px;
  accent-color: var(--brand);
  margin: 0;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #F9FAFB;
  border-radius: 11px;
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: #F5B7B1;
  box-shadow: 0 0 0 3px rgba(192, 57, 43, 0.1);
  background: #fff;
}

.btn-primary, .btn-ghost, .btn-warn, .btn-ok, .btn-danger, .btn-logout {
  border: 0;
  border-radius: 11px;
  padding: 9px 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.btn-primary {
  color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--accent));
  box-shadow: 0 5px 14px rgba(192, 57, 43, 0.2);
}
.btn-block { width: 100%; }
.btn-ghost {
  background: #fff;
  border: 1px solid var(--line);
  color: var(--text);
}
.btn-ghost.sm { padding: 4px 9px; font-size: 0.76rem; }
.btn-warn {
  background: #FFFBEB;
  color: #B45309;
  border: 1px solid #FDE68A;
  min-width: 110px;
}
.btn-ok {
  background: #ECFDF5;
  color: #047857;
  border: 1px solid #A7F3D0;
  min-width: 110px;
}
.btn-danger {
  background: #fff;
  color: var(--danger);
  border: 1px solid #FECACA;
  min-width: 110px;
}
.error { color: var(--danger); margin: 0; font-size: 0.86rem; }

/* ===== ADMIN: top bar instead of sidebar ===== */
.admin-view {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

.app-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  box-shadow: 0 2px 12px rgba(192, 57, 43, 0.06);
  position: sticky;
  top: 0;
  z-index: 20;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}
.app-brand strong { display: block; font-size: 0.88rem; letter-spacing: 0.04em; }
.app-brand small { color: var(--muted); font-size: 0.72rem; }

.top-tabs {
  display: inline-flex;
  justify-self: center;
  background: #F3F4F6;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px;
  gap: 4px;
}

.top-tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 8px 16px;
  font: inherit;
  font-weight: 700;
  font-size: 0.86rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.top-tab.active {
  background: linear-gradient(135deg, var(--brand), var(--accent));
  color: #fff;
  box-shadow: 0 4px 12px rgba(192, 57, 43, 0.25);
}

.nav-badge {
  background: var(--brand);
  color: #fff;
  border-radius: 999px;
  min-width: 20px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  font-size: 0.7rem;
  padding: 0 5px;
}

.btn-logout {
  background: var(--brand-soft);
  color: var(--danger);
  border: 1px solid #FECACA;
}

.admin-main {
  flex: 1;
  min-width: 0;
  padding: 14px 18px 20px;
}

.page-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.page-toolbar h2 { margin: 0; font-size: 1.05rem; }
.muted { color: var(--muted); margin: 2px 0 0; font-size: 0.8rem; }
#btnNewClient {
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 0.86rem;
  white-space: nowrap;
}

.page[hidden] { display: none !important; }

.workspace {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.list-pane, .detail-pane {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.list-pane {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: sticky;
  top: 72px;
  max-height: calc(100vh - 100px);
}

.list {
  display: grid;
  gap: 7px;
  overflow: auto;
}

.client-item {
  display: block;
  width: 100%;
  text-align: right;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 10px 11px;
  cursor: pointer;
  background: #fff;
  font: inherit;
  color: inherit;
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.client-item:hover {
  border-color: #F5B7B1;
  box-shadow: 0 4px 12px rgba(192, 57, 43, 0.08);
}
.client-item.active {
  border-color: var(--brand);
  background: var(--brand-soft);
  box-shadow: inset 3px 0 0 var(--brand);
}
.client-form[hidden] { display: none !important; }
.client-form.is-visible { display: block; }
.client-item .code { font-weight: 800; color: var(--brand); font-size: 0.88rem; }
.client-item .name { margin-top: 2px; font-size: 0.86rem; }
.badge {
  display: inline-block;
  margin-top: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 700;
}
.badge.Active { background: #ECFDF5; color: #047857; }
.badge.Suspended { background: #FFFBEB; color: #B45309; }
.badge.Expired { background: var(--brand-soft); color: var(--danger); }

.detail-pane { padding: 0; overflow: hidden; min-height: 120px; }

.empty-state {
  padding: 14px 16px;
  color: var(--muted);
  font-size: 0.9rem;
  background: #FAFAFA;
  border-bottom: 1px solid var(--line);
}
.empty-state p { margin: 0; }
.empty-state[hidden] { display: none !important; }

.client-form { padding: 14px 16px 16px; }
.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 12px;
}
.detail-header h3 { margin: 0 0 6px; font-size: 1.05rem; }
.activation-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.82rem;
}
.activation-row code {
  background: #1A1A2E;
  color: #fff;
  padding: 3px 9px;
  border-radius: 7px;
  letter-spacing: 0.04em;
  font-weight: 700;
}

.stat-inline {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.stat-mini {
  font-size: 0.8rem;
  color: var(--muted);
  background: #F9FAFB;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 10px;
}
.stat-mini strong { color: var(--brand); }

.status-pill {
  display: inline-block;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  background: #ECFDF5;
  color: #047857;
}
.status-pill.Suspended { background: #FFFBEB; color: #B45309; }
.status-pill.Expired { background: var(--brand-soft); color: var(--danger); }

.form-section {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 10px;
  background: #fff;
}
.form-section h4 {
  margin: 0 0 8px;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  gap: 8px;
}
.sec-num {
  display: inline-grid;
  place-items: center;
  min-width: 28px;
  height: 22px;
  padding: 0 6px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--brand), var(--accent));
  color: #fff;
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.02em;
}
.section-hint {
  margin: -2px 0 10px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.duration-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0;
}
.duration-chips button {
  border: 1px solid var(--line);
  background: #F9FAFB;
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font: inherit;
  font-weight: 700;
  font-size: 0.78rem;
  cursor: pointer;
}
.duration-chips button.active {
  background: var(--brand-soft);
  border-color: #F5B7B1;
  color: var(--brand);
}

.end-date-field { margin-top: 2px; }

.extend-panel {
  margin-top: 12px;
  padding: 10px;
  border-radius: 11px;
  background: #FFF7ED;
  border: 1px solid #FED7AA;
}
.extend-panel[hidden] { display: none !important; }
.extend-title {
  font-weight: 700;
  color: #9A3412;
  margin-bottom: 8px;
  font-size: 0.84rem;
}
.inline { display: flex; gap: 8px; }
.inline input { flex: 1; }

.action-bar {
  position: sticky;
  bottom: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 12px;
  padding: 12px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 -4px 16px rgba(26, 26, 46, 0.04);
}
.action-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.devices-list, .pending-grid { display: grid; gap: 8px; }
.device-item, .pending-card {
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  background: #F9FAFB;
}
.device-item .meta, .pending-card .meta { color: var(--muted); font-size: 0.78rem; }
.device-actions, .pending-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.pending-grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.pending-card {
  flex-direction: column;
  align-items: stretch;
  background: #fff;
  box-shadow: var(--shadow);
}
.pending-card h4 { margin: 0 0 6px; }

.btn-primary:disabled,
.btn-ghost:disabled,
.btn-warn:disabled,
.btn-ok:disabled,
.btn-danger:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
  filter: grayscale(0.25);
}

#btnSaveClient[hidden] {
  display: none !important;
}

/* ===== Confirm dialog ===== */
.confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(26, 26, 46, 0.48);
  backdrop-filter: blur(4px);
  animation: fadeIn .16s ease;
}
.confirm-overlay[hidden] { display: none !important; }

.confirm-dialog {
  width: min(420px, 100%);
  background: #fff;
  border-radius: 18px;
  padding: 22px 20px 18px;
  box-shadow: 0 24px 48px rgba(26, 26, 46, 0.22);
  text-align: center;
  border: 1px solid var(--line);
  animation: dialogIn .18s ease;
}
.confirm-icon {
  width: 52px;
  height: 52px;
  margin: 0 auto 12px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  font-size: 1.35rem;
  font-weight: 800;
  background: var(--brand-soft);
  color: var(--brand);
}
.confirm-dialog.tone-warn .confirm-icon {
  background: #FFFBEB;
  color: #B45309;
}
.confirm-dialog.tone-danger .confirm-icon {
  background: #FEF2F2;
  color: var(--danger);
}
.confirm-dialog.tone-ok .confirm-icon {
  background: #ECFDF5;
  color: #047857;
}
.confirm-dialog h3 {
  margin: 0 0 8px;
  font-size: 1.1rem;
}
.confirm-dialog p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.6;
}
.confirm-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}
.confirm-actions .btn-ghost,
.confirm-actions .btn-primary,
.confirm-actions .btn-danger,
.confirm-actions .btn-warn,
.confirm-actions .btn-ok {
  min-width: 120px;
  flex: 1;
}
.confirm-dialog.tone-danger #confirmOk.btn-danger {
  background: var(--danger);
  color: #fff;
  border: 0;
  box-shadow: 0 5px 14px rgba(220, 38, 38, 0.22);
}
.confirm-dialog.tone-warn #confirmOk {
  background: linear-gradient(135deg, #D97706, #B45309);
  box-shadow: 0 5px 14px rgba(180, 83, 9, 0.22);
  color: #fff;
  border: 0;
}
.confirm-dialog.tone-ok #confirmOk {
  background: linear-gradient(135deg, #059669, #047857);
  box-shadow: 0 5px 14px rgba(5, 150, 105, 0.22);
  color: #fff;
  border: 0;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes dialogIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 900px) {
  .app-header {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "brand logout"
      "tabs tabs";
  }
  .app-brand { grid-area: brand; }
  .btn-logout { grid-area: logout; }
  .top-tabs {
    grid-area: tabs;
    justify-self: stretch;
    width: 100%;
  }
  .top-tab { flex: 1; justify-content: center; }
  .workspace { grid-template-columns: 1fr; }
  .list-pane { position: static; max-height: 240px; }
  .form-grid { grid-template-columns: 1fr; }
  .action-bar { flex-direction: column; align-items: stretch; }
  .action-group { width: 100%; }
  .action-group .btn-primary,
  .action-group .btn-danger,
  .action-group .btn-warn,
  .action-group .btn-ok { flex: 1; }
}
