:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --line: #d9dee5;
  --text: #1f2933;
  --muted: #66788a;
  --accent: #2f6fa8;
  --accent-soft: #e8f1f8;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  color: var(--text);
  background: var(--bg);
  font-family: "Noto Sans KR", "Apple SD Gothic Neo", sans-serif;
}

.layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 64px 1fr 44px;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.top-header {
  grid-area: header;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.2;
}

.brand p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.auth-menu {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.auth-link {
  color: var(--text);
  text-decoration: none;
}

.auth-link:hover {
  color: var(--accent);
}

.auth-sep {
  color: var(--muted);
}

.auth-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 56px;
  padding: 5px 10px;
  border-radius: 4px;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  line-height: 1;
  cursor: default;
  user-select: none;
  pointer-events: none;
}

.auth-badge.user {
  background: #3d8f5e;
}

.auth-badge.admin {
  background: #b24a4a;
}

.left-sidebar {
  grid-area: sidebar;
  border-right: 1px solid var(--line);
  background: #f8fafc;
  padding: 14px 10px;
}

.menu-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu-item {
  display: block;
  text-decoration: none;
  color: var(--text);
  font-size: 14px;
  padding: 10px 12px;
  border-radius: 6px;
  margin-bottom: 4px;
}

.menu-item:hover {
  background: #edf2f7;
}

.menu-item.active {
  background: var(--accent-soft);
  color: #1f4f78;
  font-weight: 700;
}

.main-content {
  grid-area: main;
  padding: 16px;
  overflow: auto;
}

.page-header h2 {
  margin: 0;
  font-size: 20px;
}

.page-header p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.panel-grid {
  margin-top: 14px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  min-width: 0;
}

.panel {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 8px;
  padding: 12px;
  min-width: 0;
  width: 100%;
}

.panel.wide {
  grid-column: 1 / -1;
}

.panel h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.panel .panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.empty-block {
  border: 1px dashed #c9d2dc;
  background: #fbfdff;
  color: var(--muted);
  border-radius: 6px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
}

.empty-block.tall {
  height: 200px;
}

.footer {
  grid-area: footer;
  border-top: 1px solid var(--line);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: var(--muted);
}

.footer p {
  margin: 0;
}

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
}

.btn.small {
  padding: 4px 8px;
  font-size: 12px;
  border-radius: 4px;
}

.btn:hover {
  background: #f1f5f9;
}

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

.btn.primary:hover {
  background: #285f8d;
}

.btn.success {
  border-color: #3d8f5e;
  background: #3d8f5e;
  color: #fff;
}

.btn.success:hover {
  background: #35764d;
}

.btn.danger {
  border-color: #b24a4a;
  color: #b24a4a;
  background: #fff8f8;
}

.btn.danger:hover {
  background: #fff1f1;
}

.btn.danger-solid {
  border-color: #b24a4a;
  background: #b24a4a;
  color: #fff;
}

.btn.danger-solid:hover {
  background: #9d3f3f;
}

.pill {
  display: inline-block;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 11px;
  color: #4b5f74;
  background: #f8fafc;
  margin-left: 6px;
  vertical-align: middle;
}

.pill.ok {
  border-color: #a6d4a6;
  color: #2e6d2e;
  background: #f0fbf0;
}

.pill.warn {
  border-color: #e2c58e;
  color: #8a6424;
  background: #fff9ee;
}

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.gh-table,
.sensor-table,
.actuator-table,
.device-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 980px;
  font-size: 13px;
}

.gh-table th,
.gh-table td,
.sensor-table th,
.sensor-table td,
.actuator-table th,
.actuator-table td,
.device-table th,
.device-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  white-space: nowrap;
}

.gh-table th,
.sensor-table th,
.actuator-table th,
.device-table th {
  background: #f8fafc;
  font-weight: 700;
}

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

.form-grid.env-settings-grid {
  grid-template-columns: repeat(6, minmax(0, 1fr));
}

.form-grid.sensor-info-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.form-grid.single-column {
  grid-template-columns: 1fr;
}

/* Disabled form controls (greyed out) */
.form-grid .field input:disabled,
.form-grid .field select:disabled,
.form-grid .field textarea:disabled {
  background: #f3f4f6;
  color: #6b7280;
  cursor: not-allowed;
  opacity: 0.95;
}

.form-grid.sensor-info-grid-edit {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.env-grid-item {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.env-grid-item h4 {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}

.env-vertical-fields {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.field.full {
  grid-column: 1 / -1;
}

.sms-verification-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
}

/* span two columns for specific sensor-info forms */
.form-grid.sensor-info-grid .field.span-2,
.form-grid.sensor-info-grid-edit .field.span-2 {
  grid-column: span 2;
}

.field label {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
}

.field input,
.field select {
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  padding: 8px 10px;
  background: #fff;
  color: var(--text);
}

.time-input-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.time-input-group input,
.time-input-group select {
  width: 64px;
}

.time-input-group span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.meta-list {
  margin: 8px 0 0;
  padding-left: 16px;
  color: #475569;
  font-size: 12px;
  line-height: 1.6;
}

.row-actions {
  text-align: center;
}

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

.row-actions .btn.ac-control-btn {
  border-color: #374151;
  background: #4b5563;
  color: #fff;
}

.row-actions .btn.ac-control-btn:hover {
  background: #374151;
}

.row-actions .btn.zone-control-disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.row-actions .btn.zone-control-disabled:hover {
  background: #4b5563;
}

.modal-card.ac-control-card {
  max-width: 360px;
}

.ac-control-card .form-grid {
  grid-template-columns: 1fr;
}

.zone-tabbar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.zone-tab {
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
}

.zone-tab:hover {
  background: #eef4fa;
  color: var(--text);
}

.zone-tab.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: #1f4f78;
  font-weight: 700;
}

/* Fan control: 2 rows x 4 columns zone card grid */
.zone-tabbar.fan-zone-grid {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.zone-tabbar.fan-zone-grid .zone-tab {
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--muted);
  border-radius: 999px;
  padding: 7px 12px;
  font-size: 13px;
  cursor: pointer;
}

.zone-tabbar.fan-zone-grid .zone-tab:hover {
  background: #eef4fa;
  color: var(--text);
}

.zone-tabbar.fan-zone-grid .zone-tab.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: #1f4f78;
  font-weight: 700;
}

.gh-edit-btn {
  border-color: #2f6fa8;
  color: #2f6fa8;
  background: #f3f8ff;
}

.gh-edit-btn:hover {
  background: #e8f1ff;
}

.gh-create-open-btn {
  border-color: #3a8b4d;
  color: #3a8b4d;
  background: #f3fbf5;
  padding: 4px 10px;
  font-size: 12px;
}

.gh-create-open-btn:hover {
  background: #e6f6ea;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-card {
  width: min(760px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
  box-shadow: 0 16px 36px rgba(15, 23, 42, 0.2);
}

/* Smaller global notification and confirm modals */
#global-notification-modal .modal-card,
#global-confirm-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 10px;
  border-radius: 8px;
}

/* RS485 actuator modal: narrower for single-column layout */
#actuator-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

#system-fw-upload-modal .modal-card,
#system-fw-download-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

#system-fw-upload-modal .modal-header,
#system-fw-download-modal .modal-header {
  margin-bottom: 8px;
}

#system-fw-upload-modal .modal-header h3,
#system-fw-download-modal .modal-header h3 {
  font-size: 15px;
}

#system-fw-upload-modal .field,
#system-fw-download-modal .field {
  gap: 4px;
}

#system-fw-upload-modal .field label,
#system-fw-download-modal .field label,
#system-fw-upload-modal .field input,
#system-fw-download-modal .field input {
  font-size: 12px;
}

#system-fw-upload-modal .file-name {
  font-size: 10px;
}

#system-fw-download-modal #system-fw-md5-field {
  margin-top: 8px;
}

#system-fw-upload-modal .modal-actions .btn,
#system-fw-download-modal .modal-actions .btn {
  padding: 6px 10px;
  font-size: 12px;
}

#system-fw-upload-modal .modal-actions,
#system-fw-download-modal .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

#system-fw-download-modal .modal-actions {
  margin-top: 12px;
}

/* login modal: match single-column popup style (reduced) */
#global-login-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

#global-login-modal #login-modal-title {
  font-size: 16px;
  margin: 0;
}

#global-login-modal .modal-actions .btn {
  padding: 6px 10px;
  font-size: 13px;
}

/* reduce font sizes inside login modal for compact layout */
#global-login-modal .modal-card,
#global-login-modal .modal-card input,
#global-login-modal .modal-card select,
#global-login-modal .modal-card label {
  font-size: 13px;
}

#global-login-modal .modal-card input,
#global-login-modal .modal-card select {
  padding: 6px 8px;
}

/* compact width for login modal inputs to match single-input popups */
/* half-size inputs for login modal */
#global-login-modal .modal-card input,
#global-login-modal .modal-card select {
  width: 130px;
  display: block;
  margin-left: auto;
  margin-right: auto;
}

/* reduce inputs inside single-column modals to 50% width */
.modal-card .form-grid.single-column .field input,
.modal-card .form-grid.single-column .field select {
  width: auto;
  margin-left: 0;
  margin-right: 0;
}

/* ZigBee actuator modal: match RS485 modal width/design */
#zb-actuator-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

/* nutrient modal narrower for single-column layout */
#nutrient-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

/* Zone control modal: fit one control input without unused columns */
#zone-control-modal .modal-card {
  width: min(420px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

#zone-control-modal .form-grid {
  grid-template-columns: 1fr;
}

/* Zone edit modal: fit two fields per row */
#zone-edit-modal .modal-card {
  width: min(560px, 92%);
  max-height: none;
  padding: 12px;
  border-radius: 8px;
}

#zone-edit-modal .form-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

/* Reduce left padding for actuator command select to tighten left gap */
#actuator-modal #modal-command-select.form-control {
  padding-left: 6px;
}

/* Reduce left padding for nutrient command select to match actuator modal */
#nutrient-modal #nutrient-command-select.form-control {
  padding-left: 6px;
}
#global-notification-modal #global-notif-title,
#global-confirm-modal #global-conf-title {
  font-size: 16px;
  margin: 0;
}
#global-notification-modal #global-notif-body,
#global-confirm-modal #global-conf-body {
  font-size: 13px;
  color: #222;
  padding: 6px 0;
}
#global-notification-modal .modal-actions .btn,
#global-confirm-modal .modal-actions .btn {
  padding: 6px 10px;
  font-size: 13px;
}
@media (max-width: 480px) {
  #global-notification-modal .modal-card,
  #global-confirm-modal .modal-card {
    width: calc(100% - 40px);
  }
}

.modal-header {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: start;
  margin-bottom: 10px;
}

.modal-header h3 {
  margin: 0;
  grid-column: 1;
  grid-row: 1;
}

.modal-subtitle {
  margin: 5px 0 0 0;
  font-size: 13px;
  color: #666;
  grid-column: 1;
  grid-row: 2;
}

.modal-close {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  width: 30px;
  height: 30px;
  font-size: 18px;
  line-height: 1;
  grid-column: 2;
  grid-row: 1 / 3;
}

.modal-actions {
  justify-content: flex-end;
}

.env-row {
  display: grid;
  gap: 10px;
}

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

.env-row-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: 64px auto 1fr 44px;
    grid-template-areas:
      "header"
      "sidebar"
      "main"
      "footer";
  }

  .left-sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px;
  }

  .menu-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .menu-item {
    margin-bottom: 0;
  }

  .panel-grid {
    grid-template-columns: 1fr;
  }

  .panel-grid > .panel,
  .panel-grid > .dashboard-grid {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .form-grid {
    grid-template-columns: 1fr;
  }

  .form-grid.env-settings-grid {
    grid-template-columns: 1fr;
  }

  #zone-edit-modal .form-grid {
    grid-template-columns: 1fr;
  }

  .env-row-2,
  .env-row-3 {
    grid-template-columns: 1fr;
  }

  .modal-card {
    padding: 10px;
  }

  .settings-row {
    grid-template-columns: 1fr;
  }
}

/* When logged-out, hide surrounding chrome (sidebar/header/footer) so only login is shown */
body.logged-out .left-sidebar,
body.logged-out .top-header,
body.logged-out .footer {
  display: none !important;
}

/* Full-screen logged-out screen */
#logged-out-screen {
  position: fixed;
  inset: 0;
  background: #f4f6f8;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

#logged-out-screen[hidden] {
  display: none !important;
}

#logged-out-screen .login-card {
  width: min(420px, 92%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px;
  box-shadow: 0 12px 28px rgba(15,23,42,0.15);
}

#logged-out-screen .login-card .field input,
#logged-out-screen .login-card .field select {
  width: 100%;
  padding: 8px 10px;
  font-size: 14px;
  margin-top: 6px;
}

#logged-out-screen .login-card .field + .field {
  margin-top: 12px;
}

#logged-out-screen .login-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
}

.login-error {
  margin-top: 8px;
  color: #b24a4a;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.4;
}

.login-error[hidden] {
  display: none !important;
}

.sms-status {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.4;
}

.sms-status.error {
  color: #b24a4a;
}

.compact-settings-stack {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin: 16px 0;
}

.settings-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
}

.settings-column {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}

.settings-row:last-child {
  border-bottom: none;
}

.settings-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 12px;
}

.settings-group:first-child {
  padding-left: 0;
  padding-right: 24px;
  border-right: 1px solid var(--line);
}

.settings-group:last-child {
  padding-left: 24px;
  padding-right: 0;
}

.settings-group .field {
  gap: 4px;
}

.settings-group .field label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.settings-group .field input {
  padding: 8px 10px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
}

.settings-column .field {
  display: flex;
  flex-direction: row;
  gap: 8px;
  align-items: center;
}

.settings-column .field label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  width: 50%;
  flex-shrink: 0;
}

.settings-column .field input {
  width: 50%;
  padding: 6px 8px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
}
/* ensure selects match inputs in settings columns */
.settings-column .field select {
  width: 50%;
  padding: 6px 8px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
}

.advanced-settings-stack,
.basic-settings-stack {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0;
}

.advanced-group {
  gap: 10px;
}

.advanced-group .field {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

.advanced-group .field label {
  width: 50%;
  flex-shrink: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.advanced-group .field input {
  width: 50%;
  padding: 6px 8px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
}
/* ensure selects match inputs in advanced groups */
.advanced-group .field select {
  width: 50%;
  padding: 6px 8px;
  font-size: 13px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: white;
}
