:root {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --text: #13223a;
  --muted: #5f6f86;
  --border: #dbe3ef;
  --rfid: #1f5f9b;
  --nfc: #2f6fae;
  --accent: #f59f00;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  scrollbar-width: none;
}

html::-webkit-scrollbar,
body::-webkit-scrollbar {
  width: 0;
  height: 0;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Segoe UI", sans-serif;
  background: radial-gradient(circle at top right, #dcecff 0%, var(--bg) 45%);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
  height: 100%;
  max-height: 100%;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  overflow: hidden;
}

.top-bar {
  min-height: 36px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

.app-content {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
}

#appContent[hidden],
#authGate[hidden],
#userMenu[hidden],
#userMenuDropdown[hidden] {
  display: none !important;
}

.auth-gate {
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 12px;
}

.auth-gate-card {
  width: min(420px, 100%);
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  padding: 18px;
  text-align: center;
}

.auth-gate-card h2 {
  margin: 0 0 8px;
}

.auth-gate-card p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 14px;
}

.user-menu {
  position: relative;
}

.user-menu-btn {
  border: 1px solid #9cb6d8;
  background: #edf3fc;
  color: #1e3a5f;
  border-radius: 10px;
  padding: 7px 12px;
  font-weight: 700;
  cursor: pointer;
  max-width: min(64vw, 380px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-menu-dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 140px;
  border: 1px solid #c8d8f2;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 10px 24px rgba(17, 39, 72, 0.12);
  padding: 6px;
  z-index: 20;
}

.user-menu-item {
  width: 100%;
  border: 0;
  background: transparent;
  color: #203f66;
  border-radius: 8px;
  text-align: left;
  font-weight: 600;
  padding: 8px 10px;
  cursor: pointer;
}

.user-menu-item:hover {
  background: #edf3fc;
}

.auth-btn {
  min-height: 40px;
  width: 100%;
}

.page-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.tab-btn {
  border: 1px solid #9cb6d8;
  background: #edf3fc;
  color: #1e3a5f;
  border-radius: 10px;
  padding: 8px 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: normal;
  text-align: center;
  line-height: 1.2;
  min-height: 40px;
  width: 100%;
}

.tab-btn.tab-active {
  background: #1f4f85;
  color: #fff;
  border-color: #1f4f85;
}

.page {
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 12px;
}

#assignmentPage {
  grid-template-rows: auto minmax(0, 1fr);
}

#crewsPage {
  grid-template-rows: minmax(0, 1fr);
}

#crewsPage .panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#crewConfigList {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.page[hidden] {
  display: none !important;
}

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

.panel h2 {
  margin: 0 0 10px;
  font-size: 18px;
}

.panel h3 {
  margin: 12px 0 8px;
  font-size: 15px;
}

.crew-list,
.event-log {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 8px;
}

.crew-list {
  min-height: 0;
  overflow-y: auto;
  padding-right: 6px;
  align-content: start;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.crew-list::-webkit-scrollbar {
  width: 0;
  height: 0;
}

.panel-results {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.panel-results .crew-list {
  flex: 1;
}

#assignmentPage .panel {
  min-height: 0;
}

#assignmentPage .panel-results {
  overflow: hidden;
}

#assignmentPage .crew-list {
  min-height: 0;
  overflow-y: auto;
}

.reported-wrap {
  margin-top: 10px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.reported-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 6px;
  max-height: 120px;
  overflow-y: auto;
  scrollbar-width: none;
}

.reported-list::-webkit-scrollbar {
  width: 0;
  height: 0;
}

.reported-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 12px;
  background: #f9fbff;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.reported-item:hover {
  border-color: #9bb7e8;
  background: #eef4ff;
}

.crew-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
}

.crew-item.active {
  border-color: #6da6ff;
  background: #edf4ff;
}

.crew-sub {
  color: var(--muted);
  font-size: 13px;
}

.crew-tag {
  font-size: clamp(10px, 2.6vw, 12px);
  color: #fff;
  background: #2f6fdb;
  border-radius: 999px;
  padding: 4px 7px;
  white-space: normal;
  line-height: 1.2;
  text-align: center;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.edit-box .button-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.edit-box .button-row:first-of-type {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.edit-box .button-row .btn {
  width: 100%;
  min-height: 36px;
  padding: 8px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.crew-controls {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  margin-bottom: 10px;
}

.crew-controls label {
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}

.crew-select {
  min-width: 0;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  background: #fff;
}

#reportBtn {
  white-space: nowrap;
}

#scanControls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

#scanControls .btn {
  width: 100%;
  white-space: normal;
  line-height: 1.2;
  min-height: 44px;
  padding: 10px 10px;
  font-size: clamp(13px, 2.6vw, 18px);
}

#clearBtn {
  grid-column: 1 / -1;
}

.btn {
  border: 0;
  color: #fff;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.btn:disabled {
  opacity: 0.45;
  filter: grayscale(0.35);
  cursor: not-allowed;
  box-shadow: none;
}

.btn-rfid {
  background: var(--rfid);
}

.btn-nfc {
  background: var(--nfc);
}

.btn-clear {
  background: #4f6f90;
}

.btn-report {
  background: #3f6389;
}

.status {
  margin: 10px 0 0;
  font-weight: 600;
}

.status.waiting {
  color: var(--accent);
}

.event-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 13px;
}

.member-item {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  display: grid;
  gap: 4px;
}

.member-item.selectable {
  cursor: pointer;
}

.member-item.selected {
  border-color: #6da6ff;
  background: #edf4ff;
}

.member-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.member-top strong {
  font-size: 13px;
  line-height: 1.25;
}

.member-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: clamp(10px, 2.5vw, 12px);
  line-height: 1.25;
  word-break: break-word;
}

.member-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-left: auto;
  justify-content: flex-end;
}

.icon-btn {
  width: 30px;
  height: 30px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #cfd9e8;
  border-radius: 999px;
  background: #ffffff;
  color: #365072;
  transition: transform 0.06s ease, filter 0.12s ease, box-shadow 0.12s ease;
  cursor: pointer;
}

.icon-btn svg {
  width: 15px;
  height: 15px;
  fill: currentColor;
}

.icon-btn:hover {
  background: #f4f8ff;
  border-color: #9bb8e4;
  color: #22466f;
}

.icon-btn:active {
  transform: translateY(1px) scale(0.98);
}

.icon-btn:focus-visible {
  outline: 2px solid #6da6ff;
  outline-offset: 1px;
}

.person-delete-btn {
  color: #8a2d2d;
  border-color: #e3c8c8;
}

.person-delete-btn:hover {
  background: #fff3f3;
  border-color: #dbaaaa;
  color: #6f1f1f;
}

.member-item.selectable:active {
  transform: translateY(1px);
}

.btn-assign-rfid {
  background: var(--rfid);
}

.btn-assign-nfc {
  background: var(--nfc);
}

.crew-define-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
}

.crew-define-controls input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
}

.crew-define-controls .btn {
  padding: 7px 9px;
  min-height: 34px;
  font-size: 12px;
  border-radius: 8px;
  white-space: nowrap;
}

.crew-define-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.crew-define-actions .btn {
  width: 100%;
}

.crew-name {
  min-width: 0;
  overflow-wrap: anywhere;
}

.crew-edit-btn {
  color: #2d4f78;
}

.crew-edit-btn:hover {
  background: #f4f8ff;
}

.crew-delete-btn {
  color: #8a2d2d;
  border-color: #e3c8c8;
}

.crew-delete-btn:hover {
  background: #fff3f3;
  border-color: #dbaaaa;
  color: #6f1f1f;
}

.crew-summary {
  margin: -2px 0 10px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

@media (max-width: 520px) {
  .page-tabs {
    gap: 6px;
  }

  .tab-btn {
    padding: 7px 8px;
    font-size: 12px;
    min-height: 38px;
  }

  .top-bar {
    min-height: 32px;
  }

  .user-menu-btn {
    max-width: 74vw;
    padding: 7px 10px;
    font-size: 12px;
  }

  .auth-btn {
    font-size: 12px;
    min-height: 36px;
    padding: 8px 10px;
  }

  .crew-controls {
    gap: 8px;
  }

  .crew-controls label {
    font-size: 12px;
  }

  .edit-box .button-row {
    gap: 6px;
  }

  .edit-box .button-row .btn {
    min-height: 34px;
    padding: 7px 8px;
    font-size: 11px;
  }

  .crew-select {
    font-size: 13px;
    padding: 8px;
  }

  #scanControls {
    gap: 8px;
  }

  #scanControls .btn {
    padding: 9px 8px;
    font-size: 12px;
  }

  .crew-define-controls {
    gap: 8px;
  }

  .crew-define-controls .btn {
    padding: 6px 7px;
    min-height: 32px;
    font-size: 11px;
  }

  .member-item {
    padding: 8px;
  }

  .member-top strong {
    font-size: 12px;
  }

  .member-tags {
    gap: 6px;
    font-size: 10px;
  }

  .crew-tag {
    max-width: 100%;
  }
}

.qr-scanner {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  background: #f7faff;
}

.qr-view {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
}

.qr-video {
  width: 100%;
  max-height: 240px;
  border-radius: 8px;
  background: #0d1117;
  margin-bottom: 10px;
  object-fit: cover;
}

.qr-focus-frame {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 62%;
  height: 42%;
  transform: translate(-50%, -50%);
  border: 2px solid #34a853;
  border-radius: 10px;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.28);
  pointer-events: none;
}

.edit-box {
  margin-top: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  background: #f7faff;
  display: grid;
  gap: 8px;
}

.edit-box label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.edit-box input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 16px;
}

.qr-scanner[hidden],
.edit-box[hidden] {
  display: none !important;
}

@media (max-width: 640px) {
  body,
  .app {
    height: 100dvh;
  }

  .panel {
    border-radius: 10px;
  }
}
