:root {
  --navy: #17386f;
  --navy-soft: #52749a;
  --blue: #5e7de2;
  --purple: #7851ad;
  --accent: #4594e8;
  --green: #29be72;
  --orange: #ff9f0a;
  --red: #fa443d;
  --panel: #ffffff;
  --surface: #f6f8fe;
  --muted: #7890ab;
  --line: #e0e5ed;
  --shadow: 0 12px 28px rgba(32, 36, 95, .14);
  --radius: 20px;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  color: var(--navy);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
  background: linear-gradient(120deg, var(--blue) 0%, #6870cf 46%, var(--purple) 100%);
}

button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; text-decoration: none; }
.hidden { display: none !important; }

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 12px 21px;
  font-weight: 700;
  cursor: pointer;
  color: var(--navy);
  display: inline-flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  transition: transform .16s, filter .16s, background .16s;
}
.btn:hover { transform: translateY(-1px); filter: brightness(1.03); }
.btn:disabled { cursor: not-allowed; filter: grayscale(.4); opacity: .55; transform: none; }
.btn-primary { background: var(--accent); color: white; }
.btn-success { background: #45b453; color: white; }
.btn-danger { background: #cd3b59; color: white; border: 1px solid rgba(255,255,255,.35); }
.btn-muted { background: #f1f1f3; color: #25354a; }
.btn-glass {
  color: white;
  background: rgba(255,255,255,.15);
  border: 1px solid rgba(255,255,255,.38);
  backdrop-filter: blur(8px);
}
.btn-block { width: 100%; }

.toast {
  position: fixed;
  z-index: 2000;
  bottom: 24px;
  left: 50%;
  transform: translate(-50%, 120px);
  opacity: 0;
  color: white;
  background: #18315b;
  padding: 13px 20px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  transition: .25s ease;
  pointer-events: none;
  max-width: min(90vw, 560px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: #b52e43; }
.toast.success { background: #218b52; }

/* Login */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(430px, 100%);
  padding: 42px;
  text-align: center;
  background: white;
  border-radius: 28px;
  box-shadow: 0 30px 70px rgba(23, 29, 74, .32);
}
.login-card h1 { margin: 20px 0 7px; font-size: 26px; }
.login-card > p { color: var(--muted); margin: 0 0 28px; }
.login-card small { color: #8292a5; display: block; margin-top: 19px; }
.login-form { display: grid; gap: 16px; text-align: left; }
.login-form label span { font-size: 13px; color: var(--navy-soft); display: block; margin-bottom: 7px; font-weight: 600; }
.form-alert { background: #ffedf0; color: #a1283b; border-radius: 10px; padding: 11px; margin-bottom: 15px; }

/* Dashboard shell */
.app-shell { min-height: 100vh; display: grid; grid-template-columns: 300px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #f7f8fd;
  color: #304157;
  padding: 27px 20px 22px;
  z-index: 120;
}
.sidebar-brand { text-align: center; margin-bottom: 37px; }
.brand-logo {
  width: 73px;
  height: 73px;
  border-radius: 20px;
  margin: 0 auto;
  display: grid;
  place-items: center;
  color: white;
  font-size: 33px;
  background: linear-gradient(145deg, #479bec, #2876b9);
  box-shadow: 0 10px 20px rgba(37,102,161,.28);
}
.brand-logo.large { width: 86px; height: 86px; border-radius: 24px; }
.heart-line { filter: drop-shadow(0 1px 0 rgba(0,0,0,.08)); }
.sidebar-brand h1 { font-size: 20px; margin: 21px 0 9px; color: #203e72; }
.sidebar-brand p { font-size: 12px; color: #6985a6; margin: 0; }
.sidebar-nav { display: grid; gap: 4px; }
.nav-link {
  width: 100%;
  border: 0;
  background: transparent;
  border-radius: 13px;
  padding: 14px 18px;
  display: flex;
  align-items: center;
  gap: 13px;
  color: #35465b;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
}
.nav-link:hover, .nav-link.active { color: #2876ba; background: #eaf1ff; }
.nav-icon { width: 24px; text-align: center; font-size: 22px; color: #4c7194; }
.sidebar-user {
  margin-top: auto;
  border-top: 1px solid #e1e6ef;
  padding: 16px 10px 0;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
}
.sidebar-user span { font-weight: 700; }
.sidebar-user small { grid-column: 1; color: #8494a9; font-size: 10px; margin-top: 2px; }
.sidebar-user a { grid-column: 2; grid-row: 1 / span 2; color: #c43a52; font-size: 13px; font-weight: 700; }
.mobile-menu, .mobile-close { display: none; }

.dashboard-main {
  min-height: 100vh;
  padding: 28px 34px 55px;
  color: white;
  overflow: hidden;
}
.header-title-row { display: flex; align-items: center; gap: 14px; min-height: 50px; }
.header-title-row h2 { font-size: 32px; margin: 0; text-shadow: 0 2px 3px rgba(0,0,0,.12); }
.header-actions { margin-left: auto; display: flex; gap: 14px; }
.breadcrumb {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 14px;
  padding: 15px 3px 10px;
  color: rgba(255,255,255,.9);
}
.breadcrumb button { color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
.breadcrumb button:hover { text-decoration: underline; }
.crumb-separator { font-size: 22px; opacity: .85; line-height: 14px; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 28px;
}
.summary-card {
  height: 158px;
  padding: 31px 28px;
  background: var(--panel);
  color: var(--navy);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(70px, 5vw, 92px);
  gap: 12px;
  align-items: center;
  overflow: hidden;
}
.summary-card > div:first-child { min-width: 0; }
.summary-card .label { color: #5c7592; font-size: clamp(14px, 1vw, 17px); line-height: 1.15; font-weight: 600; margin-bottom: 15px; text-transform: uppercase; overflow-wrap: anywhere; }
.summary-card .value { font-size: 44px; font-weight: 700; line-height: 1; }
.donut {
  --percentage: 0;
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--percentage) * 1%), #e6edf5 0);
  position: relative;
  justify-self: end;
}
.donut::before { content: ""; position: absolute; inset: 11px; border-radius: 50%; background: white; }
.donut span { z-index: 1; font-size: clamp(16px, 1.2vw, 20px); font-weight: 800; }

.dashboard-section { padding-top: 38px; }
.dashboard-section > h3 { margin: 0 0 20px; font-size: 25px; color: white; }
.selection-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(190px, 1fr));
  gap: 22px;
  align-items: start;
}
.selection-card {
  appearance: none;
  border: 0;
  min-height: 145px;
  border-radius: 18px;
  padding: 27px 23px 22px;
  background: white;
  color: var(--navy);
  box-shadow: 0 9px 22px rgba(32,38,90,.1);
  text-align: left;
  cursor: pointer;
  transition: transform .18s, box-shadow .18s;
}
.selection-card:hover { transform: translateY(-4px); box-shadow: 0 15px 28px rgba(27,33,83,.2); }
.selection-card h4 { font-size: 18px; margin: 0 0 11px; }
.selection-card .unit-count { font-size: 13px; color: #6080a4; margin-bottom: 21px; }
.mini-status { display: flex; gap: 13px; color: #365777; font-size: 12px; }
.mini-status span { display: flex; align-items: center; gap: 5px; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.dot.green { background: var(--green); }
.dot.blue { background: #2f92d1; }
.dot.orange { background: #da7315; }

.category-grid { grid-template-columns: repeat(2, minmax(240px, 350px)); }
.category-card { min-height: 190px; display: grid; align-content: center; text-align: center; }
.category-icon { font-size: 42px; margin-bottom: 10px; }

.machine-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(275px, 1fr));
  gap: 22px;
  align-items: start;
}
.machine-card {
  min-height: 384px;
  color: var(--navy);
  background: white;
  border-radius: 18px;
  padding: 20px;
  box-shadow: var(--shadow);
  position: relative;
}
.machine-header { display: flex; gap: 10px; justify-content: space-between; align-items: flex-start; }
.machine-header h4 { margin: 0 0 5px; font-size: 19px; }
.machine-subtitle { color: #7190b0; font-size: 12px; margin-bottom: 5px; }
.serial { display: inline-block; background: #f0f5fb; color: #54759a; border-radius: 10px; padding: 4px 9px; font: 12px ui-monospace, SFMono-Regular, Consolas, monospace; }
.badges { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.badge { color: white; border-radius: 999px; padding: 7px 12px; font-size: 11px; font-weight: 800; white-space: nowrap; }
.badge.off { background: var(--red); }
.badge.on { background: var(--green); }
.badge.treatment { background: #2f91e6; }
.badge.maintenance { background: var(--orange); cursor: pointer; border: 0; }
.badge.archived { background: #667085; }
.machine-stats { display: grid; gap: 14px; margin: 21px 0 12px; }
.stat-row { display: flex; justify-content: space-between; gap: 16px; font-size: 13px; align-items: center; }
.stat-row .stat-label { color: #6684a5; font-weight: 600; }
.stat-value { font-weight: 800; white-space: nowrap; }
.timer-pill { color: white; background: linear-gradient(100deg, #607ce3, #7650c3); border-radius: 7px; padding: 6px 9px; font: 700 14px ui-monospace, SFMono-Regular, Consolas, monospace; }
.timer-pill.treatment { background: #2795e8; }
.machine-footer { display: flex; align-items: end; gap: 7px; padding-top: 2px; }
.icon-button { border: 0; background: none; font-size: 27px; cursor: pointer; padding: 0; line-height: 1; transition: transform .15s; }
.icon-button:hover { transform: scale(1.14); }
.last-update { margin-left: auto; color: #91a5bc; font-size: 9px; text-align: right; max-width: 160px; }
.empty-state { text-align: center; padding: 80px 20px; }
.empty-state > div { font-size: 60px; opacity: .7; }
.empty-state h4 { font-size: 24px; margin: 10px 0 5px; }
.empty-state p { margin: 0; opacity: .8; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(17,28,62,.62);
  backdrop-filter: blur(3px);
  display: grid;
  place-items: center;
  padding: 22px;
}
.modal-card {
  width: min(620px, 100%);
  max-height: 90vh;
  overflow: auto;
  background: white;
  border-radius: 22px;
  box-shadow: 0 30px 80px rgba(12,17,42,.36);
  padding: 28px;
  position: relative;
  color: var(--navy);
}
.modal-wide { width: min(970px, 100%); }
.modal-close { position: absolute; top: 14px; right: 17px; border: 0; background: #eff2f7; color: #4c607a; width: 34px; height: 34px; border-radius: 50%; font-size: 22px; cursor: pointer; }
.modal-heading { display: flex; align-items: center; gap: 14px; padding-right: 35px; margin-bottom: 22px; }
.modal-heading h3 { margin: 0 0 4px; font-size: 21px; }
.modal-heading p { margin: 0; color: #7a8ea5; font-size: 13px; }
.modal-heading p span { display: block; }
.modal-heading .modal-identifiers { margin-top: 3px; color: #536f90; font-size: 12px; }
.modal-heading .modal-identifiers strong { color: var(--navy); font-weight: 700; }
.modal-icon { width: 45px; height: 45px; border-radius: 13px; display: grid; place-items: center; color: white; font-size: 22px; font-weight: 800; }
.modal-icon.warning, .modal-icon.voltage { background: #ff9f0a; }
.modal-icon.humidity { background: #3199df; }
.modal-icon.archive { background: #667085; }
.modal-form { display: grid; gap: 16px; }
.modal-form label > span { display: block; margin-bottom: 7px; color: #576e8d; font-size: 12px; font-weight: 700; }
.modal-form input:disabled { background: #f2f4f7; color: #475467; }
.modal-form textarea { min-height: 130px; }
.maintenance-items { display: grid; gap: 12px; }
.maintenance-item { border: 1px solid var(--line); background: #f8faff; border-radius: 14px; padding: 16px; }
.maintenance-item h4 { margin: 0 0 6px; }
.maintenance-item p { color: #657d98; font-size: 13px; margin: 0 0 9px; line-height: 1.45; }
.maintenance-item-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 12px; color: #66809f; }
.maintenance-item-footer .btn { padding: 8px 14px; }
.voltage-summary { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.voltage-item { border: 1px solid var(--line); border-radius: 14px; padding: 17px; background: #f9fbfe; }
.voltage-item strong { display: block; font-size: 25px; margin: 7px 0; }
.voltage-item small { color: #7b91a9; }
.chart-wrap { border: 1px solid var(--line); border-radius: 14px; padding: 14px; overflow: hidden; position: relative; }
#humidityCanvas { width: 100%; height: 330px; display: block; }
.chart-tooltip {
  position: absolute;
  z-index: 3;
  transform: translate(-50%, calc(-100% - 12px));
  pointer-events: none;
  white-space: nowrap;
  color: white;
  background: rgba(20, 48, 91, .96);
  border-radius: 9px;
  padding: 8px 11px;
  box-shadow: 0 8px 20px rgba(14, 33, 65, .2);
  font-size: 12px;
  line-height: 1.45;
}
.chart-tooltip strong { display: block; font-size: 13px; }
.chart-tooltip::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: rgba(20, 48, 91, .96);
  border-bottom: 0;
}
.chart-tooltip.below { transform: translate(-50%, 12px); }
.chart-tooltip.below::after {
  top: -5px;
  bottom: auto;
  border-top: 0;
  border-bottom: 5px solid rgba(20, 48, 91, .96);
}

/* Admin pages */
.admin-page { min-height: 100vh; max-width: 1600px; margin: 0 auto; padding: 32px 0 65px; color: white; }
.admin-header { display: flex; align-items: center; gap: 20px; margin-bottom: 30px; }
.admin-header h1 { margin: 0; font-size: 35px; text-shadow: 0 2px 2px rgba(0,0,0,.1); }
.admin-nav { margin-left: auto; display: flex; gap: 16px; }
.admin-card { background: white; color: var(--navy); border-radius: 23px; padding: 29px; margin-bottom: 34px; }
.admin-card h2, .table-title { margin: 0 0 22px; font-size: 25px; }
.table-title { color: white; }
.viewer-banner { background: rgba(255,255,255,.18); border: 1px solid rgba(255,255,255,.35); padding: 12px 17px; border-radius: 12px; margin: -14px 0 24px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-grid label { min-width: 0; }
.form-grid label > span {
  color: #576e8d;
  display: block;
  font-size: 12px;
  margin: 0 0 -19px 17px;
  position: relative;
  z-index: 1;
  pointer-events: none;
}
input, select, textarea {
  width: 100%;
  color: #172f57;
  border: 1px solid #d6dce5;
  background: white;
  border-radius: 12px;
  padding: 16px 17px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.form-grid label > span + input,
.form-grid label > span + select,
.form-grid label > span + textarea { padding-top: 25px; padding-bottom: 10px; }
input:focus, select:focus, textarea:focus { border-color: #538cde; box-shadow: 0 0 0 3px rgba(73,143,224,.14); }
textarea { min-height: 82px; resize: vertical; }
.full-span { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 15px; padding-top: 6px; }
.section-divider { border: 0; border-top: 2px solid rgba(255,255,255,.2); margin: 30px 0 36px; }
.filter-row { display: flex; gap: 18px; margin: -5px 0 18px; }
.filter-row select { width: 190px; padding: 11px 15px; border-radius: 10px; }
.table-card { background: white; color: var(--navy); border-radius: 22px; overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 880px; }
th, td { padding: 18px 17px; text-align: left; border-bottom: 1px solid #e5e7eb; font-size: 14px; }
th { background: #f8f9fb; color: #0c306a; font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
.table-actions { display: flex; gap: 7px; align-items: center; }
.table-actions .btn { font-size: 12px; padding: 7px 12px; }
.role-pill, .type-pill { display: inline-block; padding: 5px 10px; border-radius: 999px; font-size: 11px; font-weight: 700; color: white; background: #468edc; }
.role-pill.teknisi { background: var(--orange); }
.role-pill.viewer { background: #8d59bc; }
.type-pill.time_interval { background: #9252b9; }
.region-pill { display: inline-block; color: #33465e; background: #e9edf2; border-radius: 999px; padding: 4px 9px; font-size: 11px; margin: 2px; }
.tab-row { display: flex; gap: 10px; margin: -3px 0 20px; }
.tab-button {
  border: 1px solid rgba(255,255,255,.42);
  border-radius: 999px;
  background: rgba(255,255,255,.13);
  color: white;
  padding: 10px 17px;
  font-weight: 700;
  cursor: pointer;
}
.tab-button.active { background: white; color: var(--navy); }
.tab-button span { display: inline-grid; place-items: center; min-width: 24px; height: 24px; margin-left: 6px; border-radius: 999px; background: rgba(85,108,142,.18); font-size: 11px; }
.archived-row { background: #fafafa; color: #5f6b7a; }
.archive-pill, .performed-pill { display: inline-block; margin-top: 5px; border-radius: 999px; padding: 4px 9px; background: #667085; color: white; font-size: 11px; font-weight: 700; }
.history-filter-card { margin-bottom: 30px; }
.history-filter-grid { display: grid; grid-template-columns: repeat(4, minmax(180px, 1fr)); gap: 15px; }
.history-filter-grid label > span { display: block; margin: 0 0 7px 2px; color: #576e8d; font-size: 12px; font-weight: 700; }
.history-filter-grid input, .history-filter-grid select { padding: 12px 14px; }
.history-filter-actions { align-self: end; }
.history-title-row { display: flex; align-items: center; justify-content: space-between; gap: 18px; }
.history-total { margin: 0 4px 22px 0; border-radius: 999px; padding: 8px 13px; background: rgba(255,255,255,.18); border: 1px solid rgba(255,255,255,.32); font-weight: 700; }
.history-table { min-width: 1050px; }
.history-table td { vertical-align: top; line-height: 1.45; }
.history-table td small { display: block; margin-top: 4px; color: #7b8da4; }
.history-date { white-space: nowrap; }
.history-note { min-width: 280px; white-space: pre-wrap; overflow-wrap: anywhere; }
.performed-pill { margin: 0; background: #4d7fbb; }
.pagination-row { display: flex; align-items: center; justify-content: center; gap: 18px; margin-top: 20px; color: white; }

@media (max-width: 1500px) {
  .summary-grid { gap: 18px; }
  .summary-card { padding: 25px 22px; }
  .selection-grid { grid-template-columns: repeat(4, minmax(190px, 1fr)); }
  .machine-grid { grid-template-columns: repeat(3, minmax(275px, 1fr)); }
  .admin-page { max-width: none; margin: 0 6%; }
}

@media (max-width: 1180px) {
  .app-shell { grid-template-columns: 260px minmax(0, 1fr); }
  .sidebar { padding-inline: 13px; }
  .dashboard-main { padding-inline: 24px; }
  .summary-grid { grid-template-columns: repeat(2, 1fr); }
  .selection-grid { grid-template-columns: repeat(3, minmax(190px, 1fr)); }
  .machine-grid { grid-template-columns: repeat(2, minmax(275px, 1fr)); }
  .history-filter-grid { grid-template-columns: repeat(2, minmax(180px, 1fr)); }
}

@media (max-width: 800px) {
  .app-shell { display: block; }
  .sidebar {
    position: fixed;
    left: 0;
    transform: translateX(-105%);
    transition: transform .22s ease;
    width: min(300px, 86vw);
    box-shadow: 15px 0 35px rgba(16,22,49,.3);
  }
  .sidebar.open { transform: translateX(0); }
  .mobile-menu { display: inline-grid; place-items: center; border: 1px solid rgba(255,255,255,.4); color: white; background: rgba(255,255,255,.13); border-radius: 10px; width: 42px; height: 42px; }
  .mobile-close { display: block; position: absolute; right: 10px; top: 8px; border: 0; background: none; font-size: 28px; color: #65778d; }
  .dashboard-main { padding: 18px 16px 40px; }
  .header-title-row { align-items: flex-start; flex-wrap: wrap; }
  .header-title-row h2 { font-size: 24px; line-height: 42px; }
  .header-actions { width: 100%; margin: 2px 0 0; justify-content: flex-end; }
  .summary-grid { grid-template-columns: 1fr; gap: 14px; }
  .summary-card { height: 128px; }
  .selection-grid, .machine-grid, .category-grid { grid-template-columns: 1fr; }
  .selection-card { min-height: 130px; }
  .dashboard-section { padding-top: 27px; }
  .admin-page { margin: 0; padding: 20px 15px 45px; }
  .admin-header { align-items: flex-start; flex-wrap: wrap; margin-bottom: 20px; }
  .admin-header h1 { font-size: 27px; }
  .admin-nav { width: 100%; margin: 0; overflow-x: auto; gap: 9px; padding-bottom: 4px; }
  .admin-nav .btn { padding: 10px 14px; }
  .admin-card { padding: 20px 16px; border-radius: 18px; }
  .form-grid { grid-template-columns: 1fr; }
  .full-span { grid-column: auto; }
  .form-actions { flex-wrap: wrap; }
  .filter-row { flex-wrap: wrap; }
  .filter-row select { width: 100%; }
  .voltage-summary { grid-template-columns: 1fr; }
  .history-filter-grid { grid-template-columns: 1fr; }
  .history-filter-actions { width: 100%; }
  .history-title-row { align-items: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
