:root {
  --bg: #ffffff;
  --surface: #f6f7f9;
  --border: #dfe2e7;
  --text: #1c2027;
  --muted: #646b78;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --folder: #d99a2b;
  --file: #8a93a3;
  --error-bg: #fdecec;
  --error-text: #a11d1d;
  --row-hover: #f0f4fb;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

/* Dark tokens apply when the OS prefers dark and no theme is forced,
   or when dark is chosen explicitly. Keep the two blocks in sync. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171c;
    --surface: #1c2027;
    --border: #2e333d;
    --text: #e3e6eb;
    --muted: #98a0ad;
    --accent: #5b9dff;
    --accent-text: #0b0d10;
    --folder: #e8b04d;
    --file: #7d8696;
    --error-bg: #3a1c1c;
    --error-text: #ffb3b3;
    --row-hover: #20262f;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #14171c;
  --surface: #1c2027;
  --border: #2e333d;
  --text: #e3e6eb;
  --muted: #98a0ad;
  --accent: #5b9dff;
  --accent-text: #0b0d10;
  --folder: #e8b04d;
  --file: #7d8696;
  --error-bg: #3a1c1c;
  --error-text: #ffb3b3;
  --row-hover: #20262f;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

button,
select,
input {
  font: inherit;
  color: inherit;
}

button,
.action-link {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled) {
  border-color: var(--muted);
}
button:disabled {
  opacity: 0.45;
  cursor: default;
}

input,
select {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

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

/* ---------- header ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
}
.topbar h1 {
  font-size: 16px;
  margin: 0;
}
.topbar h1 a {
  color: var(--text);
}
.location-form {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 0;
}
#location-input {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
}
#region-input {
  width: 150px;
}
.location-form button[type="submit"] {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

main {
  flex: 1;
  padding: 16px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

/* ---------- welcome ---------- */

.welcome {
  max-width: 640px;
}
.examples {
  font-family: var(--mono);
  padding-left: 20px;
}
.examples li {
  margin: 4px 0;
}
.note {
  color: var(--muted);
}

/* ---------- path + controls ---------- */

.pathbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.breadcrumbs {
  font-family: var(--mono);
  font-size: 15px;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  white-space: nowrap;
  overflow: hidden; /* JS collapses middle crumbs into "…" so this rarely clips */
}
.breadcrumbs > * {
  flex: none;
}
.breadcrumbs > .current {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 600;
}
.breadcrumbs > .copy-btn {
  margin-left: 8px;
}
.breadcrumbs .sep {
  color: var(--muted);
  margin: 0 4px;
}

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.controls label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
}
.search-input {
  flex: 1 1 240px; /* fills the row; the paging controls keep their size */
  min-width: 0;
  font-family: var(--mono);
}
#page-size-custom {
  width: 80px;
}
.page-info {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.status {
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--surface);
  margin-bottom: 8px;
}
.status.error {
  background: var(--error-bg);
  color: var(--error-text);
}

/* ---------- listing ---------- */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
#listing {
  width: 100%;
  border-collapse: collapse;
}
#listing th,
#listing td {
  padding: 6px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
#listing tbody tr:last-child td {
  border-bottom: none;
}
#listing tbody tr:hover {
  background: var(--row-hover);
}
#listing thead th {
  background: var(--surface);
  font-weight: 600;
  white-space: nowrap;
}
#listing th button {
  background: none;
  border: none;
  padding: 0;
  font-weight: 600;
}
#listing th button::after {
  content: attr(data-arrow);
  margin-left: 4px;
  font-size: 10px;
  color: var(--muted);
}
.col-name {
  font-family: var(--mono);
  overflow-wrap: anywhere;
}
#listing thead .col-name {
  font-family: inherit;
}
.col-size,
.col-modified {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.col-size {
  text-align: right !important;
}
.col-actions {
  white-space: nowrap;
  text-align: right !important;
}
.col-actions > * + * {
  margin-left: 6px;
}
.copy-btn,
.action-link {
  font-size: 12px;
  padding: 2px 8px;
  color: var(--text);
}
.action-link:hover {
  text-decoration: none;
  border-color: var(--muted);
}
/* Icon-only buttons and links (copy, open, download, close) */
.copy-btn,
.icon-btn,
.icon-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 3px 6px;
  vertical-align: middle;
  color: var(--muted);
}
.copy-btn:hover,
.icon-btn:hover,
.icon-link:hover {
  color: var(--text);
}
.copy-btn svg,
.icon-btn svg,
.icon-link svg {
  display: block;
}

button.link {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font-family: var(--mono);
  text-align: left;
  white-space: normal;
  overflow-wrap: anywhere;
}
button.link:hover {
  text-decoration: underline;
}

.icon {
  display: inline-block;
  width: 14px;
  height: 11px;
  margin-right: 8px;
  vertical-align: -1px;
  border-radius: 2px;
}
.icon-folder {
  background: var(--folder);
  clip-path: polygon(0 0, 40% 0, 50% 18%, 100% 18%, 100% 100%, 0 100%);
}
.icon-file {
  width: 10px;
  height: 13px;
  margin: 0 10px 0 2px;
  vertical-align: -2px;
  background: var(--file);
  clip-path: polygon(0 0, 65% 0, 100% 30%, 100% 100%, 0 100%);
}

.hint {
  color: var(--muted);
  font-size: 12px;
}

/* ---------- preview ---------- */

dialog#preview {
  width: min(1100px, calc(100vw - 32px));
  height: min(85vh, 900px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  flex-direction: column;
}
dialog#preview[open] {
  display: flex;
}
dialog::backdrop {
  background: rgba(0, 0, 0, 0.45);
}
.preview-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.preview-title {
  flex: 1;
  min-width: 0;
  font-family: var(--mono);
  overflow-wrap: anywhere;
}
.preview-title .muted {
  margin-left: 8px;
  font-family: inherit;
}
.preview-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}
.preview-notice {
  padding: 6px 14px;
  font-size: 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
}
.preview-body {
  flex: 1;
  overflow: auto;
}
.preview-body pre {
  margin: 0;
  padding: 12px 14px;
  font: 12.5px/1.5 var(--mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.preview-image,
.preview-video {
  display: block;
  max-width: 100%;
  max-height: 100%;
  margin: auto;
  object-fit: contain;
}
.preview-video {
  width: 100%;
  height: 100%;
  background: #000;
}
.viewer-body .preview-video {
  height: auto;
  max-height: calc(100vh - 190px);
}
table.csv {
  border-collapse: collapse;
  font: 12.5px/1.4 var(--mono);
}
table.csv th,
table.csv td {
  border: 1px solid var(--border);
  padding: 3px 8px;
  white-space: nowrap;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
}
table.csv th {
  position: sticky;
  top: 0;
  background: var(--surface);
}

/* ---------- toast + footer ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 13px;
  max-width: calc(100vw - 32px);
  overflow-wrap: anywhere;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
  z-index: 10;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

footer {
  padding: 12px 16px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  border-top: 1px solid var(--border);
}

@media (max-width: 640px) {
  .topbar {
    padding: 10px 16px;
  }
  .location-form {
    flex-wrap: wrap;
    flex-basis: 100%;
  }
  .theme-switch {
    order: 1;
  }
  .location-form {
    order: 2;
  }
  #location-input {
    flex-basis: 100%;
  }
  #region-input {
    flex: 1;
  }
  .search-input {
    flex-basis: 100%;
  }
  .col-modified {
    display: none;
  }
}

/* ---------- theme switch ---------- */

.theme-switch {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  margin-left: auto;
}
.theme-switch button {
  border: none;
  border-radius: 0;
  background: var(--bg);
  color: var(--muted);
  padding: 5px 8px;
  display: inline-flex;
  align-items: center;
}
.theme-switch button + button {
  border-left: 1px solid var(--border);
}
.theme-switch button:hover {
  color: var(--text);
}
.theme-switch button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--accent);
}
.theme-switch svg {
  display: block;
}

/* ---------- full-page file viewer ---------- */

.viewer-notice {
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 8px 8px 0 0;
}
.viewer-body {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: auto;
  max-height: calc(100vh - 190px);
}
.viewer-notice:not([hidden]) + .viewer-body {
  border-radius: 0 0 8px 8px;
}
