:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1f2933;
  --muted: #65737e;
  --line: #ccd3da;
  --accent: #12736d;
  --accent-dark: #0d5d58;
  --danger: #9f2a2a;
}

[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.auth-locked {
  min-height: 100vh;
  background: #f2f4f7;
  display: grid;
  place-items: center;
}

body.auth-locked .shell {
  display: none;
}

.shell {
  width: min(1180px, calc(100% - 32px));
  margin: 24px auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  margin-bottom: 18px;
}

h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.2;
}

.app-main-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-direction: row-reverse;
}

#appLogoMain {
  width: 40px;
  height: 40px;
  object-fit: contain;
}

#mqttStatus {
  margin: 6px 0 0;
  color: var(--muted);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.menu-dropdown {
  position: relative;
  display: inline-block;
  padding-bottom: 8px;
  margin-bottom: -8px;
}

.menu-trigger {
  border: 1px solid var(--line);
  background: #eef1f4;
  color: var(--text);
  font-size: 16px;
  font-weight: 700;
  padding: 10px 12px;
  border-radius: 8px;
  line-height: 1.2;
  min-width: 120px;
  text-align: left;
}

.menu-dropdown > .menu-list {
  display: none !important;
  position: absolute;
  top: 100%;
  left: 0;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 8px 24px rgba(31, 41, 51, 0.12);
  z-index: 40;
  display: block;
  overflow: hidden;
}

.menu-list .menu-item {
  display: block;
  width: 100%;
  border: 0;
  background: #fff;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  text-align: left;
  padding: 10px 12px;
  border-radius: 0;
  margin: 0;
}

.menu-list .menu-item:hover {
  background: #eef1f4;
}

.menu-dropdown:hover > .menu-list,
.menu-dropdown:focus-within > .menu-list {
  display: block !important;
}

#languageMenuList,
#accountMenuList {
  width: 100%;
}

.login-screen {
  width: min(460px, calc(100% - 24px));
  display: none;
}

.auth-locked .login-screen {
  display: block;
}

.login-screen.active {
  display: block;
}

.login-card {
  width: 100%;
  background: #fff;
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 24px;
  display: grid;
  gap: 12px;
}

.login-brand {
  display: grid;
  justify-items: center;
  gap: 8px;
  margin-bottom: 4px;
}

.login-brand-title {
  margin: 0;
  font-size: 28px;
  line-height: 1.2;
  text-align: center;
}

.login-brand-logo {
  width: 80px !important;
  height: 80px !important;
  max-width: 80px !important;
  max-height: 80px !important;
  object-fit: contain;
  display: block;
}

.interface-logo-preview {
  width: 80px;
  height: 80px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px;
  background: #fff;
  display: block;
}

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

.permissions-grid label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.permissions-grid input {
  width: 16px;
  height: 16px;
}

.permissions-tree-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.perm-tree-column {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
  padding: 8px;
}

.perm-tree-title {
  font-size: 18px;
  font-weight: 800;
  color: var(--text);
}

.perm-node-header {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 30px;
  justify-content: space-between;
}

.perm-node-toggle,
.perm-node-toggle-spacer {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  line-height: 1;
  flex: 0 0 18px;
}

.perm-node-toggle {
  all: unset;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  border-radius: 3px;
}

.perm-node-toggle-spacer {
  background: transparent;
}

.perm-node-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-weight: 700;
  flex: 1;
}

.perm-node-checkbox {
  width: 16px;
  height: 16px;
  margin: 0;
}

.perm-node-body {
  display: none;
  margin-left: 6px;
}

.perm-node.open > .perm-node-body {
  display: block;
}

@media (max-width: 1200px) {
  .permissions-tree-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 720px) {
  .permissions-tree-grid {
    grid-template-columns: 1fr;
  }
}

.tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border-bottom: 1px solid var(--line);
  margin-bottom: 24px;
}

.subtabs,
.mqtt-tabs,
.advanced-tabs,
.advanced-mqtt-tabs,
.settings-tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 18px;
}

.tab,
.subtab,
.mqtt-tab,
.advanced-tab,
.advanced-mqtt-tab,
.settings-tab {
  border: 1px solid var(--line);
  border-bottom: 0;
  background: #eef1f4;
  color: var(--muted);
  padding: 16px 24px;
  font-size: 20px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.subtab,
.mqtt-tab,
.advanced-tab,
.advanced-mqtt-tab,
.settings-tab {
  font-size: 16px;
  padding: 12px 18px;
}

.tab.active,
.subtab.active,
.mqtt-tab.active,
.advanced-tab.active,
.advanced-mqtt-tab.active,
.settings-tab.active {
  background: var(--surface);
  color: var(--text);
}

.panel {
  display: none;
  background: var(--surface);
  border: 1px solid var(--line);
  padding: 24px;
}

.panel.active {
  display: block;
}

.connection-type,
.mqtt-panel,
.advanced-panel,
.advanced-mqtt-panel,
.settings-panel {
  display: none;
}

.connection-type.active,
.mqtt-panel.active,
.advanced-panel.active,
.advanced-mqtt-panel.active,
.settings-panel.active {
  display: block;
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  gap: 18px 24px;
}

.clients-form {
  display: grid;
  gap: 18px;
}

label {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 11px 12px;
  font: inherit;
  color: var(--text);
  background: #fff;
}

textarea {
  resize: vertical;
}

small {
  color: var(--muted);
  font-weight: 500;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
}

.checkbox input {
  width: 20px;
  height: 20px;
}

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

.vrm-section {
  display: grid;
  gap: 10px;
  color: var(--muted);
  font-weight: 700;
}

.vrm-add-block {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfd;
}

.vrm-add-block strong {
  font: inherit;
  font-weight: 800;
  color: var(--text);
}

.clients-divider {
  width: 100%;
  margin: 4px 0;
  border: 0;
  height: 3px;
  border-radius: 999px;
  background: linear-gradient(90deg, #2f7bff 0%, #1fbf8f 100%);
}

.vrm-existing-dropdown {
  display: grid;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  padding: 10px 12px;
}

.vrm-existing-dropdown summary {
  font: inherit;
  line-height: 1.2;
  cursor: pointer;
  font-weight: 800;
  color: var(--text);
}

.vrm-existing-dropdown[open] summary {
  margin-bottom: 4px;
}

.vrm-table {
  display: grid;
  gap: 10px;
}

.vrm-header,
.vrm-row {
  display: grid;
  grid-template-columns: minmax(180px, 0.7fr) minmax(220px, 1fr) 160px 44px;
  gap: 10px;
  align-items: end;
}

.vrm-header {
  color: var(--muted);
  font-weight: 800;
}

.vrm-row[hidden] {
  display: none !important;
}

.vrm-row label > span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.vrm-row button {
  width: 44px;
  height: 44px;
  padding: 0;
  background: var(--danger);
}

.vrm-row-new button {
  background: #eef5ff;
  color: #15396b;
  border: 1px solid #aec7e8;
}

.vrm-add-block .vrm-header {
  grid-template-columns: minmax(220px, 1fr) minmax(180px, 320px) 140px minmax(190px, 220px);
}

.vrm-row.vrm-row-new {
  grid-template-columns: minmax(220px, 1fr) minmax(180px, 320px) 140px minmax(190px, 220px);
  align-items: end;
}

.vrm-row.vrm-row-new .vrm-id {
  max-width: 320px;
}

.vrm-row.vrm-row-new .vrm-name,
.vrm-row.vrm-row-new .vrm-id,
.vrm-row.vrm-row-new .vrm-retention {
  height: 44px;
}

.vrm-row .vrm-retention {
  height: 44px;
}

.vrm-row.vrm-row-new button {
  width: 100%;
  min-width: 190px;
  height: 44px;
  white-space: nowrap;
  font-size: 16px;
  line-height: 1;
  background: var(--accent);
  color: #fff;
  border: 0;
}

.vrm-row.vrm-row-new button:hover {
  background: var(--accent-dark);
}

.actions,
.certificate-form {
  display: flex;
  align-items: end;
  gap: 12px;
  flex-wrap: wrap;
}

.notice {
  display: none;
  border-radius: 6px;
  padding: 12px 14px;
  font-weight: 800;
}

.notice.show {
  display: block;
}

.notice.success {
  background: #e8f6f0;
  color: #0c6b4c;
  border: 1px solid #8dd0b6;
}

.notice.warning {
  background: #fff4db;
  color: #8a5600;
  border: 1px solid #e3ba64;
}

.certificate-current {
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 14px;
  margin: 16px 0;
  background: #fbfcfd;
  color: var(--muted);
  font-weight: 700;
}

.certificate-current strong {
  color: var(--text);
  font-size: 18px;
}

.debug-summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin: 14px 0;
}

.debug-card {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 10px;
  display: grid;
  gap: 4px;
}

.debug-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.debug-card strong {
  color: var(--text);
  font-size: 15px;
  overflow-wrap: anywhere;
}

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

#mqttDebugInfo {
  color: var(--muted);
  font-weight: 700;
}

.debug-log {
  max-height: 460px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 8px;
  display: grid;
  gap: 6px;
}

.debug-event {
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #fbfcfd;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
}

.debug-event.warning {
  border-color: #e3ba64;
  background: #fff4db;
}

.debug-event.error {
  border-color: #d39a9a;
  background: #fff0f0;
}

.debug-event.success {
  border-color: #8dd0b6;
  background: #e8f6f0;
}

.user-log-entry {
  display: grid;
  gap: 6px;
}

.user-log-summary {
  display: grid;
  grid-template-columns: 230px 140px minmax(0, 1fr);
  gap: 10px;
}

.user-log-summary strong {
  overflow-wrap: anywhere;
}

.user-log-details {
  margin: 0;
  border: 1px dashed var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 8px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.user-log-dropdown summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  user-select: none;
}

.empty-state {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.messages-toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 360px) auto 1fr;
  gap: 12px;
  align-items: end;
  margin-bottom: 16px;
}

.advanced-form {
  display: grid;
  gap: 14px;
  margin-bottom: 20px;
}

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

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

.input-with-suffix span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.advanced-sources {
  display: grid;
  gap: 10px;
}

.advanced-source-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  background: #fbfcfd;
}

.advanced-source-row strong,
.advanced-source-row span {
  display: block;
}

.advanced-source-row span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.user-custom-editor {
  grid-column: 1 / -1;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f6f8fa;
  padding: 10px;
  display: grid;
  gap: 10px;
}

.user-custom-editor-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

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

.tree-toolbar button {
  padding: 9px 12px;
}

.logs-section-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f8fafc;
  padding: 12px;
  margin-bottom: 14px;
}

.logs-section-card .tree-toolbar {
  margin-bottom: 10px;
}

.logs-toolbar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.logs-users-card {
  border-left: 5px solid #0f766e;
}

.logs-system-card {
  border-left: 5px solid #1d4ed8;
}

#topicCount {
  color: var(--muted);
  font-weight: 700;
}

.connection-status-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.status-tile {
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  background: #fbfcfd;
}

.status-tile span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.status-tile strong {
  color: var(--text);
  font-size: 16px;
  overflow-wrap: anywhere;
}

.status-tile strong.debug-metric-good {
  color: #1e7a36;
}

.status-tile strong.debug-metric-warn {
  color: #a16207;
}

.status-tile strong.debug-metric-bad {
  color: #b42318;
}

.status-tile strong.debug-metric-na {
  color: var(--muted);
}

#messageCount {
  color: var(--muted);
  font-weight: 700;
  align-self: center;
}

.messages-list {
  display: grid;
  gap: 6px;
  max-height: 620px;
  overflow: auto;
}

.tree-node {
  border-left: 1px solid var(--line);
  margin-left: 10px;
  padding-left: 10px;
}

.tree-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 9px 10px;
  background: #fbfcfd;
  margin-bottom: 6px;
}

.tree-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  margin-right: 8px;
  border-radius: 4px;
  background: #e8edf1;
  color: var(--text);
  font-size: 12px;
}

.tree-label {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  color: var(--text);
  font-weight: 800;
  overflow-wrap: anywhere;
}

.tree-count,
.tree-date {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.tree-children {
  display: none;
}

.tree-node.open > .tree-children {
  display: block;
}

.tree-leaf {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  padding: 10px;
  margin-bottom: 6px;
}

.tree-leaf-header {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  margin-bottom: 8px;
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 8px;
}

.message-topic {
  overflow-wrap: anywhere;
}

.message-payload {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  padding: 12px 16px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

button:hover {
  background: var(--accent-dark);
}

button.secondary {
  background: #6b7680;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.status-ok {
  color: var(--accent);
}

.status-error {
  color: var(--danger);
}

@media (max-width: 720px) {
  .shell {
    width: min(100% - 20px, 1180px);
    margin: 12px auto;
  }

  .topbar,
  .actions,
  .certificate-form {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar-right {
    width: 100%;
    align-items: stretch;
    flex-direction: column;
  }

  .form-grid,
  .advanced-grid,
  .advanced-source-row,
  .messages-toolbar,
  .connection-status-grid,
  .debug-summary {
    grid-template-columns: 1fr;
  }

  .user-log-summary {
    grid-template-columns: 1fr;
  }

  .tabs,
  .subtabs,
  .mqtt-tabs,
  .advanced-tabs,
  .advanced-mqtt-tabs,
  .settings-tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 6px;
    gap: 8px;
  }

  .tab,
  .subtab,
  .mqtt-tab,
  .advanced-tab,
  .advanced-mqtt-tab,
  .settings-tab {
    flex: 0 0 auto;
    white-space: nowrap;
    min-width: max-content;
    padding: 10px 14px;
    font-size: 15px;
  }

  .vrm-header {
    display: none;
  }

  .vrm-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 10px;
    background: #fbfcfd;
  }

  .vrm-row label > span {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
    color: var(--muted);
    font-size: 13px;
    font-weight: 700;
  }

  .tree-leaf-header,
  .tree-row {
    grid-template-columns: 1fr;
  }

  .vrm-row button {
    width: 100%;
    height: 42px;
  }

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

  .vrm-row.vrm-row-new .vrm-id {
    max-width: 100%;
  }

  .messages-list {
    max-height: 52vh;
  }

  .tree-node {
    margin-left: 4px;
    padding-left: 6px;
  }

  .tree-row {
    gap: 8px;
    padding: 8px;
  }

  .tree-label {
    font-size: 13px;
    line-height: 1.3;
  }

  .tree-count,
  .tree-date {
    font-size: 11px;
    white-space: normal;
    overflow-wrap: anywhere;
  }

  .tree-toggle {
    width: 22px;
    height: 22px;
    margin-right: 6px;
  }

  .message-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .tab {
    font-size: 15px;
  }
}
