:root {
  color-scheme: light;
  --green: #176b4d;
  --green-dark: #0e4d38;
  --green-soft: #dcece4;
  --orange: #dc7428;
  --orange-soft: #fbe8d8;
  --yellow: #e5b93f;
  --red: #b4473f;
  --ink: #18221e;
  --muted: #66716c;
  --line: #d7ddd8;
  --surface: #ffffff;
  --canvas: #f4f6f1;
  --shadow: 0 8px 28px rgba(20, 45, 35, 0.09);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--canvas);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: wait;
  opacity: 0.62;
}

[hidden] {
  display: none !important;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(90deg, rgba(23, 107, 77, 0.055) 1px, transparent 1px),
    linear-gradient(rgba(23, 107, 77, 0.055) 1px, transparent 1px),
    var(--canvas);
  background-size: 32px 32px;
}

.login-panel {
  width: min(100%, 430px);
  padding: 32px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.brand-mark {
  width: 86px;
  height: 86px;
  display: block;
  margin-bottom: 24px;
  object-fit: contain;
}

.brand-mark-small {
  width: 48px;
  height: 48px;
  margin: 0;
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--green);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 23px;
  line-height: 1.1;
}

h2 {
  margin-bottom: 0;
  font-size: 24px;
  line-height: 1.15;
}

h3 {
  margin-bottom: 2px;
  font-size: 17px;
}

.login-copy {
  margin: 10px 0 28px;
  color: var(--muted);
}

.field-label {
  display: block;
  margin-bottom: 7px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
}

input,
select {
  min-width: 0;
  height: 44px;
  border: 1px solid #bfc8c2;
  border-radius: 6px;
  background: white;
  color: var(--ink);
  outline: none;
}

input {
  width: 100%;
  padding: 0 12px;
}

select {
  padding: 0 34px 0 11px;
}

input:focus,
select:focus,
button:focus-visible {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(23, 107, 77, 0.18);
  outline: none;
}

.password-row,
.manual-entry > div {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.login-fields,
.admin-form {
  display: grid;
  gap: 10px;
}

.login-fields .field-label,
.admin-form .field-label {
  margin: 4px 0 -4px;
}

.login-fields .primary-button {
  margin-top: 10px;
}

.primary-button,
.secondary-button,
.camera-button {
  min-height: 42px;
  padding: 0 16px;
  border-radius: 6px;
  font-weight: 800;
}

.primary-button,
.camera-button {
  border: 1px solid var(--green);
  background: var(--green);
  color: white;
}

.primary-button:hover,
.camera-button:hover {
  background: var(--green-dark);
}

.secondary-button {
  border: 1px solid #b9c5be;
  background: white;
  color: var(--ink);
}

.secondary-button:hover {
  background: #edf2ee;
}

.danger-button {
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--red);
  border-radius: 6px;
  background: var(--red);
  color: white;
  font-weight: 800;
}

.account-label {
  max-width: 150px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.admin-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}

.admin-column {
  min-width: 0;
  display: grid;
  align-content: start;
  gap: 16px;
}

.admin-form {
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.admin-form select {
  width: 100%;
}

.charm-admin-form {
  margin-bottom: 20px;
}

.charm-form-grid {
  display: grid;
  grid-template-columns: minmax(120px, 0.45fr) minmax(0, 1fr);
  gap: 12px;
}

.charm-form-grid > div {
  display: grid;
  gap: 8px;
}

.admin-form .charm-form-grid .field-label {
  margin: 0;
}

.file-input {
  height: auto;
  min-height: 44px;
  padding: 8px;
}

.file-input::file-selector-button {
  min-height: 30px;
  margin-right: 10px;
  padding: 0 10px;
  border: 1px solid #b9c5be;
  border-radius: 5px;
  background: white;
  color: var(--ink);
  font-weight: 700;
}

.admin-scanner {
  width: 100%;
  min-height: 260px;
}

.admin-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
}

.admin-row:last-child {
  border-bottom: 0;
}

.dialog-copy {
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.45;
}

.camera-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.camera-button svg {
  width: 18px;
  height: 18px;
}

.form-error {
  min-height: 20px;
  margin: 9px 0 0;
  color: var(--red);
  font-size: 13px;
  font-weight: 650;
}

.app-shell {
  min-height: 100vh;
}

.app-header {
  position: sticky;
  z-index: 20;
  top: 0;
  min-height: 66px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px max(16px, calc((100vw - 900px) / 2));
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(12px);
}

.brand-lockup,
.header-actions,
.section-heading,
.list-heading,
.status-strip,
.record-title,
.student-title,
.dialog-heading,
.dialog-actions,
.metric-row {
  display: flex;
  align-items: center;
}

.brand-lockup {
  gap: 10px;
}

.header-actions {
  gap: 7px;
}

.connection-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green-dark);
  font-size: 12px;
  font-weight: 800;
}

.connection-status.offline {
  background: #f4dedd;
  color: #8c302b;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #1e8e61;
}

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

.icon-button {
  width: 38px;
  height: 38px;
  display: inline-grid;
  flex: 0 0 38px;
  place-items: center;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
}

.icon-button:hover {
  background: #e9eeea;
}

.icon-button svg {
  width: 20px;
  height: 20px;
}

.app-main {
  width: min(100%, 900px);
  min-height: calc(100vh - 66px);
  margin: 0 auto;
  padding: 22px 16px calc(112px + env(safe-area-inset-bottom));
}

.tab-panel {
  animation: tab-in 160ms ease-out;
}

@keyframes tab-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.section-heading {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}

.mode-status,
.count-badge {
  flex: 0 0 auto;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--orange-soft);
  color: #8c461a;
  font-size: 12px;
  font-weight: 800;
}

.mode-status.active,
.count-badge {
  background: var(--green-soft);
  color: var(--green-dark);
}

.count-badge.neutral {
  background: #e9edeb;
  color: #4d5953;
}

.scanner-tool {
  position: relative;
  width: 100%;
  min-height: 300px;
  overflow: hidden;
  aspect-ratio: 4 / 3;
  border: 1px solid #26342d;
  border-radius: 8px;
  background: #101613;
  box-shadow: var(--shadow);
}

.compact-scanner {
  max-height: 430px;
}

.scanner-reader,
.scanner-reader > div,
.scanner-reader video {
  width: 100% !important;
  height: 100% !important;
}

.scanner-reader > div {
  border: 0 !important;
}

.scanner-reader video {
  object-fit: cover;
}

.scanner-reader img,
.scanner-reader button,
.scanner-reader select,
.scanner-reader > div > div:last-child {
  display: none !important;
}

.camera-placeholder {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 18px;
  background: #111814;
  color: #d9e3dd;
}

.camera-placeholder::before,
.camera-placeholder::after {
  position: absolute;
  width: 58px;
  height: 58px;
  content: "";
  border-color: var(--orange);
  border-style: solid;
}

.camera-placeholder::before {
  top: 22px;
  left: 22px;
  border-width: 4px 0 0 4px;
}

.camera-placeholder::after {
  right: 22px;
  bottom: 22px;
  border-width: 0 4px 4px 0;
}

.camera-placeholder > svg {
  width: 42px;
  height: 42px;
  color: var(--orange);
}

.scanner-tool.camera-active .camera-placeholder {
  display: none;
}

.manual-entry {
  margin-top: 12px;
}

.manual-entry > label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.status-strip {
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 20px;
}

.status-strip > span {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: white;
  color: #425049;
  font-size: 12px;
  font-weight: 650;
}

.status-strip svg {
  width: 15px;
  height: 15px;
  color: var(--green);
}

.list-section {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.list-heading {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 11px;
}

.list-heading p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

.scroll-list {
  max-height: 358px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.scan-row,
.record-row,
.charm-row {
  min-height: 70px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.scan-row + .scan-row,
.record-row + .record-row,
.charm-row + .charm-row {
  margin-top: 8px;
}

.row-icon,
.rank,
.charm-image,
.charm-placeholder {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  flex: 0 0 46px;
  border-radius: 6px;
}

.row-icon {
  background: var(--green-soft);
  color: var(--green-dark);
}

.row-icon.start {
  background: var(--orange-soft);
  color: #8c461a;
}

.row-icon svg {
  width: 22px;
  height: 22px;
}

.row-main,
.record-main {
  min-width: 0;
}

.student-title,
.record-title {
  min-width: 0;
  gap: 8px;
}

.student-title strong,
.record-title strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-subtitle,
.record-subtitle {
  margin-top: 3px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.row-time,
.lap-total {
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}

.grade-badge {
  min-width: 26px;
  padding: 3px 6px;
  border-radius: 999px;
  background: #e4e9e6;
  color: #4c5952;
  font-size: 10px;
  font-weight: 900;
  text-align: center;
}

.grade-k { background: #eee1f3; color: #76418c; }
.grade-1 { background: #dde9f5; color: #235f96; }
.grade-2 { background: #d8efec; color: #0d736c; }
.grade-3 { background: var(--orange-soft); color: #9a511c; }
.grade-4 { background: #f4dedd; color: #923c36; }
.grade-5 { background: #e4ecd8; color: #486925; }

.empty-list,
.empty-result {
  display: grid;
  place-items: center;
  padding: 34px 20px;
  color: var(--muted);
  text-align: center;
}

.empty-list svg,
.empty-result > svg {
  width: 34px;
  height: 34px;
  margin-bottom: 10px;
  color: var(--green);
}

.empty-list p,
.empty-result p {
  margin: 0;
  font-size: 13px;
}

.stats-result {
  margin-top: 18px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

.stats-result:not(.empty-result) {
  display: block;
}

.stats-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.stats-name-row h3 {
  margin: 0;
  font-size: 21px;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.metric {
  min-width: 0;
  padding: 13px;
  border-left: 3px solid var(--green);
  background: #f0f4f1;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin-top: 4px;
  font-size: 18px;
  overflow-wrap: anywhere;
}

.records-heading {
  align-items: flex-end;
}

.select-field {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.rank {
  border-radius: 50%;
  background: #e8ece9;
  color: var(--ink);
  font-size: 15px;
  font-weight: 900;
}

.record-row:first-child .rank {
  background: #f4e6ae;
  color: #6f5511;
}

.record-row:nth-child(2) .rank {
  background: #e2e7e5;
}

.record-row:nth-child(3) .rank {
  background: #f2dfd0;
  color: #7f4c28;
}

.lap-total {
  font-size: 18px;
}

.lap-total small {
  display: block;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  text-align: right;
}

.charm-section + .charm-section {
  margin-top: 26px;
}

.charm-row.actionable {
  width: 100%;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.charm-row.actionable:hover {
  border-color: #91b3a3;
  background: #fbfdfb;
}

.charm-image {
  object-fit: cover;
}

.charm-placeholder {
  background: var(--orange-soft);
  color: #8c461a;
}

.charm-placeholder svg {
  width: 23px;
  height: 23px;
}

.chevron {
  color: var(--muted);
}

.chevron svg {
  width: 20px;
  height: 20px;
}

.bottom-nav {
  position: fixed;
  z-index: 30;
  right: 0;
  bottom: 0;
  left: 0;
  width: min(100%, 900px);
  min-height: 74px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin: 0 auto;
  padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
  border: 1px solid var(--line);
  border-bottom: 0;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 -6px 24px rgba(20, 45, 35, 0.09);
  backdrop-filter: blur(12px);
}

.bottom-nav.admin-enabled {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.nav-button {
  min-width: 0;
  min-height: 57px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 4px;
  padding: 4px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #68736e;
}

.nav-button:hover {
  background: #edf1ee;
}

.nav-button.active {
  background: var(--green-soft);
  color: var(--green-dark);
}

.nav-button svg {
  width: 21px;
  height: 21px;
}

.nav-button span {
  max-width: 100%;
  overflow: hidden;
  font-size: 10px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.app-dialog {
  width: min(calc(100% - 32px), 440px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--ink);
  box-shadow: 0 20px 70px rgba(13, 33, 25, 0.24);
}

.app-dialog::backdrop {
  background: rgba(13, 24, 19, 0.52);
}

.app-dialog form,
.charm-dialog {
  padding: 22px;
}

.dialog-heading {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.dialog-close {
  margin-right: -8px;
}

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

.number-field span {
  color: var(--muted);
}

.dialog-actions {
  justify-content: flex-end;
  gap: 8px;
  margin-top: 22px;
}

.charm-confirmation {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 16px;
}

.charm-confirmation h2 {
  font-size: 20px;
}

.charm-confirmation p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.toast-region {
  position: fixed;
  z-index: 60;
  right: 16px;
  bottom: calc(88px + env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
  pointer-events: none;
}

.toast {
  max-width: min(360px, calc(100vw - 32px));
  padding: 11px 14px;
  border-radius: 6px;
  background: #1d2c25;
  color: white;
  box-shadow: var(--shadow);
  font-size: 13px;
  font-weight: 700;
  animation: toast-in 180ms ease-out;
}

.toast.error {
  background: #873a35;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (min-width: 700px) {
  .app-main {
    padding-right: 24px;
    padding-left: 24px;
  }

  .scanner-tool {
    min-height: 440px;
  }

  .compact-scanner {
    width: min(100%, 620px);
    min-height: 360px;
  }

  .manual-entry {
    width: min(100%, 620px);
  }
}

@media (max-width: 520px) {
  .app-header {
    min-height: 60px;
  }

  .brand-mark-small {
    width: 42px;
    height: 42px;
  }

  .brand-lockup h1 {
    font-size: 18px;
  }

  .connection-status {
    width: 10px;
    min-height: 10px;
    padding: 0;
    overflow: hidden;
    color: transparent;
    background: transparent;
  }

  .connection-status .status-dot {
    flex: 0 0 8px;
  }

  .app-main {
    padding-top: 16px;
  }

  .scanner-tool {
    min-height: 250px;
  }

  .password-row,
  .manual-entry > div {
    grid-template-columns: 1fr;
  }

  .metrics {
    grid-template-columns: 1fr;
  }

  .metric {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
  }

  .metric strong {
    margin: 0;
    font-size: 16px;
    text-align: right;
  }

  .scan-row,
  .record-row,
  .charm-row {
    gap: 9px;
    padding: 9px;
  }

  .row-icon,
  .rank,
  .charm-image,
  .charm-placeholder {
    width: 40px;
    height: 40px;
  }

  .row-time {
    font-size: 11px;
  }

  .lap-total {
    font-size: 16px;
  }

  .records-heading {
    align-items: center;
  }

  .account-label {
    display: none;
  }

  .admin-grid {
    grid-template-columns: 1fr;
  }

  .charm-form-grid {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
