:root {
  --bg: #f3f0ea;
  --ink: #1d1a16;
  --muted: #6d655c;
  --card: #fffdf8;
  --line: #e2d9cc;
  --accent: #0f6b5c;
  --accent-ink: #f4fffb;
  --guest: #e8f3f0;
  --owner: #fff1df;
  --danger: #a33b2b;
  --emergency: #b45309;
  --shadow: 0 10px 30px rgba(40, 28, 12, 0.08);
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
}

body {
  background:
    radial-gradient(1200px 500px at 10% -10%, #ffe7c8 0%, transparent 55%),
    radial-gradient(900px 400px at 100% 0%, #cfeee6 0%, transparent 50%),
    var(--bg);
}

.app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 16px 14px 24px;
}

.app.chat-app {
  height: 100dvh;
  min-height: 100dvh;
  padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
  overflow: hidden;
}

.header {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.header h1 {
  margin: 0;
  font-size: 1.45rem;
  letter-spacing: 0.02em;
}

.header-sub {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 26px;
}

.header-sub p {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.3;
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.live-dot {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.68rem;
  color: var(--accent);
  letter-spacing: 0.02em;
}

.live-dot::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.6s ease-in-out infinite;
}

.live-dot.offline {
  color: var(--muted);
}

.live-dot.offline::before {
  background: var(--muted);
  animation: none;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.85); }
}

.toast {
  position: fixed;
  left: 50%;
  top: calc(12px + env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 50;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.88rem;
  font-weight: 600;
  box-shadow: 0 10px 28px rgba(15, 107, 92, 0.28);
  animation: toast-in 0.25s ease;
  max-width: calc(100% - 32px);
  text-align: center;
}

.toast.hidden { display: none; }

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, -8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.header-actions {
  display: flex;
  flex-wrap: nowrap;
  gap: 4px;
  justify-content: flex-end;
  align-items: center;
  flex-shrink: 0;
}

.header-actions .tiny {
  height: 24px;
  padding: 0 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.66rem;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 14px;
}

.chat-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin-bottom: 0;
  padding-bottom: 10px;
}

.nick-row {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  margin-bottom: 10px;
  flex-shrink: 0;
  align-items: center;
}

.nick-label {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
}

.nick-row input {
  height: 32px;
  padding: 0 8px;
  font-size: 0.82rem;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 80%, transparent);
  border-radius: 0;
}

.nick-row input::placeholder {
  color: color-mix(in srgb, var(--muted) 70%, white);
  font-size: 0.82rem;
}

.nick-row input:focus {
  border-color: color-mix(in srgb, var(--accent) 35%, var(--line));
  box-shadow: none;
  color: var(--ink);
}

input, textarea, button {
  font: inherit;
}

input, textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 12px;
  color: var(--ink);
  outline: none;
}

input:focus, textarea:focus {
  border-color: color-mix(in srgb, var(--accent) 45%, white);
}

textarea {
  min-height: 48px;
  max-height: 140px;
  resize: none;
}

button {
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 12px 14px;
  cursor: pointer;
}

button.secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 40%, white);
}

button.tiny {
  padding: 4px 8px;
  font-size: 0.68rem;
  line-height: 1.2;
  border-radius: 999px;
  white-space: nowrap;
}

button.tiny.emergency {
  color: var(--emergency);
  border-color: color-mix(in srgb, var(--emergency) 45%, white);
}

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

.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 2px 10px;
  overflow-y: auto;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}

.messages .empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.6;
  padding: 24px 12px;
}

.bubble {
  max-width: 88%;
  padding: 10px 12px;
  border-radius: 16px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble.fresh {
  animation: bubble-pop 0.45s ease;
}

@keyframes bubble-pop {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

.bubble.guest {
  align-self: flex-end;
  background: var(--guest);
  border-bottom-right-radius: 6px;
}

.bubble.owner {
  align-self: flex-start;
  background: var(--owner);
  border-bottom-left-radius: 6px;
}

.bubble.system {
  align-self: center;
  max-width: 92%;
  background: #f1efe8;
  color: var(--muted);
  border: 1px dashed var(--line);
  text-align: center;
  font-size: 0.92rem;
}

.meta {
  display: block;
  margin-top: 6px;
  font-size: 0.72rem;
  color: var(--muted);
  white-space: pre-line;
}

.composer-dock {
  flex-shrink: 0;
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  background: var(--card);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
}

.composer textarea {
  min-height: 40px;
  height: 40px;
  padding: 9px 12px;
  line-height: 1.35;
}

.composer #sendBtn {
  min-width: 64px;
  height: 40px;
  padding: 0 14px;
}

.hint, .error, .ok {
  font-size: 0.82rem;
  margin-top: 6px;
  min-height: 1.1em;
}

.hint { color: var(--muted); }
.error { color: var(--danger); }
.ok { color: var(--accent); }

.modal.hidden { display: none; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: end center;
  padding: 16px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(40, 28, 12, 0.35);
}

.modal-card {
  position: relative;
  width: min(420px, 100%);
  background: #fff;
  border-radius: 18px;
  padding: 18px 16px 16px;
  box-shadow: var(--shadow);
  margin-bottom: env(safe-area-inset-bottom);
}

.modal-card h2 {
  margin: 0 0 6px;
  font-size: 1.1rem;
}

.modal-card p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.45;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.emergency-card .contact-actions {
  display: grid;
  gap: 10px;
  margin: 4px 0 14px;
}

.contact-action {
  display: block;
  text-align: center;
  text-decoration: none;
  border-radius: 12px;
  padding: 13px 12px;
  font-weight: 600;
  color: #fff;
}

.contact-action.call {
  background: var(--accent);
}

.contact-action.sms {
  background: var(--emergency);
}

button.full {
  width: 100%;
}

.thread-list {
  display: grid;
  gap: 8px;
}

.thread-item {
  text-align: left;
  background: #fff;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px;
}

.thread-item strong { display: block; margin-bottom: 4px; }
.thread-item span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.admin-layout {
  display: grid;
  gap: 12px;
}

.admin-app {
  max-width: 1100px;
}

.admin-layout.two {
  align-items: start;
}

.admin-side,
.admin-chat {
  max-height: calc(100dvh - 120px);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.admin-side .thread-list {
  max-height: none;
}

.admin-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
  align-items: center;
}

.chip {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
  text-decoration: none;
}

.chip.active {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 40%, white);
  background: color-mix(in srgb, var(--accent) 8%, white);
}

button.chip,
.ghost-chip {
  cursor: pointer;
  font: inherit;
}

.admin-section-title {
  margin: 0 0 10px;
  font-size: 1rem;
}

.admin-reply {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
}

.admin-reply textarea {
  min-height: 40px;
  height: 40px;
  padding: 9px 12px;
}

.admin-reply button {
  height: 40px;
  min-width: 64px;
  padding: 0 14px;
}

.thread-item.active {
  border-color: color-mix(in srgb, var(--accent) 45%, white);
  background: color-mix(in srgb, var(--accent) 6%, white);
}

.view-log {
  display: grid;
  gap: 0;
  margin-top: 8px;
  max-height: 220px;
  overflow: auto;
  padding-right: 2px;
}

.view-log-full {
  max-height: calc(100dvh - 180px);
  gap: 10px;
}

.view-log-item {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 10px 2px;
  background: transparent;
  border: 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  border-radius: 0;
}

.view-log-item:last-child {
  border-bottom: 0;
}

.view-log-item strong {
  color: var(--ink);
  font-weight: 600;
}

.view-log-item small {
  display: block;
  margin-top: 2px;
  font-size: 0.72rem;
  color: var(--muted);
}

.visitor-group {
  padding: 10px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
}

.visitor-group:last-child {
  border-bottom: 0;
}

.visitor-group-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
}

.visitor-group-head strong {
  color: var(--ink);
  font-size: 0.92rem;
}

.visitor-group-head small {
  display: block;
  margin-top: 3px;
  font-size: 0.72rem;
  color: var(--muted);
}

.visitor-group-head > span {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}

.visitor-group-times {
  margin-top: 8px;
  padding-left: 2px;
  display: grid;
  gap: 4px;
}

.visitor-time-row {
  font-size: 0.78rem;
  color: var(--muted);
  padding: 2px 0 2px 10px;
  border-left: 2px solid color-mix(in srgb, var(--accent) 25%, var(--line));
}

.admin-settings .settings-meta {
  margin-top: 18px;
  display: grid;
  gap: 10px;
}

.admin-settings .settings-meta p {
  margin: 0;
  display: grid;
  gap: 4px;
}

.admin-settings .settings-meta span {
  font-size: 0.78rem;
  color: var(--muted);
}

.admin-settings .settings-meta code {
  font-size: 0.78rem;
  word-break: break-all;
  background: #f7f4ee;
  padding: 8px 10px;
  border-radius: 10px;
}

.nick-login {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.admin-chat .messages {
  max-height: none;
  flex: 1;
  min-height: 200px;
  max-height: calc(100dvh - 280px);
}

/* Admin: my (owner) replies on the right; visitor on the left. */
.admin-app .bubble.owner {
  align-self: flex-end;
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 6px;
}

.admin-app .bubble.guest {
  align-self: flex-start;
  border-bottom-right-radius: 16px;
  border-bottom-left-radius: 6px;
}

@media (min-width: 860px) {
  .admin-layout.two {
    grid-template-columns: 320px 1fr;
  }
  .modal {
    place-items: center;
  }
}
