:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-soft: #f1f5f4;
  --text: #1f2933;
  --muted: #6b7684;
  --border: #d9e0e7;
  --primary: #0f8f72;
  --primary-dark: #0a6f5a;
  --danger: #b42318;
  --warning: #b54708;
  --online: #047857;
  --sidebar: #0f1f1d;
  --sidebar-soft: #18302c;
  --sidebar-text: #edf7f5;
  --sidebar-muted: #9db6b0;
  --shadow: 0 16px 40px rgba(22, 34, 51, 0.08);
}

* {
  box-sizing: border-box;
}

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

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  padding: 0 14px;
}

button:hover {
  border-color: #a8b4bf;
}

button.primary {
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
}

button.primary:hover {
  border-color: var(--primary-dark);
  background: var(--primary-dark);
}

button.danger {
  color: var(--danger);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  padding: 0 12px;
}

input,
select {
  min-height: 40px;
}

textarea {
  min-height: 120px;
  padding: 12px;
  resize: vertical;
}

label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

.app-shell {
  display: grid;
  min-height: 100vh;
  grid-template-columns: 292px 1fr;
}

.login-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.login-card {
  display: grid;
  width: min(100%, 420px);
  gap: 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-brand {
  margin-bottom: 10px;
}

.login-error {
  min-height: 20px;
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  height: 100vh;
  flex-direction: column;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
  background: var(--sidebar);
  color: var(--sidebar-text);
  padding: 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 36px;
}

.brand-mark {
  display: grid;
  width: 46px;
  height: 46px;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 12px;
  background: #12a783;
  color: #ffffff;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.22);
  font-weight: 800;
}

.brand h1,
.toolbar h2,
.panel h3,
.dialog-header h3 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 19px;
}

.sidebar .brand h1 {
  color: var(--sidebar-text);
}

.brand p,
.toolbar p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.sidebar .brand p {
  color: var(--sidebar-muted);
}

.sidebar-nav {
  display: grid;
  gap: 6px;
  margin-bottom: auto;
}

.nav-link {
  display: flex;
  min-height: 46px;
  align-items: center;
  gap: 12px;
  border: 1px solid rgba(255, 255, 255, 0);
  border-radius: 12px;
  color: var(--sidebar-muted);
  font-weight: 700;
  padding: 0 12px;
  text-decoration: none;
  transition:
    background 140ms ease,
    border-color 140ms ease,
    color 140ms ease,
    transform 140ms ease;
}

.nav-link span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-link:hover {
  border-color: rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.06);
  color: var(--sidebar-text);
}

.nav-link.active {
  border-color: rgba(34, 197, 154, 0.34);
  background: var(--sidebar-soft);
  color: #ffffff;
  box-shadow: inset 3px 0 0 #22c59a;
}

.nav-icon {
  display: grid;
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  place-items: center;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: currentColor;
}

.nav-link.active .nav-icon {
  background: rgba(34, 197, 154, 0.18);
  color: #69e5c2;
}

.nav-icon svg,
.logout-button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.admin-session {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 24px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  padding: 14px;
}

.admin-session span {
  display: block;
  color: var(--sidebar-text);
  font-weight: 800;
}

.admin-session small {
  color: var(--sidebar-muted);
  font-weight: 600;
}

.logout-button {
  display: inline-flex;
  min-height: 36px;
  align-items: center;
  gap: 8px;
  border-color: rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.07);
  color: var(--sidebar-text);
  padding: 0 10px;
}

.logout-button:hover {
  border-color: rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.12);
}

.input-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.dashboard-summary {
  margin-bottom: 18px;
}

.summary div {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.68);
  padding: 14px;
}

.summary span {
  display: block;
  font-size: 28px;
  font-weight: 800;
}

.summary small {
  color: var(--muted);
  font-weight: 600;
}

.workspace {
  padding: 28px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.toolbar h2 {
  font-size: 28px;
}

.toolbar-actions {
  display: flex;
  gap: 10px;
}

.page-section {
  display: block;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(420px, 1.2fr) minmax(320px, 0.8fr);
  gap: 18px;
}

.panel {
  min-height: 520px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel-header {
  display: flex;
  min-height: 64px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding: 0 18px;
}

.panel h3 {
  font-size: 16px;
}

.device-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.device-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  padding: 14px;
}

.device-item.selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(15, 143, 114, 0.12);
}

.device-top {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
}

.device-name {
  margin: 0 0 6px;
  font-size: 15px;
  font-weight: 750;
}

.device-meta {
  display: grid;
  gap: 3px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.device-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.device-actions button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.status-pill {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  border-radius: 999px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 700;
}

.status-pill.open {
  background: #dff8ed;
  color: var(--online);
}

.status-pill.qr,
.status-pill.connecting {
  background: #fff2d6;
  color: var(--warning);
}

.status-pill.closed,
.status-pill.logged_out,
.status-pill.replaced,
.status-pill.failed {
  background: #ffe4e2;
  color: var(--danger);
}

.status-pill.sent {
  background: #dff8ed;
  color: var(--online);
}

.status-pill.muted,
.status-pill.created {
  background: var(--surface-soft);
  color: var(--muted);
}

.empty-state {
  display: grid;
  min-height: 430px;
  place-items: center;
  color: var(--muted);
  padding: 24px;
  text-align: center;
}

.qr-box {
  display: grid;
  gap: 16px;
  justify-items: center;
  padding: 24px;
}

.qr-frame {
  display: grid;
  width: min(100%, 380px);
  aspect-ratio: 1;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
}

.qr-frame img {
  width: 92%;
  height: 92%;
  object-fit: contain;
}

.online-state {
  display: grid;
  width: min(100%, 380px);
  min-height: 300px;
  align-content: center;
  justify-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f7fbfa;
  padding: 28px;
  text-align: center;
}

.online-mark {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 999px;
  background: #dff8ed;
}

.online-mark::after {
  position: absolute;
  top: 18px;
  left: 16px;
  width: 28px;
  height: 14px;
  border-bottom: 5px solid var(--online);
  border-left: 5px solid var(--online);
  content: "";
  transform: rotate(-45deg);
}

.online-state strong {
  font-size: 20px;
}

.online-state span {
  color: var(--muted);
  font-size: 14px;
}

.qr-meta {
  display: grid;
  width: 100%;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  overflow-wrap: anywhere;
}

.qr-meta strong {
  color: var(--text);
  font-size: 15px;
}

.token-box {
  border-top: 1px solid var(--border);
  padding: 18px;
}

.token-box input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

.send-layout {
  display: grid;
  grid-template-columns: minmax(420px, 1.15fr) minmax(320px, 0.85fr);
  gap: 18px;
}

.send-panel,
.contacts-panel {
  min-height: 0;
  margin-top: 0;
}

.history-panel {
  min-height: 0;
  margin-top: 0;
}

.history-header {
  align-items: center;
}

.history-controls {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) 140px auto auto;
  gap: 8px;
  width: min(100%, 720px);
}

.history-controls select,
.history-controls button {
  min-height: 36px;
}

.history-delete-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border-color: #f4c7c3;
  background: #fff7f6;
  white-space: nowrap;
}

.history-delete-button:hover {
  border-color: #e59b95;
  background: #fff0ee;
}

.history-delete-button svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.send-form {
  display: grid;
  gap: 16px;
  padding: 18px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(180px, 1fr));
  gap: 16px;
}

.contact-form {
  display: grid;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  padding: 18px;
}

.contacts-header button {
  min-height: 34px;
}

.contact-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.contact-empty {
  min-height: 120px;
}

.contact-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  padding: 12px;
}

.contact-name {
  margin: 0 0 4px;
  font-weight: 750;
}

.contact-phone {
  color: var(--muted);
  font-size: 13px;
}

.contact-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.contact-actions button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.send-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#send-result {
  min-height: 20px;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.history-list {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.history-empty {
  min-height: 160px;
}

.history-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  padding: 14px;
}

.history-top {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
}

.history-recipient {
  margin: 0 0 6px;
  font-size: 15px;
  font-weight: 750;
}

.history-message {
  margin: 12px 0;
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.history-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.docs-layout {
  display: grid;
  gap: 16px;
}

.doc-card {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 18px;
}

.doc-intro {
  display: grid;
  gap: 14px;
}

.doc-card h3,
.doc-card p {
  margin: 0;
}

.doc-card p {
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.doc-card code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.doc-card pre {
  margin: 14px 0 0;
  max-width: 100%;
  overflow: auto;
  border: 1px solid #243b35;
  border-radius: 8px;
  background: #0e1f1c;
  color: #dff8ed;
  padding: 16px;
  font-size: 13px;
  line-height: 1.55;
}

.doc-heading {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.method {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  border-radius: 999px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 800;
}

.method.get {
  background: #e8f2ff;
  color: #175cd3;
}

.method.post {
  background: #dff8ed;
  color: var(--online);
}

.method.delete {
  background: #fff0ee;
  color: var(--danger);
}

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

.doc-meta-grid div {
  display: grid;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f7fbfa;
  padding: 12px;
}

.doc-meta-grid span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.doc-meta-grid code {
  color: var(--text);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.hidden {
  display: none !important;
}

dialog {
  width: min(440px, calc(100vw - 32px));
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(22, 34, 51, 0.34);
}

dialog form {
  padding: 18px;
}

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

#close-dialog-button {
  min-width: 40px;
  padding: 0;
  font-size: 24px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

@media (max-width: 1180px) {
  .app-shell,
  .content-grid,
  .send-layout,
  .form-grid,
  .history-controls,
  .doc-meta-grid {
    grid-template-columns: 1fr;
  }

  .app-shell {
    display: block;
  }

  .history-header {
    align-items: stretch;
    flex-direction: column;
  }

  .history-controls {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .history-controls select {
    grid-column: 1 / -1;
  }

  .history-controls button {
    width: 100%;
  }

  .sidebar {
    position: static;
    display: flex;
    height: 82px;
    align-items: center;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 12px;
    border-right: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    padding: 12px 16px;
  }

  .brand,
  .sidebar-nav,
  .admin-session {
    margin-top: 0;
    margin-bottom: 0;
  }

  .sidebar .brand {
    flex: 1 1 auto;
    gap: 10px;
  }

  .sidebar .brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 10px;
  }

  .sidebar .brand h1 {
    font-size: 18px;
  }

  .sidebar .brand p {
    font-size: 12px;
  }

  .sidebar-nav {
    display: grid;
    position: fixed;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 40;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(15, 31, 29, 0.98);
    box-shadow: 0 -16px 34px rgba(15, 31, 29, 0.2);
    overflow: hidden;
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }

  .nav-link {
    min-height: 58px;
    min-width: 0;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    border-radius: 10px;
    padding: 6px 4px;
    font-size: 11px;
    text-align: center;
  }

  .nav-link span:last-child {
    max-width: 100%;
  }

  .nav-link.active {
    box-shadow: none;
  }

  .nav-icon {
    width: 26px;
    height: 26px;
    flex-basis: 26px;
    border-radius: 9px;
  }

  .admin-session {
    width: auto;
    flex: 0 0 auto;
    margin-top: 0;
    margin-left: auto;
    border-radius: 12px;
    padding: 10px;
  }

  .admin-session span {
    font-size: 14px;
  }

  .admin-session small {
    font-size: 12px;
  }

  .logout-button {
    min-height: 34px;
    font-size: 13px;
  }

  .workspace {
    padding: 20px 20px 96px;
  }

  .toolbar {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 16px;
  }

  .toolbar h2 {
    font-size: 25px;
  }

  .toolbar p {
    font-size: 13px;
  }

  .toolbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .panel {
    min-height: 0;
  }

  .empty-state {
    min-height: 240px;
  }
}

@media (max-width: 760px) {
  .sidebar-nav {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }

  .nav-link {
    min-height: 48px;
    justify-content: center;
    gap: 0;
    text-align: center;
  }

  .nav-link span:last-child {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .nav-icon {
    width: 30px;
    height: 30px;
    flex-basis: 30px;
  }

  .nav-link.active {
    box-shadow: none;
  }

  .workspace {
    padding-bottom: 84px;
  }
}

@media (max-width: 560px) {
  .workspace {
    padding: 16px 16px 82px;
  }

  .input-row,
  .toolbar-actions,
  .send-actions {
    grid-template-columns: 1fr;
  }

  .sidebar {
    align-items: center;
    height: 76px;
    padding: 10px 16px;
  }

  .sidebar .brand {
    align-items: center;
  }

  .sidebar .brand-mark {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  .sidebar .brand h1 {
    font-size: 17px;
  }

  .nav-link {
    min-width: 0;
    min-height: 48px;
    padding: 6px 3px;
  }

  .nav-link span:last-child {
    max-width: 1px;
  }

  .admin-session {
    width: auto;
    gap: 10px;
    padding: 4px;
  }

  .admin-session div {
    display: none;
  }

  .logout-button {
    padding: 0 10px;
  }

  .toolbar h2 {
    font-size: 24px;
  }

  .panel-header {
    min-height: 0;
    align-items: stretch;
    flex-direction: column;
    padding: 14px;
  }

  .history-controls {
    grid-template-columns: 1fr 1fr;
  }

  .history-controls select {
    grid-column: 1 / -1;
  }

  .history-controls button {
    width: 100%;
  }

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

  .summary span {
    font-size: 24px;
  }

  .device-list,
  .history-list,
  .contact-list {
    padding: 12px;
  }

  .send-form,
  .contact-form,
  .token-box {
    padding: 14px;
  }

  .send-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .device-top {
    display: grid;
  }

  .device-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .device-actions button {
    width: 100%;
  }

  .contact-item {
    align-items: stretch;
    flex-direction: column;
  }

  .contact-actions {
    justify-content: stretch;
  }

  .contact-actions button {
    flex: 1;
  }

  .doc-card {
    padding: 14px;
  }

  .doc-heading {
    align-items: flex-start;
    flex-direction: column;
  }

  .doc-card pre {
    padding: 12px;
    font-size: 12px;
  }
}
