:root {
  --bg: #f3efe5;
  --bg-accent: #e5efe9;
  --card: #fffdf7;
  --text: #1f2b24;
  --muted: #55635c;
  --border: #cad4ca;
  --accent: #0b7d5d;
  --accent-hover: #085d45;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  color: var(--text);
  font-family: "Space Grotesk", "Avenir Next", "Segoe UI", sans-serif;
  background: linear-gradient(140deg, var(--bg) 10%, var(--bg-accent) 90%);
  min-height: 100vh;
}

.topbar {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 253, 247, 0.86);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.requires-login .nav {
  display: none;
}

h1 {
  margin: 0;
  font-size: 1.55rem;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
}

.nav {
  display: flex;
  gap: 8px;
}

.nav a {
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
}

.nav a.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.container {
  max-width: 860px;
  margin: 0 auto;
  padding: 18px;
}

.page {
  display: grid;
  gap: 12px;
}

.hidden {
  display: none;
}

[hidden] {
  display: none !important;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
}

.login-card {
  max-width: 480px;
  margin: 48px auto 0;
}

h2 {
  margin: 0 0 10px;
  font-size: 1rem;
}

label {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
}

input,
select,
button {
  width: 100%;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 1rem;
  font-family: inherit;
}

button {
  margin-top: 10px;
  cursor: pointer;
  color: white;
  background: var(--accent);
  border: none;
  font-weight: 600;
}

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

button.danger {
  background: #b9382a;
}

button.danger:hover {
  background: #92291d;
}

button:disabled {
  background: #9fb8ad;
  cursor: not-allowed;
}

.controls {
  display: grid;
  gap: 8px;
}

.controls label {
  margin-bottom: 0;
}

.controls-row {
  display: grid;
  gap: 10px;
  align-items: center;
}

.controls-row button {
  margin-top: 0;
}

.list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
  display: grid;
  gap: 8px;
}

.list li {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  background: #f8fbf7;
}

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

.device-meta {
  display: grid;
  gap: 4px;
}

.device-line {
  line-height: 1.4;
}

.device-state {
  color: var(--muted);
  font-size: 0.92rem;
}

.device-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.device-delete,
.device-import,
.device-export {
  width: auto;
  min-width: 80px;
  margin-top: 0;
  padding: 8px 10px;
}

.device-import {
  background: #46685f;
}

.device-import:hover {
  background: #335049;
}

.device-export {
  background: #2a7a94;
}

.device-export:hover {
  background: #1f6278;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-weight: 600;
}

.checkbox-label input {
  width: 16px;
  height: 16px;
  margin: 0;
  padding: 0;
}

.push-toggle {
  align-items: center;
  font-size: 1rem;
}

.message-actions {
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.message-actions button {
  width: auto;
  min-width: 150px;
  margin-top: 0;
}

.message-item-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  position: relative;
}

.message-item-header .checkbox-label {
  font-weight: 700;
}

.message-select-label {
  padding-right: 68px;
}

.message-card {
  display: grid;
  gap: 6px;
}

.message-sender {
  display: block;
  color: var(--text);
  line-height: 1.3;
}

.message-menu {
  position: absolute;
  top: -2px;
  right: -2px;
}

.message-menu[open] {
  z-index: 2;
}

.message-menu-toggle {
  list-style: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  background: #edf5f0;
  color: var(--muted);
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}

.message-menu-toggle::-webkit-details-marker {
  display: none;
}

.message-menu-panel {
  margin-top: 6px;
  min-width: 112px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fffdf7;
  box-shadow: 0 10px 24px rgba(31, 43, 36, 0.12);
  display: grid;
  gap: 6px;
}

.message-copy,
.message-delete {
  width: auto;
  margin-top: 0;
  min-width: 0;
  padding: 8px 10px;
}

.message-copy {
  background: #46685f;
}

.message-copy:hover {
  background: #335049;
}

.message-meta {
  color: var(--muted);
  font-size: 0.86rem;
}

.message-body-box {
  border: 1px solid #d6e1d7;
  border-radius: 12px;
  background: #fdfcf8;
  padding: 10px 12px;
}

#messages-list li {
  padding: 9px 10px;
}

#messages-list .message-body-box p {
  margin: 0;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

.status-card pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

.hint {
  color: var(--muted);
  margin: 8px 0 0;
}

.push-help {
  margin-top: 10px;
}

.hint-list {
  margin: 6px 0 0;
  padding-left: 18px;
  color: var(--muted);
  display: grid;
  gap: 4px;
}

.qr-preview {
  margin-top: 12px;
  display: grid;
  justify-items: start;
  gap: 8px;
}

.qr-preview img {
  width: 220px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
  padding: 8px;
}

@media (min-width: 640px) {
  .controls-row {
    grid-template-columns: 1fr auto;
  }

  .controls-row button {
    width: auto;
    min-width: 170px;
    height: 100%;
    min-height: 44px;
  }
}

@media (max-width: 639px) {
  .container {
    padding: 12px;
  }

  .card {
    padding: 12px;
    border-radius: 12px;
  }

  .message-actions {
    margin-top: 10px;
    gap: 8px;
  }

  .message-actions button {
    min-width: 0;
    padding: 9px 10px;
  }

  .message-select-label {
    padding-right: 56px;
  }

  .message-menu {
    top: -4px;
    right: -4px;
  }

  .message-menu-toggle {
    padding: 3px 9px;
    font-size: 0.92rem;
  }

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

  .device-actions {
    width: 100%;
    flex-direction: column;
    align-items: stretch;
  }

  .device-delete,
  .device-import,
  .device-export {
    width: 100%;
  }

  #messages-list {
    gap: 6px;
  }

  #messages-list li {
    padding: 8px;
    border-radius: 9px;
  }

  .message-card {
    gap: 5px;
  }

  .message-meta {
    font-size: 0.8rem;
  }

  .message-body-box {
    padding: 8px 10px;
    border-radius: 10px;
  }
}
