:root {
  color-scheme: light dark;

  --paper: #ffffff;
  --ink: #16211e;
  --ink-soft: #66756f;
  --rule: #e5eae8;
  --rule-strong: #cbd4d0;
  --accent: #0d8c70;
  --accent-ink: #ffffff;
  --warn: #9a6207;
  --danger: #b8433f;

  --sans:
    Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  font-family: var(--sans);
  font-synthesis: none;
  text-rendering: optimizeLegibility;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #0f1412;
    --ink: #e9efec;
    --ink-soft: #93a29c;
    --rule: #253330;
    --rule-strong: #3a4d47;
    --accent: #43d0a8;
    --accent-ink: #0c1f1a;
    --warn: #e0a94f;
    --danger: #f08a86;
  }
}

* {
  box-sizing: border-box;
}

/* display: grid 等宣告的優先權高於 hidden 屬性，必須顯式壓回去。 */
[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  background: var(--paper);
}

body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-size: 14px;
  line-height: 1.6;
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

h1,
h2 {
  margin: 0;
  font-size: inherit;
  font-weight: 600;
}

p {
  margin: 0;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  clip-path: inset(50%);
  overflow: hidden;
  white-space: nowrap;
}

.mono,
.name-preview,
.stats dd,
#folder-name,
.source-item strong,
.rows-field input {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
}

/* ---------- shell ---------- */

.shell {
  max-width: 780px;
  margin: 0 auto;
  padding: 28px 24px 72px;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  color: inherit;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-decoration: none;
}

.brand-mark svg {
  display: block;
  width: 17px;
  height: 17px;
  fill: var(--accent);
}

.privacy-note {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--ink-soft);
  font-size: 12.5px;
}

.privacy-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.tabs {
  display: flex;
  gap: 20px;
  margin: 26px 0 0;
  border-bottom: 1px solid var(--rule);
}

.tab {
  padding: 0 0 10px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  color: var(--ink-soft);
  cursor: pointer;
}

.tab.is-active {
  border-bottom-color: var(--ink);
  color: var(--ink);
  font-weight: 600;
}

.lede {
  margin: 22px 0 26px;
  color: var(--ink-soft);
}

.banner {
  display: grid;
  gap: 2px;
  margin: 20px 0;
  padding: 12px 14px;
  border: 1px solid var(--rule-strong);
  border-left: 2px solid var(--warn);
  border-radius: 4px;
}

.banner span {
  color: var(--ink-soft);
  font-size: 13px;
}

/* ---------- setting rows ---------- */

.rows {
  border-top: 1px solid var(--rule);
}

.row {
  display: grid;
  grid-template-columns: 108px minmax(0, 1fr) auto;
  gap: 24px;
  align-items: start;
  padding: 18px 0;
  border-bottom: 1px solid var(--rule);
}

.row-label {
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 26px;
}

.row-body {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.row-value {
  font-size: 15px;
  font-weight: 600;
  line-height: 26px;
  overflow-wrap: anywhere;
}

.row-hint {
  color: var(--ink-soft);
  font-size: 12.5px;
  line-height: 1.5;
}

.row-hint.is-error {
  color: var(--danger);
}

.row-hint.is-warning {
  color: var(--warn);
}

.row-links {
  display: flex;
  gap: 14px;
}

.link-button {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 12.5px;
  cursor: pointer;
}

.link-button:disabled {
  color: var(--rule-strong);
  cursor: default;
}

.link-danger {
  color: var(--danger);
}

.name-preview {
  color: var(--ink-soft);
  font-size: 13.5px;
  font-weight: 400;
}

.name-preview strong {
  color: var(--ink);
  font-weight: 600;
}

.name-preview .sep {
  padding: 0 6px;
  color: var(--rule-strong);
}

/* ---------- source list ---------- */

.source-list {
  display: grid;
  gap: 4px;
  margin: 2px 0 0;
  padding: 0;
  list-style: none;
}

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

.file-meta {
  display: flex;
  gap: 10px;
  align-items: baseline;
  min-width: 0;
}

.file-meta strong {
  overflow: hidden;
  font-size: 13.5px;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-meta span {
  color: var(--ink-soft);
  font-family: var(--mono);
  font-size: 12px;
  white-space: nowrap;
}

.remove-file {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: none;
  color: var(--ink-soft);
  cursor: pointer;
}

.remove-file:hover:not(:disabled) {
  color: var(--danger);
}

.remove-file:disabled {
  opacity: 0.4;
  cursor: default;
}

.remove-file svg {
  width: 13px;
  height: 13px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
}

/* ---------- fields ---------- */

.rules-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 22px;
  align-items: center;
}

.rows-field {
  display: inline-flex;
  gap: 8px;
  align-items: baseline;
}

.rows-field input {
  /* 只支援 Chrome / Edge，可以直接用 field-sizing 讓底線貼齊數字寬度。 */
  min-width: 4ch;
  field-sizing: content;
  padding: 3px 0;
  border: 0;
  border-bottom: 1px solid var(--rule-strong);
  border-radius: 0;
  background: none;
  font-size: 15px;
  font-weight: 600;
  text-align: right;
}

.rows-field input:focus-visible {
  border-bottom-color: var(--accent);
  outline: none;
}

.rows-field input::-webkit-outer-spin-button,
.rows-field input::-webkit-inner-spin-button {
  margin: 0;
  -webkit-appearance: none;
}

.unit {
  color: var(--ink-soft);
  font-size: 12.5px;
}

.check-field {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  cursor: pointer;
}

.check-field input {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--accent);
}

/* ---------- buttons ---------- */

.button {
  padding: 7px 14px;
  border: 1px solid var(--rule-strong);
  border-radius: 6px;
  background: none;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
}

.button:hover:not(:disabled) {
  border-color: var(--ink-soft);
}

.button-primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.button-primary:hover:not(:disabled) {
  border-color: var(--accent);
  filter: brightness(1.06);
}

.button-danger {
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
  color: var(--danger);
}

.button:disabled {
  border-color: var(--rule);
  background: none;
  color: var(--rule-strong);
  cursor: default;
}

/* ---------- run bar ---------- */

.run-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  padding: 20px 0;
}

.run-state {
  display: inline-flex;
  gap: 9px;
  align-items: center;
  color: var(--ink-soft);
  font-size: 12.5px;
}

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

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--rule-strong);
}

.status-ready {
  background: var(--accent);
}

.status-running {
  background: var(--accent);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.25;
  }
}

/* ---------- progress ---------- */

.progress {
  display: grid;
  gap: 8px;
  padding: 18px 0 20px;
  border-top: 1px solid var(--rule);
}

.progress-line {
  display: flex;
  gap: 10px;
  align-items: baseline;
  font-size: 13px;
}

.progress-line span {
  color: var(--ink-soft);
  font-size: 12.5px;
  white-space: nowrap;
}

#current-file-name {
  overflow: hidden;
  flex: 1;
  font-weight: 500;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#batch-percent {
  font-size: 15px;
}

.track {
  height: 3px;
  border-radius: 2px;
  background: var(--rule);
}

.track span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width 0.2s ease;
}

.track-thin {
  height: 2px;
  opacity: 0.55;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: 12px;
  margin: 8px 0 0;
}

.stats div {
  display: grid;
  gap: 1px;
}

.stats dt {
  color: var(--ink-soft);
  font-size: 11.5px;
}

.stats dd {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
}

/* ---------- result ---------- */

.result {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px 10px;
  padding: 14px 0;
  border-top: 1px solid var(--rule);
}

.result-icon {
  grid-row: span 2;
  align-self: center;
  color: var(--accent);
  font-size: 15px;
}

.result strong {
  font-size: 14px;
}

.result p {
  color: var(--ink-soft);
  font-size: 12.5px;
}

.result.is-warning .result-icon {
  color: var(--warn);
}

.result.is-error .result-icon {
  color: var(--danger);
}

/* ---------- log ---------- */

.log {
  padding-top: 18px;
  border-top: 1px solid var(--rule);
}

.log-heading {
  display: flex;
  gap: 16px;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
}

.log-heading h2 {
  color: var(--ink-soft);
  font-size: 13px;
  font-weight: 400;
}

.log-list {
  max-height: 232px;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  list-style: none;
}

.log-entry {
  display: grid;
  grid-template-columns: auto 58px minmax(0, 1fr);
  gap: 10px;
  padding: 3px 0;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
  animation: fade-in 0.12s ease;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

.log-time {
  color: var(--ink-soft);
}

.log-level {
  color: var(--ink-soft);
  letter-spacing: 0.04em;
}

.log-entry[data-level="SUCCESS"] .log-level {
  color: var(--accent);
}

.log-entry[data-level="WARN"] .log-level {
  color: var(--warn);
}

.log-entry[data-level="ERROR"] .log-level {
  color: var(--danger);
}

.log-message {
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

/* ---------- dialog & toast ---------- */

.confirm-dialog {
  max-width: 340px;
  padding: 20px;
  border: 1px solid var(--rule-strong);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
}

.confirm-dialog::backdrop {
  background: rgb(10 20 18 / 40%);
}

.confirm-dialog h2 {
  margin-bottom: 6px;
  font-size: 15px;
}

.confirm-dialog p {
  color: var(--ink-soft);
  font-size: 13px;
}

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

.toast-region {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: grid;
  gap: 8px;
  justify-items: end;
}

.toast {
  padding: 8px 12px;
  border: 1px solid var(--rule-strong);
  border-radius: 6px;
  background: var(--paper);
  font-size: 12.5px;
  animation: fade-in 0.15s ease;
}

/* ---------- responsive ---------- */

@media (width <= 620px) {
  .shell {
    padding: 20px 16px 56px;
  }

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

  .row-label {
    line-height: 1.4;
  }

  .row-action:empty {
    display: none;
  }

  .run-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .run-actions .button {
    flex: 1;
  }

  .toast-region {
    right: 16px;
    left: 16px;
    justify-items: stretch;
  }
}

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