:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --surface-alt: #f9fafb;
  --text: #1a1a1a;
  --text-soft: #374151;
  --muted: #6b7280;
  --border: #e5e7eb;
  --input-border: #d1d5db;
  --link: #1d4ed8;
  --header-bg: #1f2937;
  --header-text: #d1d5db;
  --button-bg: #1f2937;
  --button-hover: #374151;
  --button-text: #ffffff;
  --error-text: #b91c1c;
  --error-bg: #fee2e2;
  --error-border: #fca5a5;
  --pick-bg: #dcfce7;
  --pick-border: #86efac;
  --pick-text: #166534;
}

[data-theme="dark"] {
  --bg: #0a0a0a;
  --surface: #161616;
  --surface-alt: #1f1f1f;
  --text: #e8e8e8;
  --text-soft: #c9c9c9;
  --muted: #9a9a9a;
  --border: #2c2c2c;
  --input-border: #3d3d3d;
  --link: #8ab4f8;
  --header-bg: #000000;
  --header-text: #a3a3a3;
  --button-bg: #3b82f6;
  --button-hover: #2563eb;
  --button-text: #ffffff;
  --error-text: #fca5a5;
  --error-bg: #2a0a0a;
  --error-border: #6b1d1d;
  --pick-bg: #0a2614;
  --pick-border: #1d5c34;
  --pick-text: #86efac;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--header-bg);
  color: #fff;
  padding: 0.6rem 1.25rem;
}

header .brand {
  color: #fff;
  font-weight: 700;
  text-decoration: none;
  font-size: 1.05rem;
}

header nav {
  display: flex;
  align-items: center;
  gap: 1rem;
}

header nav a {
  color: var(--header-text);
  text-decoration: none;
}

header nav a:hover {
  color: #fff;
}

main {
  max-width: 960px;
  margin: 1.5rem auto;
  padding: 0 1.25rem;
}

h1 {
  font-size: 1.4rem;
  margin: 0 0 1rem;
}

h2 {
  font-size: 1.1rem;
  margin: 0 0 0.75rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.35rem;
}

h3 {
  font-size: 1rem;
}

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

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

.small {
  font-size: 0.85rem;
}

.error {
  color: var(--error-text);
  background: var(--error-bg);
  border: 1px solid var(--error-border);
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
}

.notice {
  background: var(--pick-bg);
  border: 1px solid var(--pick-border);
  color: var(--pick-text);
  padding: 0.5rem 0.75rem;
  border-radius: 4px;
}

.pick-flag {
  display: inline-block;
  background: var(--pick-bg);
  border: 1px solid var(--pick-border);
  color: var(--pick-text);
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  font-weight: 600;
}

/* Login */

.login-page {
  max-width: 360px;
  margin: 5rem auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1.5rem;
}

.login-page form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* Tables */

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  margin-bottom: 1rem;
}

th, td {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

th {
  background: var(--surface-alt);
  font-weight: 600;
}

th a {
  color: inherit;
  text-decoration: none;
}

th a:hover {
  text-decoration: underline;
}

tbody tr:hover {
  background: var(--surface-alt);
}

.notes-cell {
  max-width: 280px;
}

/* Forms */

input[type="text"],
input[type="search"],
input[type="password"],
input[type="number"],
input[type="file"],
select,
textarea {
  width: 100%;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--input-border);
  border-radius: 4px;
  font: inherit;
  background: var(--surface);
  color: var(--text);
}

/* Search inputs get heavy browser default styling - strip it so they match */
input[type="search"] {
  -webkit-appearance: none;
  appearance: none;
}

textarea {
  resize: vertical;
}

label {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

label input,
label select,
label textarea {
  margin-top: 0.25rem;
  font-weight: 400;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.checkbox-label input {
  width: auto;
  margin: 0;
}

fieldset {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  padding: 1rem;
  margin: 0 0 1.25rem;
}

legend {
  font-weight: 700;
  padding: 0 0.5rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.5rem 1rem;
  align-items: end;
}

.search-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1rem;
}

.search-form input {
  max-width: 360px;
}

.search-form select {
  max-width: 200px;
}

button {
  font: inherit;
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--button-bg);
  background: var(--button-bg);
  color: var(--button-text);
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background: var(--button-hover);
}

button.danger {
  background: var(--surface);
  color: var(--error-text);
  border-color: var(--error-border);
}

button.danger:hover {
  background: var(--error-bg);
}

button.small-button {
  padding: 0.15rem 0.5rem;
  font-size: 0.8rem;
}

.link-button {
  background: none;
  border: none;
  color: var(--header-text);
  padding: 0;
  cursor: pointer;
}

.link-button:hover {
  color: #fff;
  background: none;
}

.button-link {
  display: inline-block;
  padding: 0.4rem 0.9rem;
  border: 1px solid var(--text-soft);
  border-radius: 4px;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
}

.button-link:hover {
  background: var(--surface-alt);
}

.inline-form {
  display: inline;
}

.form-actions {
  display: flex;
  gap: 1rem;
  align-items: center;
  margin-top: 0.5rem;
}

.page-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.page-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

/* Detail page definition lists */

section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1rem 1.25rem;
  margin-bottom: 1.25rem;
}

section h2 {
  margin-top: 0;
}

dl {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.5rem 1.5rem;
  margin: 0;
}

dl div {
  display: flex;
  gap: 0.5rem;
}

dt {
  font-weight: 600;
  min-width: 140px;
  color: var(--text-soft);
}

dd {
  margin: 0;
  white-space: pre-wrap;
}

/* Photos */

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

.photo-card {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  align-items: flex-start;
}

.photo-card img {
  width: 100%;
  height: 140px;
  object-fit: cover;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-alt);
}

/* Tables scroll sideways instead of breaking the layout on narrow screens */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 1rem;
}

.table-wrap table {
  margin-bottom: 0;
}

/* Mobile */

@media (max-width: 640px) {
  header {
    padding: 0.5rem 0.75rem;
    flex-wrap: wrap;
    gap: 0.25rem 0.75rem;
  }

  header nav {
    gap: 0.85rem;
    flex-wrap: wrap;
  }

  main {
    margin: 1rem auto;
    padding: 0 0.75rem;
  }

  /* 16px stops iOS Safari from zooming in when a field is focused */
  input[type="text"],
  input[type="search"],
  input[type="password"],
  input[type="number"],
  input[type="file"],
  select,
  textarea {
    font-size: 16px;
  }

  .search-form {
    flex-wrap: wrap;
  }

  .search-form input[type="search"] {
    max-width: none;
    flex: 1 1 100%;
  }

  .search-form select {
    max-width: none;
    flex: 1 1 40%;
    min-width: 0;
  }

  th, td {
    padding: 0.45rem 0.5rem;
  }

  .hide-mobile {
    display: none;
  }

  section,
  fieldset {
    padding: 0.75rem;
  }

  dt {
    min-width: 110px;
  }

  /* Page action buttons (Import event / Refresh all stats, Edit / Delete)
     become two equal-width columns with breathing room */
  .page-title {
    margin-bottom: 0.75rem;
  }

  .page-title h1 {
    margin-bottom: 0.25rem;
  }

  .page-actions {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.6rem;
  }

  .page-actions .inline-form {
    display: block;
  }

  .page-actions button,
  .page-actions .button-link {
    width: 100%;
    text-align: center;
  }

  .search-form {
    gap: 0.6rem;
  }

  /* Bigger tap targets */
  button,
  .button-link {
    padding: 0.55rem 1rem;
  }

  button.small-button {
    padding: 0.3rem 0.6rem;
  }

  .login-page {
    margin: 3rem auto;
    max-width: none;
  }

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

/* Motion */

@keyframes page-enter {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@keyframes card-enter {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

@keyframes banner-enter {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: none; }
}

main {
  animation: page-enter 0.3s ease both;
}

.login-page {
  animation: card-enter 0.4s ease both;
}

.notice,
.error {
  animation: banner-enter 0.3s ease both;
}

/* Dark/light switch: the .theme-transition class is added for ~400ms by toggleTheme() */
.theme-transition,
.theme-transition *,
.theme-transition *::before,
.theme-transition *::after {
  transition: background-color 0.35s ease, color 0.35s ease, border-color 0.35s ease !important;
}

button,
.button-link,
.link-button {
  transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease,
              transform 0.15s ease, box-shadow 0.2s ease;
}

button:hover:not(:disabled),
.button-link:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
}

button:active:not(:disabled),
.button-link:active {
  transform: none;
  box-shadow: none;
}

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

.link-button:hover,
.link-button:active {
  transform: none;
  box-shadow: none;
}

header nav a {
  transition: color 0.2s ease;
}

a {
  transition: color 0.2s ease;
}

tbody tr {
  transition: background-color 0.15s ease;
}

input[type="text"],
input[type="search"],
input[type="password"],
input[type="number"],
input[type="file"],
select,
textarea {
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--button-bg);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

input[readonly],
textarea[readonly] {
  background: var(--surface-alt);
  color: var(--text-soft);
  cursor: default;
}

input[readonly]:focus,
textarea[readonly]:focus {
  border-color: var(--input-border);
  box-shadow: none;
}

.photo-card a {
  display: block;
  overflow: hidden;
  border-radius: 4px;
  width: 100%;
}

.photo-card img {
  transition: transform 0.25s ease;
}

.photo-card a:hover img {
  transform: scale(1.04);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
