*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-app);
  color: var(--text-primary);
  font-family: var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* Buttons & Inputs */
button,
input,
select {
  font-family: inherit;
  color: inherit;
}

input,
select {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 16px; /* Prevents iOS Safari zoom on focus */
  outline: none;
  transition: border-color 0.15s ease;
}

input:focus,
select:focus {
  border-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}

.btn-ghost:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 0.82rem;
}

.btn-full {
  width: 100%;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 1.05rem;
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Auth View */
.auth-view {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: var(--bg-app);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 26px;
}

.auth-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
}

.auth-header h1 {
  margin: 0;
  font-size: 1.35rem;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 0.86rem;
  color: var(--text-muted);
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md);
  background: var(--accent);
  color: var(--accent-contrast);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.3rem;
}

.brand-mark.sm {
  width: 28px;
  height: 28px;
  font-size: 1rem;
  border-radius: var(--radius-sm);
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--bg-app);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 18px;
}

.auth-tab {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 8px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
}

.auth-tab.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.auth-form,
.modal-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.86rem;
  color: var(--text-secondary);
}

.form-error {
  color: var(--danger);
  font-size: 0.86rem;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}

.auth-footer {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.theme-picker-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.84rem;
  color: var(--text-muted);
}

.theme-select {
  padding: 5px 8px;
  font-size: 0.84rem;
}

/* Main App Shell */
.app-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 240px;
  height: 100dvh;
  overflow: hidden;
  position: relative;
}

.sidebar {
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
}

.sidebar-right {
  border-right: none;
  border-left: 1px solid var(--border);
}

.sidebar-header {
  height: 56px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand-inline {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
}

.status-dot.online {
  background: var(--success);
}

.status-dot.offline {
  background: var(--warning);
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px;
}

.sidebar-section-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 10px 8px 6px;
  font-weight: 700;
}

.channel-list,
.member-list,
.bans-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.channel-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 0.92rem;
  user-select: none;
}

.channel-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.channel-item.active {
  background: var(--bg-active);
  color: var(--text-primary);
  font-weight: 600;
}

.channel-item-left {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.channel-hash {
  color: var(--text-muted);
  font-weight: 700;
}

.channel-name-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 0.72rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 999px;
  flex-shrink: 0;
}

.unread-badge.mention {
  background: var(--danger);
  color: #fff;
}

.join-pill {
  font-size: 0.74rem;
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 7px;
  flex-shrink: 0;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  flex-shrink: 0;
}

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.88rem;
  flex-shrink: 0;
  text-transform: uppercase;
}

.user-meta {
  min-width: 0;
  flex: 1;
}

.user-name {
  font-weight: 600;
  font-size: 0.9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-status {
  font-size: 0.76rem;
  color: var(--success);
}

.theme-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* Chat Main */
.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg-app);
}

.chat-header {
  height: 56px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.channel-heading {
  min-width: 0;
  flex: 1;
}

.channel-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.channel-heading h2 {
  margin: 0;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.role-pill {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: var(--radius-sm);
  background: var(--bg-active);
  color: var(--accent);
}

.channel-topic {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* Messages */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.load-older {
  align-self: center;
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: var(--msg-gap);
}

.msg-row {
  display: flex;
  gap: 10px;
  padding: var(--msg-padding);
  border-radius: var(--radius-sm);
  font-size: var(--msg-font-size);
  line-height: 1.45;
  word-break: break-word;
}

.msg-row:hover {
  background: var(--bg-hover);
}

.msg-row.mention {
  background: var(--bg-mention);
  border-left: 3px solid var(--warning);
}

.msg-avatar {
  display: var(--avatar-display);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.82rem;
  color: #fff;
  flex-shrink: 0;
  margin-top: 2px;
}

.msg-body {
  min-width: 0;
  flex: 1;
}

.msg-meta {
  display: flex;
  align-items: baseline;
  gap: 7px;
  flex-wrap: wrap;
}

.msg-author {
  font-weight: 700;
  font-size: 0.92rem;
}

.msg-role-badge {
  font-size: 0.72rem;
  font-family: var(--font-mono);
  color: var(--accent);
  font-weight: 700;
}

.msg-time {
  font-size: 0.74rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.msg-content {
  margin-top: 2px;
  white-space: pre-wrap;
  color: var(--text-primary);
}

.msg-system {
  font-size: 0.82rem;
  color: var(--text-muted);
  padding: 4px 10px;
  font-style: italic;
}

.msg-action .msg-content {
  font-style: italic;
  color: var(--text-secondary);
}

/* IRC Classic inline message layout */
[data-theme="irc-classic"] .msg-body {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

[data-theme="irc-classic"] .msg-content {
  margin-top: 0;
}

.typing-bar {
  min-height: 22px;
  padding: 0 16px;
  font-size: 0.78rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.composer {
  padding: 10px 14px 14px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  background: var(--bg-app);
}

.composer input {
  flex: 1;
  min-width: 0;
}

/* Members */
.member-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
}

.member-item:hover {
  background: var(--bg-hover);
}

.member-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.member-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.member-dot.online {
  background: var(--success);
}

.member-dot.offline {
  background: var(--text-muted);
  opacity: 0.5;
}

.member-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.member-item.offline .member-name {
  color: var(--text-muted);
}

.member-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.mini-btn {
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  font-size: 0.72rem;
  cursor: pointer;
}

.mini-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.mini-btn.danger:hover {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.modal-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  margin: 0;
  font-size: 1rem;
}

.modal-body {
  padding: 16px;
}

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

.ban-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.86rem;
}

/* Toast */
.toast-container {
  position: fixed;
  bottom: 74px;
  right: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
  max-width: min(360px, calc(100vw - 32px));
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  font-size: 0.86rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

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

/* Responsive drawers */
.mobile-only {
  display: none;
}

.tablet-only {
  display: none;
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 25;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 240px minmax(0, 1fr);
  }

  .tablet-only {
    display: inline-flex;
  }

  .sidebar-right {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(290px, 84vw);
    z-index: 30;
    transform: translateX(100%);
    transition: transform 0.2s ease;
  }

  .sidebar-right.open {
    transform: translateX(0);
  }
}

@media (max-width: 700px) {
  .app-shell {
    grid-template-columns: minmax(0, 1fr);
  }

  .mobile-only {
    display: inline-flex;
  }

  .sidebar-left {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(290px, 84vw);
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }

  .sidebar-left.open {
    transform: translateX(0);
  }

  .messages-container {
    padding: 10px 12px;
  }

  .composer {
    padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  }
}
