:root {
  color-scheme: light;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-soft: #fbfbfd;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #d8d8df;
  --line-soft: #ececf1;
  --blue: #0066cc;
  --blue-soft: #eaf3ff;
  --green: #157f3b;
  --orange: #a45b00;
  --shadow: 0 10px 30px rgb(0 0 0 / 7%);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  letter-spacing: 0;
}

a {
  color: inherit;
}

button,
input,
select {
  font: inherit;
}

.shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
}

h1 {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.15;
  letter-spacing: 0;
}

.toolbar {
  position: sticky;
  top: 10px;
  z-index: 2;
  display: grid;
  gap: 8px;
  align-items: center;
  margin-top: 16px;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: rgb(255 255 255 / 92%);
  padding: 10px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.search-box,
.filter-control {
  position: relative;
  display: grid;
  gap: 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 650;
}

.search-box > span,
.filter-control > span {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.search-box {
  /* removed grid-column override */
}

input {
  width: 100%;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 0 12px;
  outline: none;
}

input:focus,
button:focus-visible,
a:focus-visible {
  outline: 3px solid rgb(0 102 204 / 24%);
  outline-offset: 2px;
}

.segment {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f2f2f4;
  padding: 3px;
}

.segment button {
  min-height: 30px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}

.segment button.is-active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 4px rgb(0 0 0 / 10%);
}

.filters-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
}

.custom-select {
  position: relative;
  color: var(--text);
}

.select-button {
  display: flex;
  width: 100%;
  min-height: 36px;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 0 12px;
  text-align: left;
  cursor: pointer;
}

.select-button span:first-child {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.select-icon {
  width: 16px;
  height: 16px;
  color: var(--muted);
  fill: none;
  flex: 0 0 auto;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
  transition: transform 140ms ease;
}

.custom-select.is-open .select-icon {
  transform: rotate(180deg);
}

.custom-select.is-open .select-button {
  border-color: #9fc5ef;
  box-shadow: 0 0 0 4px rgb(0 102 204 / 12%);
}

.select-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 8;
  display: none;
  width: 100%;
  max-height: 280px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 6px;
  box-shadow: 0 16px 46px rgb(0 0 0 / 14%);
}

.custom-select.is-open .select-menu {
  display: grid;
  gap: 2px;
}

.select-option {
  display: flex;
  width: 100%;
  min-height: 36px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  padding: 0 10px;
  text-align: left;
  cursor: pointer;
}

.select-option:hover,
.select-option.is-selected {
  background: var(--blue-soft);
  color: var(--blue);
}

.select-count {
  color: var(--muted);
  font-size: 0.78rem;
}

.result-bar {
  margin: 18px 0 10px;
}

.result-bar p {
  margin: 0;
  color: var(--muted);
}

.records {
  display: grid;
  gap: 10px;
  padding-bottom: 40px;
}

.record {
  display: grid;
  grid-template-columns: 148px minmax(0, 1fr);
  gap: 16px;
  align-items: start;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: var(--surface);
  padding: 12px;
  cursor: pointer;
  transition: border-color 140ms ease, box-shadow 140ms ease, transform 140ms ease;
}

.record:hover {
  border-color: #c8c8d2;
  box-shadow: 0 8px 22px rgb(0 0 0 / 5%);
  transform: translateY(-1px);
}

.record:focus-visible {
  outline: 3px solid rgb(0 102 204 / 20%);
  outline-offset: 2px;
}

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

.record-thumb {
  width: 100%;
  aspect-ratio: 16 / 11;
  overflow: hidden;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #f7f7f8;
}

.record-thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.record-top {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.record-index {
  flex: 0 0 auto;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.record-title {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 1rem;
  line-height: 1.32;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0;
}

.chip {
  display: inline-flex;
  min-height: 26px;
  align-items: center;
  border-radius: 999px;
  background: #f0f0f2;
  color: var(--muted);
  padding: 0 9px;
  font-size: 0.78rem;
  font-weight: 650;
}

.chip.pdf {
  background: var(--blue-soft);
  color: var(--blue);
}

.chip.image {
  background: #edf8f1;
  color: var(--green);
}

.chip.video {
  background: #fff3df;
  color: var(--orange);
}

.description {
  display: -webkit-box;
  margin: 0;
  overflow: hidden;
  color: #3b3b40;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
}

.empty-state {
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 32px;
  text-align: center;
}

.empty-state h2 {
  margin: 0 0 8px;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
}

@keyframes dialogEnter {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes backdropEnter {
  from { background: rgb(0 0 0 / 0%); }
  to { background: rgb(0 0 0 / 28%); }
}

.detail-dialog {
  width: min(900px, calc(100% - 28px));
  border: 0;
  border-radius: 12px; /* Slightly rounder */
  padding: 0;
  box-shadow: 0 24px 80px rgb(0 0 0 / 22%);
}

.detail-dialog[open] {
  animation: dialogEnter 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.detail-dialog::backdrop {
  background: rgb(0 0 0 / 28%);
}

.detail-dialog[open]::backdrop {
  animation: backdropEnter 0.25s ease-out forwards;
}

.detail-dialog article {
  position: relative;
  max-height: min(760px, 88vh);
  overflow: auto;
  padding: 28px; /* Slightly more breathing room */
}

.dialog-close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 50%; /* Circle looks better for close buttons */
  background: #fff;
  color: var(--muted);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.15s ease, border-color 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog-close:hover {
  background: var(--surface-soft);
  color: var(--text);
  border-color: var(--muted);
  transform: scale(1.05);
}

.dialog-title {
  max-width: calc(100% - 46px);
  margin: 0 0 12px;
  overflow-wrap: anywhere;
  font-size: 1.35rem;
  line-height: 1.25;
}

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

.fact {
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 10px 12px;
}

.fact span {
  display: block;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}

.fact strong {
  display: block;
  margin-top: 2px;
  overflow-wrap: anywhere;
}

.dialog-description {
  margin: 0 0 18px;
  color: #333338;
}

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

.preview-panel {
  display: grid;
  gap: 10px;
  margin: 18px 0;
}

.preview {
  width: 100%;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
  background: #000; /* Darker background looks better for media */
}

.media-preview {
  max-height: 420px;
  object-fit: contain;
}

.document-image-preview {
  width: min(100%, 420px);
  max-height: 320px;
  object-fit: contain;
}

@media (max-width: 780px) {
  .record,
  .filters-row {
    grid-template-columns: 1fr;
  }

  .record {
    grid-template-columns: 108px minmax(0, 1fr);
    gap: 12px;
  }

  .toolbar {
    position: static;
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 14px;
  }

  .toolbar-top {
    flex-direction: column;
  }

  .search-box,
  .filter-control {
    gap: 6px;
  }

  .search-box > span,
  .filter-control > span {
    position: static;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
  }

  input,
  .select-button {
    min-height: 42px;
  }

  .segment {
    gap: 6px;
    padding: 4px;
  }

  .segment button {
    min-height: 36px;
  }

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

@media (max-width: 460px) {
  .shell {
    width: min(100% - 20px, 1180px);
  }

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

  .record {
    grid-template-columns: 92px minmax(0, 1fr);
    padding: 10px;
  }

  .description {
    -webkit-line-clamp: 4;
  }

  .result-bar {
    align-items: flex-start;
    flex-direction: column;
  }

}
