:root {
  --bg: #f3f7fb;
  --surface: #ffffff;
  --soft: #f8fafc;
  --line: #dce7f3;
  --line-strong: #c8d6e6;
  --text: #111827;
  --muted: #64748b;
  --primary: #2d8cff;
  --primary-dark: #176fd6;
  --success: #62c63a;
  --teal: #42b8a6;
  --danger: #ef4d45;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.07);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-feature-settings: "tnum";
}

body.is-scanning,
body.is-capturing {
  overflow: hidden;
}

h1,
h2 {
  margin: 0;
  color: var(--text);
  font-weight: 800;
}

a {
  color: var(--primary);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
}

.brand,
.login-brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 800;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 32px;
  height: 32px;
  place-items: center;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(135deg, var(--success), var(--primary));
  font-weight: 900;
}

.top-nav {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.top-nav a,
.account a {
  padding: 9px 12px;
  border-radius: 8px;
  color: #53647c;
  font-weight: 760;
  text-decoration: none;
}

.top-nav a.active,
.top-nav a:hover {
  color: #fff;
  background: linear-gradient(135deg, var(--success), var(--teal));
}

.account {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.94rem;
  font-weight: 700;
}

.app-main {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.login-main {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: min(440px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: 0 22px 70px rgba(15, 23, 42, 0.12);
}

.login-brand {
  margin-bottom: 22px;
}

.login-brand h1 {
  font-size: 1.45rem;
}

.login-brand p,
.page-title p {
  margin: 4px 0 0;
  color: var(--muted);
}

.login-form {
  display: grid;
  gap: 14px;
}

.page-title {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.page-title h1 {
  font-size: 1.7rem;
}

.panel,
.scan-card,
.recent-card,
.stats-grid > div {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.panel {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.panel h2,
.section-head h2 {
  font-size: 1.08rem;
}

.form-label {
  color: #334155;
  font-weight: 650;
}

.form-control,
.form-select {
  min-height: 44px;
  border-color: var(--line-strong);
  border-radius: 8px;
}

.form-control:focus,
.form-select:focus {
  border-color: rgba(45, 140, 255, 0.72);
  box-shadow: 0 0 0 3px rgba(45, 140, 255, 0.14);
}

.btn {
  border-radius: 8px;
  font-weight: 760;
}

.btn-primary {
  border-color: var(--primary);
  background: linear-gradient(135deg, var(--primary), #246ee9);
  box-shadow: 0 8px 18px rgba(45, 140, 255, 0.2);
}

.btn-primary:hover,
.btn-primary:focus {
  border-color: var(--primary-dark);
  background: var(--primary-dark);
}

.btn-success {
  border-color: var(--success);
  background: linear-gradient(135deg, var(--success), #3dac60);
}

.btn-outline-primary {
  color: var(--primary);
  border-color: rgba(45, 140, 255, 0.48);
}

.btn-outline-primary:hover {
  border-color: var(--primary);
  background: var(--primary);
}

.scan-page {
  width: min(860px, 100%);
  margin: 0 auto;
}

.front-hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
  min-height: 18vh;
  margin-bottom: 18px;
  padding-top: 18px;
}

.front-hero h1 {
  font-size: 2rem;
}

.front-hero p {
  margin: 6px 0 0;
  color: var(--muted);
}

.front-admin-link {
  flex: 0 0 auto;
  color: var(--muted);
  font-weight: 760;
  text-decoration: none;
}

.front-actions {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

.front-actions .btn {
  min-height: 58px;
  font-size: 1.08rem;
}

.front-manual {
  margin-bottom: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.scan-card,
.recent-card {
  margin-bottom: 14px;
  padding: 14px;
}

.scanner-card {
  padding: 0;
  overflow: hidden;
}

body.is-scanning .scanner-card,
body.is-capturing .scanner-card {
  position: fixed;
  inset: 0;
  z-index: 1000;
  margin: 0;
  border: 0;
  border-radius: 0;
  background: #000;
  box-shadow: none;
}

.scanner {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: #eaf3fb;
  aspect-ratio: 4 / 5;
}

body.is-scanning .scanner,
body.is-capturing .scanner {
  width: 100vw;
  height: 100vh;
  border-radius: 0;
  aspect-ratio: auto;
  background: #000;
}

.scanner video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.scan-frame {
  position: absolute;
  inset: 18%;
  border: 3px solid rgba(45, 140, 255, 0.92);
  border-radius: 8px;
  box-shadow: 0 0 0 999px rgba(255, 255, 255, 0.34);
  pointer-events: none;
}

body.is-scanning .scan-frame {
  inset: 24% 10%;
  border-color: rgba(98, 198, 58, 0.96);
  box-shadow: 0 0 0 999px rgba(0, 0, 0, 0.18);
}

body.is-capturing .scan-frame,
body.is-capturing .scan-line {
  display: none;
}

.scan-line {
  position: absolute;
  left: 20%;
  right: 20%;
  top: 20%;
  height: 3px;
  border-radius: 999px;
  background: linear-gradient(90deg, transparent, #62c63a, transparent);
  box-shadow: 0 0 18px rgba(98, 198, 58, 0.58);
  animation: scanLine 1.45s ease-in-out infinite;
  pointer-events: none;
}

body.is-scanning .scan-line {
  left: 12%;
  right: 12%;
}

@keyframes scanLine {
  0% {
    top: 20%;
  }
  50% {
    top: 80%;
  }
  100% {
    top: 20%;
  }
}

.scan-status {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  color: #fff;
  background: rgba(15, 23, 42, 0.76);
  font-weight: 760;
  text-align: center;
}

body.is-scanning .scan-status,
body.is-capturing .scan-status {
  left: 16px;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 18px);
  z-index: 1002;
  background: rgba(15, 23, 42, 0.68);
}

.success-prompt {
  display: grid;
  gap: 3px;
  margin-bottom: 14px;
  padding: 14px 16px;
  border: 1px solid #bfeacb;
  border-radius: 8px;
  color: #17604f;
  background: linear-gradient(135deg, #f6fff8, #effcf3);
  box-shadow: var(--shadow);
}

.camera-prompt {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 16px);
  left: 16px;
  right: 16px;
  z-index: 1003;
  margin: 0;
  text-align: center;
}

.success-prompt strong {
  font-size: 1.1rem;
}

.success-prompt span {
  color: #3f705f;
  font-weight: 650;
}

.primary-actions,
.photo-actions {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.primary-actions {
  grid-template-columns: 1fr 1fr;
}

.photo-actions {
  grid-template-columns: 1fr 1fr;
}

.photo-actions #saveBtn {
  grid-column: 1 / -1;
}

body.is-capturing #captureCard {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  z-index: 1004;
  margin: 0;
  border-color: rgba(255, 255, 255, 0.44);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 14px 36px rgba(15, 23, 42, 0.24);
  backdrop-filter: blur(12px);
}

body.is-capturing #captureCard .photo-actions {
  margin-top: 10px;
}

body.is-capturing #captureCard .btn-lg {
  min-height: 48px;
}

.manual-form {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.capture-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.capture-head span {
  display: block;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 700;
}

.capture-head strong,
.tracking-title {
  display: block;
  overflow-wrap: anywhere;
  font-size: 1.35rem;
  font-weight: 850;
  line-height: 1.2;
}

.capture-head em {
  flex: 0 0 auto;
  padding: 6px 10px;
  border-radius: 999px;
  color: #17604f;
  background: #e9fbf5;
  font-style: normal;
  font-weight: 800;
}

.photo-preview {
  width: 100%;
  max-height: 52vh;
  border: 1px solid var(--line);
  border-radius: 8px;
  object-fit: contain;
  background: var(--soft);
}

body.is-capturing .photo-preview {
  max-height: 30vh;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.recent-list {
  display: grid;
  gap: 8px;
}

.recent-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--soft);
  text-decoration: none;
}

.recent-item span {
  color: var(--muted);
  font-size: 0.88rem;
}

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

.filter-bar {
  display: grid;
  grid-template-columns: minmax(280px, 1fr) 150px 150px 140px 96px;
  align-items: end;
  gap: 12px;
  margin-bottom: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.filter-bar textarea {
  min-height: 44px;
  max-height: 120px;
  resize: vertical;
}

.filter-submit .btn {
  width: 100%;
  min-height: 44px;
}

.single-result {
  margin-bottom: 14px;
}

.single-layout {
  display: grid;
  grid-template-columns: minmax(280px, 520px) 1fr;
  gap: 18px;
  align-items: start;
}

.single-layout img {
  width: 100%;
  max-height: 620px;
  border: 1px solid var(--line);
  border-radius: 8px;
  object-fit: contain;
  background: var(--soft);
}

.single-layout p {
  margin: 9px 0;
  color: #475569;
  font-weight: 650;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.stats-grid > div {
  padding: 14px 16px;
}

.stats-grid span {
  display: block;
  color: var(--muted);
  font-weight: 650;
}

.stats-grid strong {
  display: block;
  margin-top: 4px;
  font-size: 1.55rem;
  font-weight: 850;
}

.results-panel {
  margin-bottom: 14px;
  padding: 0;
}

.results-panel .section-head {
  margin: 0;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.table thead th {
  color: #53647c;
  background: #f7fbff;
  font-size: 0.88rem;
  font-weight: 800;
}

.table tbody td {
  border-color: #edf2f7;
}

.photo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 12px;
}

.photo-grid a {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-decoration: none;
}

.photo-grid img {
  width: 100%;
  height: 180px;
  object-fit: cover;
  background: var(--soft);
}

.photo-grid span {
  display: block;
  padding: 9px 10px;
  overflow: hidden;
  font-weight: 760;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@media (max-width: 900px) {
  .app-header {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .top-nav {
    justify-content: stretch;
  }

  .top-nav a {
    flex: 1;
    text-align: center;
  }

  .account {
    justify-content: space-between;
  }

  .page-title {
    align-items: stretch;
    flex-direction: column;
  }

  .front-hero {
    align-items: stretch;
    flex-direction: column;
    min-height: auto;
  }

  .filter-bar {
    grid-template-columns: 1fr 1fr;
  }

  .filter-bar .search-field {
    grid-column: 1 / -1;
  }

  .filter-submit {
    grid-column: 1 / -1;
  }

  .single-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  .app-main {
    padding: 12px;
  }

  .brand {
    justify-content: center;
  }

  .scan-card,
  .recent-card,
  .panel {
    padding: 12px;
  }

  .scanner {
    aspect-ratio: 3 / 4;
  }

  .primary-actions,
  .photo-actions,
  .front-actions,
  .filter-bar {
    grid-template-columns: 1fr;
  }

  .filter-bar .search-field,
  .filter-submit {
    grid-column: auto;
  }

  .btn-lg {
    min-height: 50px;
    font-size: 1rem;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .results-panel .section-head {
    align-items: stretch;
    flex-direction: column;
  }

  .result-actions,
  .result-actions form,
  .result-actions button {
    width: 100%;
  }
}
