:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --primary: #1a73e8;
  --primary-dark: #1557b0;
  --text: #1f1f1f;
  --muted: #5f6368;
  --border: #dadce0;
  --mine: #d3e3fd;
  --theirs: #ffffff;
  --danger: #d93025;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body {
  overscroll-behavior: none;
}

.hidden {
  display: none !important;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 480px;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: 0 0 24px rgba(0, 0, 0, 0.08);
}

.top-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  background: var(--primary);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 10;
}

.top-bar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  border: none;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.icon-svg {
  display: block;
  pointer-events: none;
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.35);
}

#main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.auth-tabs {
  display: flex;
  margin: 16px;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
}

.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px;
  border-radius: 8px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.auth-form {
  padding: 16px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.4;
}

.admin-tabs {
  display: flex;
  margin: 12px 16px 0;
  background: var(--bg);
  border-radius: 10px;
  padding: 4px;
  flex-shrink: 0;
}

.admin-panel {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0 16px 16px;
}

.admin-form {
  background: var(--bg);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.admin-form h3,
.admin-detail h3,
.admin-assign h4 {
  margin: 0 0 8px;
  font-size: 1rem;
}

.admin-form label,
.admin-inline-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}

.admin-form input,
.admin-form select,
.admin-inline-form select {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
}

.admin-inline-form {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-bottom: 10px;
}

.admin-inline-form select {
  flex: 1;
}

.admin-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.admin-list.compact .admin-row {
  padding: 10px 0;
}

.admin-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.admin-row-main {
  flex: 1;
  min-width: 0;
}

.admin-row-main strong {
  display: block;
}

.admin-row-main span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
}

.admin-row .btn {
  padding: 8px 10px;
  font-size: 0.85rem;
}

.admin-row-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.admin-form .hint {
  font-weight: normal;
  color: var(--muted);
  font-size: 0.8rem;
}

.btn.danger {
  background: #fce8e6;
  color: #c5221f;
}

.btn.danger:active {
  background: #f5c6c3;
}

.modal-form {
  margin-bottom: 0;
}

.modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
}

.modal-actions .btn {
  flex: 1;
}

.admin-detail {
  background: var(--bg);
  border-radius: 10px;
  padding: 14px;
  margin-top: 12px;
}

.admin-assign {
  margin-top: 14px;
}

.admin-fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  margin: 0;
}

.admin-fieldset legend {
  font-size: 0.85rem;
  color: var(--muted);
  padding: 0 4px;
}

.admin-checkbox {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text);
  margin-bottom: 8px;
}

.admin-checkbox:last-child {
  margin-bottom: 0;
}

.admin-checkbox input {
  width: auto;
  margin-top: 3px;
  flex-shrink: 0;
}

.admin-all-read-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 12px;
  margin-bottom: 8px;
}

.admin-checkbox-inline {
  margin-bottom: 0;
}

.admin-all-read-delay {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--text);
}

.admin-all-read-delay select {
  width: auto;
  min-width: 4.5rem;
  padding: 4px 8px;
}

.admin-all-read-delay select:disabled {
  opacity: 0.5;
}

.room-policy-tag {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  background: var(--bg);
  color: var(--muted);
  padding: 2px 5px;
  border-radius: 4px;
  margin-right: 4px;
}

.role-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  background: var(--primary);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
}

.role-badge.admin {
  background: #137333;
}

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}

.auth-form input {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
}

.btn {
  border: none;
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

.btn.primary {
  background: var(--primary);
  color: #fff;
}

.btn.primary:active {
  background: var(--primary-dark);
}

.btn.secondary {
  background: var(--bg);
  color: var(--primary);
}

.error {
  color: var(--danger);
  text-align: center;
  padding: 0 16px;
  font-size: 0.9rem;
}

.rooms-toolbar {
  padding: 12px 16px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.ios-banner,
.push-banner {
  margin: 12px 16px 0;
  padding: 12px 14px;
  border-radius: 10px;
  font-size: 0.9rem;
  line-height: 1.45;
}

.ios-banner {
  background: #fef7e0;
  border: 1px solid #f9ab00;
  color: #3c4043;
}

.ios-banner strong {
  display: block;
  margin-bottom: 6px;
}

.push-banner {
  background: #e8f0fe;
  border: 1px solid #1a73e8;
  color: #174ea6;
}

.push-banner.warn {
  background: #fce8e6;
  border-color: #d93025;
  color: #a50e0e;
}

.push-banner.ok {
  background: #e6f4ea;
  border-color: #137333;
  color: #137333;
}

.room-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.room-item:active {
  background: var(--bg);
}

.room-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}

.room-meta {
  flex: 1;
  min-width: 0;
}

.room-meta strong {
  display: block;
  font-size: 1rem;
}

.room-meta span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  background: var(--primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  min-width: 22px;
  height: 22px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

#chat-view {
  background: var(--bg);
}

.message-list {
  list-style: none;
  margin: 0;
  padding: 12px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.message {
  max-width: 85%;
  padding: 10px 12px;
  border-radius: 14px;
  font-size: 0.95rem;
  line-height: 1.4;
  word-wrap: break-word;
}

.message.mine {
  align-self: flex-end;
  background: var(--mine);
  border-bottom-right-radius: 4px;
}

.message.theirs {
  align-self: flex-start;
  background: var(--theirs);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.message .sender {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 4px;
}

.message.all-read {
  background: #c8e6c9 !important;
  border-color: #81c784 !important;
}

.message.mine.all-read {
  background: #c8e6c9 !important;
}

.message-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 6px;
  flex-wrap: wrap;
}

.read-receipts {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  justify-content: flex-end;
}

.read-initial {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 4px;
  border-radius: 10px;
  background: rgba(26, 115, 232, 0.15);
  color: var(--primary);
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1;
}

.message.all-read .read-initial {
  background: rgba(19, 115, 51, 0.2);
  color: #137333;
}

.message .time {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 0;
  text-align: right;
  flex-shrink: 0;
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 12px 16px;
  font-size: 1rem;
}

.send-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  padding: 0;
  flex-shrink: 0;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  background: #323232;
  color: #fff;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 0.9rem;
  z-index: 100;
  max-width: 90%;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 200;
}

.modal-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 20px;
  max-width: 360px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-card h2 {
  margin: 0 0 10px;
  font-size: 1.15rem;
}

.modal-lead {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.45;
}

.modal-steps {
  margin: 0 0 14px;
  padding-left: 20px;
  font-size: 0.95rem;
  line-height: 1.55;
}

.modal-steps li {
  margin-bottom: 8px;
}

.modal-note {
  margin: 0 0 16px;
  font-size: 0.85rem;
  color: var(--muted);
}

.modal-close-btn {
  width: 100%;
}

@media (min-width: 481px) {
  body {
    padding: 16px 0;
    background: #e8eaed;
  }
}
