:root {
  --line-bg: #7494C0;
  --me-bubble: #8DE055;
  --member-bubble: #FFFFFF;
  --header-bg: #273246;
  --sidebar-bg: #FFFFFF;
  --border-color: #E5E7EB;
  --text-main: #1F2937;
  --text-sub: #6B7280;
  --accent: #06C755;
  --danger: #EF4444;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

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

html,
body {
  height: 100%;
  font-family: "Noto Sans JP", "Noto Sans CJK JP", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  color: var(--text-main);
  background: var(--header-bg);
  overflow: hidden;
  overscroll-behavior: none;
}

.app {
  display: flex;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  background: #F3F4F6;
}

/* Sidebar */
.sidebar {
  width: 340px;
  min-width: 300px;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 10;
}

.sidebar-header {
  background: var(--header-bg);
  color: #FFF;
  padding: calc(12px + var(--safe-top)) 14px 12px calc(14px + var(--safe-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.03em;
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-actions {
  display: flex;
  gap: 6px;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.14);
  color: #FFF;
  border: none;
  border-radius: 8px;
  width: 34px;
  height: 34px;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s;
  flex-shrink: 0;
}

.icon-btn svg {
  width: 18px;
  height: 18px;
  display: block;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.24);
}

.icon-btn:active {
  transform: scale(0.95);
}

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

.icon-btn.primary:hover {
  background: #05B34C;
}

.icon-btn.compact {
  width: 30px;
  height: 30px;
  border-radius: 7px;
}

.icon-btn.compact svg {
  width: 16px;
  height: 16px;
}

.icon-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Tabs */
.tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  background: #FAFAFA;
  flex-shrink: 0;
}

.tab-btn {
  flex: 1;
  padding: 10px 8px;
  border: none;
  background: transparent;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-sub);
  cursor: pointer;
  border-bottom: 2.5px solid transparent;
  transition: all 0.15s;
}

.tab-btn.active {
  color: var(--header-bg);
  border-bottom-color: var(--accent);
  background: #FFF;
}

/* Search & Filter */
.controls {
  padding: 10px 12px 8px calc(12px + var(--safe-left));
  border-bottom: 1px solid var(--border-color);
  background: #FFF;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
}

.search-input {
  width: 100%;
  padding: 8px 30px 8px 12px;
  border: 1px solid #D1D5DB;
  border-radius: 20px;
  font-size: 13px;
  background: #F9FAFB;
  outline: none;
}

.search-input:focus {
  border-color: var(--accent);
  background: #FFF;
}

.search-clear {
  position: absolute;
  right: 8px;
  background: none;
  border: none;
  color: #9CA3AF;
  font-size: 16px;
  cursor: pointer;
  display: none;
  padding: 2px 5px;
}

.search-clear.visible {
  display: block;
}

.filter-pills {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.pill {
  padding: 4px 10px;
  border-radius: 14px;
  border: 1px solid #D1D5DB;
  background: #FFF;
  font-size: 12px;
  color: var(--text-sub);
  cursor: pointer;
  font-weight: 500;
  transition: all 0.12s;
}

.pill.active {
  background: var(--header-bg);
  color: #FFF;
  border-color: var(--header-bg);
}

/* Room List */
.room-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-left: var(--safe-left);
  padding-bottom: var(--safe-bottom);
}

.room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid #F3F4F6;
  cursor: pointer;
  transition: background 0.12s;
}

.room-item:hover {
  background: #F9FAFB;
}

.room-item.active {
  background: #ECFDF5;
}

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FFF;
  font-weight: 700;
  font-size: 16px;
  overflow: hidden;
  background: #94A3B8;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar.small {
  width: 34px;
  height: 34px;
  font-size: 13px;
}

.room-info {
  flex: 1;
  min-width: 0;
}

.room-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 3px;
}

.room-name {
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-date {
  font-size: 11px;
  color: var(--text-sub);
  flex-shrink: 0;
}

.room-preview {
  font-size: 12px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 10px;
  margin-right: 4px;
  vertical-align: middle;
}

.badge.miguri {
  background: #DBEAFE;
  color: #1D4ED8;
}

.badge.riami {
  background: #FCE7F3;
  color: #BE185D;
}

.badge.other {
  background: #E5E7EB;
  color: #374151;
}

/* Chat Pane */
.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--line-bg);
  position: relative;
  min-width: 0;
}

.chat-header {
  background: rgba(39, 50, 70, 0.94);
  backdrop-filter: blur(6px);
  color: #FFF;
  padding: calc(10px + var(--safe-top)) calc(14px + var(--safe-right)) 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
  z-index: 5;
}

.chat-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.back-btn {
  display: none;
  background: none;
  border: none;
  color: #FFF;
  cursor: pointer;
  padding: 4px 6px 4px 0;
  align-items: center;
  justify-content: center;
}

.back-btn svg {
  width: 22px;
  height: 22px;
  display: block;
}

.chat-title-wrap {
  min-width: 0;
}

.chat-title {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-subtitle {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.78);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sync-status {
  font-size: 11px;
  padding: 4px 9px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.14);
  color: #FFF;
  white-space: nowrap;
  flex-shrink: 0;
}

.sync-status.ok {
  background: rgba(6, 199, 85, 0.24);
  color: #DCFCE7;
}

.sync-status.warn {
  background: rgba(245, 158, 11, 0.28);
  color: #FEF3C7;
}

.sync-status.err {
  background: rgba(239, 68, 68, 0.32);
  color: #FEE2E2;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px calc(14px + var(--safe-right)) 32px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: rgba(255, 255, 255, 0.92);
  background: rgba(0, 0, 0, 0.18);
  padding: 24px 28px;
  border-radius: 16px;
  max-width: 340px;
  font-size: 13px;
  line-height: 1.65;
}

/* Section Divider (Date / Event / Part / Member / Tickets) */
.session-divider {
  align-self: center;
  margin: 14px 0 8px;
  background: rgba(25, 35, 52, 0.45);
  color: #FFF;
  font-size: 11.5px;
  font-weight: 600;
  padding: 5px 14px;
  border-radius: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background 0.15s;
  max-width: 92%;
  text-align: center;
}

.session-divider:hover {
  background: rgba(25, 35, 52, 0.68);
}

.session-divider .edit-hint {
  opacity: 0.72;
  font-size: 10px;
}

/* System / Stage Note */
.note-row {
  align-self: center;
  margin: 4px 0;
  max-width: 86%;
}

.note-pill {
  background: rgba(0, 0, 0, 0.24);
  color: #F9FAFB;
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 12px;
  cursor: pointer;
  line-height: 1.45;
  text-align: center;
  transition: transform 0.1s, background 0.15s;
}

.note-pill:hover {
  background: rgba(0, 0, 0, 0.36);
}

/* Chat Message Rows */
.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: 100%;
}

.msg-row.me {
  justify-content: flex-end;
}

.msg-row.member {
  justify-content: flex-start;
}

.msg-col {
  display: flex;
  flex-direction: column;
  max-width: min(74%, 520px);
}

.msg-row.me .msg-col {
  align-items: flex-end;
}

.msg-row.member .msg-col {
  align-items: flex-start;
}

.sender-name {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.92);
  margin-bottom: 3px;
  margin-left: 4px;
  font-weight: 600;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.18);
}

.bubble {
  position: relative;
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
  white-space: pre-wrap;
  cursor: pointer;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.08);
  transition: filter 0.12s, transform 0.08s;
}

.bubble:hover {
  filter: brightness(0.96);
}

.bubble:active {
  transform: scale(0.985);
}

.msg-row.me .bubble {
  background: var(--me-bubble);
  color: #111827;
  border-top-right-radius: 4px;
}

.msg-row.me .bubble::after {
  content: "";
  position: absolute;
  top: 0;
  right: -7px;
  width: 0;
  height: 0;
  border-top: 8px solid var(--me-bubble);
  border-right: 8px solid transparent;
}

.msg-row.member .bubble {
  background: var(--member-bubble);
  color: #111827;
  border-top-left-radius: 4px;
}

.msg-row.member .bubble::after {
  content: "";
  position: absolute;
  top: 0;
  left: -7px;
  width: 0;
  height: 0;
  border-top: 8px solid var(--member-bubble);
  border-left: 8px solid transparent;
}

mark {
  background: #FEF08A;
  color: #111827;
  padding: 0 2px;
  border-radius: 3px;
}

.chat-hint-bar {
  background: rgba(255, 255, 255, 0.9);
  border-top: 1px solid rgba(0, 0, 0, 0.08);
  padding: 7px calc(14px + var(--safe-right)) calc(7px + var(--safe-bottom)) 14px;
  font-size: 11.5px;
  color: #4B5563;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

#totalCountLabel {
  white-space: nowrap;
  flex-shrink: 0;
}

/* Auth / Setup Overlay */
.auth-overlay {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, #273246 0%, #1C2536 100%);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 150;
  padding: calc(18px + var(--safe-top)) calc(18px + var(--safe-right)) calc(18px + var(--safe-bottom)) calc(18px + var(--safe-left));
  overflow-y: auto;
}

.auth-overlay.open {
  display: flex;
}

.auth-card {
  width: 100%;
  max-width: 460px;
  background: #FFF;
  border-radius: 18px;
  padding: 24px 22px;
  box-shadow: 0 24px 48px rgba(0, 0, 0, 0.35);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.auth-logo {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  flex-shrink: 0;
}

.auth-title {
  font-size: 19px;
  font-weight: 700;
  color: var(--header-bg);
}

.auth-desc {
  font-size: 12.5px;
  color: var(--text-sub);
  line-height: 1.55;
}

.auth-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 2px;
}

.auth-error {
  background: #FEF2F2;
  border: 1px solid #FECACA;
  color: #B91C1C;
  font-size: 12px;
  line-height: 1.45;
  padding: 9px 11px;
  border-radius: 8px;
  display: none;
}

.auth-error.visible {
  display: block;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: calc(14px + var(--safe-top)) calc(14px + var(--safe-right)) calc(14px + var(--safe-bottom)) calc(14px + var(--safe-left));
}

.modal-backdrop.open {
  display: flex;
}

.modal {
  background: #FFF;
  border-radius: 16px;
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
}

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

.modal-title {
  font-size: 15px;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 22px;
  color: var(--text-sub);
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
}

.modal-body {
  padding: 16px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  background: #F9FAFB;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.field label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: #374151;
  margin-bottom: 4px;
}

.field input[type="text"],
.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid #D1D5DB;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  background: #FFF;
}

.field textarea {
  min-height: 90px;
  resize: vertical;
  line-height: 1.5;
}

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

.field-hint {
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-sub);
  line-height: 1.45;
}

.btn {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid #D1D5DB;
  background: #FFF;
  color: var(--text-main);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s;
}

.btn:hover {
  background: #F3F4F6;
}

.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #FFF;
}

.btn.primary:hover {
  background: #05B34C;
}

.btn.dark {
  background: var(--header-bg);
  border-color: var(--header-bg);
  color: #FFF;
}

.btn.dark:hover {
  background: #1E2838;
}

.btn.danger {
  background: #FEE2E2;
  border-color: #FCA5A5;
  color: #B91C1C;
}

.btn.danger:hover {
  background: #FECACA;
}

.btn.full {
  width: 100%;
  padding: 10px 14px;
}

.quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.info-box {
  background: #F8FAFC;
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 12px;
  color: #374151;
  line-height: 1.55;
}

.warn-box {
  background: #FFFBEB;
  border: 1px solid #FCD34D;
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 12.5px;
  color: #92400E;
  line-height: 1.55;
}

/* Member config row */
.member-cfg-card {
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 12px;
  background: #F9FAFB;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.member-cfg-top {
  display: flex;
  align-items: center;
  gap: 10px;
}

.member-cfg-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.color-input-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
}

.color-input-wrap input[type="color"] {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  cursor: pointer;
  flex-shrink: 0;
}

/* Dropzone */
.dropzone {
  border: 2px dashed #CBD5E1;
  border-radius: 12px;
  padding: 24px 16px;
  text-align: center;
  background: #F8FAFC;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-sub);
  line-height: 1.6;
}

.dropzone.dragover {
  border-color: var(--accent);
  background: #ECFDF5;
  color: #065F46;
}

/* Toast */
.toast {
  position: fixed;
  bottom: calc(20px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(17, 24, 39, 0.92);
  color: #FFF;
  padding: 9px 18px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: all 0.2s ease;
  z-index: 220;
  max-width: calc(100vw - 32px);
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Mobile Responsive */
@media (max-width: 768px) {
  .sidebar {
    width: 100%;
    min-width: 0;
  }

  .chat-pane {
    position: fixed;
    inset: 0;
    transform: translateX(100%);
    transition: transform 0.22s ease;
    z-index: 20;
  }

  .app.mobile-chat-open .chat-pane {
    transform: translateX(0);
  }

  .back-btn {
    display: inline-flex;
  }

  .msg-col {
    max-width: 80%;
  }

  .member-cfg-grid {
    grid-template-columns: 1fr;
  }
}
