:root {
  --bg: #0f1420;
  --bg-panel: #171d2b;
  --bg-input: #0c111b;
  --border: #2a3346;
  --border-strong: #3a465e;
  --text: #e6ebf4;
  --text-dim: #97a2b8;
  --accent: #4f8cff;
  --accent-hover: #6ba0ff;
  --ok: #37c98b;
  --err: #ff6b6b;
  --err-bg: #3a1f27;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding: 0 20px 40px;
  max-width: 1280px;
  margin-inline: auto;
}

.app-header { padding: 28px 0 12px; }
.app-header h1 { margin: 0; font-size: 1.6rem; }
.subtitle { margin: 6px 0 0; color: var(--text-dim); font-size: 0.92rem; }

/* ---- Tabs ---- */
.tabs { display: flex; gap: 6px; margin: 18px 0 0; border-bottom: 1px solid var(--border); }
.tab {
  appearance: none; border: none; background: transparent; color: var(--text-dim);
  font: inherit; font-weight: 600; padding: 12px 18px; cursor: pointer;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab:hover { color: var(--text); }
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); }
.tab-sub { color: var(--text-dim); font-weight: 400; font-size: 0.85em; }

/* ---- Panels ---- */
.panel { padding: 22px 0 0; }
.panel[hidden] { display: none; }

/* Side-by-side: input controls on the left, output on the right. */
.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  align-items: start;
}
.output-pane { display: flex; flex-direction: column; position: relative; }

.controls { display: flex; flex-direction: column; gap: 12px; }

.field { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.field label { color: var(--text-dim); font-size: 0.9rem; }
.file-name { color: var(--text-dim); font-size: 0.88rem; font-family: var(--mono); }

.hint { margin: 4px 0 0; color: var(--text-dim); font-size: 0.85rem; }
.hint.inline { margin: 0; }
code { font-family: var(--mono); background: var(--bg-input); padding: 1px 5px; border-radius: 4px; }

textarea.io {
  min-height: 150px;
  resize: vertical;
}
.io {
  width: 100%;
  font-family: var(--mono);
  font-size: 0.85rem;
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
}
.io:focus { outline: none; border-color: var(--accent); }

.app-field input[type="text"] {
  font-family: var(--mono);
  background: var(--bg-input);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 7px 10px;
  min-width: 200px;
}
.app-field input[type="text"]:focus { outline: none; border-color: var(--accent); }
.app-field.is-hidden { display: none; }

/* ---- Toolbar ---- */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spacer { flex: 1 1 auto; }

.segmented { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg {
  appearance: none; border: none; background: transparent; color: var(--text-dim);
  font: inherit; font-size: 0.85rem; padding: 7px 14px; cursor: pointer;
}
.seg + .seg { border-left: 1px solid var(--border-strong); }
.seg:hover { color: var(--text); }
.seg.is-active { background: var(--accent); color: #fff; }

/* ---- Buttons ---- */
.btn {
  appearance: none; font: inherit; font-size: 0.88rem; font-weight: 600;
  color: var(--text); background: var(--bg-panel);
  border: 1px solid var(--border-strong); border-radius: 8px;
  padding: 8px 16px; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; }
.file-btn { display: inline-flex; align-items: center; }
.file-btn input { position: absolute; }

/* ---- Messages ---- */
.msg { border-radius: 8px; padding: 10px 14px; font-size: 0.88rem; }
.msg.error { background: var(--err-bg); color: var(--err); border: 1px solid var(--err); }
.msg.success { color: var(--ok); border: 1px solid var(--ok); background: transparent; }

/* ---- Output ---- */
.output-head { display: flex; align-items: center; justify-content: space-between; margin: 0 0 8px; min-height: 38px; }
.output-head h2 { font-size: 1.05rem; margin: 0; }
.output-actions { display: flex; gap: 8px; }
.output {
  flex: 1 1 auto;
  min-height: 320px;
  max-height: 70vh;
  overflow: auto;
  white-space: pre;
  margin: 0;
}

/* The encode preview <pre>: wrap long base64 so it never scrolls sideways. */
pre.output { white-space: pre-wrap; word-break: break-all; overflow-wrap: anywhere; }

/* Loading state: a spinner over the output pane so every run is felt,
   even when the resulting output is identical to the previous one. */
.output-pane.is-loading::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  background: rgba(12, 17, 27, 0.66);
  border-radius: 10px;
}
.output-pane.is-loading::after {
  content: "";
  position: absolute;
  z-index: 6;
  top: 50%;
  left: 50%;
  width: 30px;
  height: 30px;
  margin: -15px 0 0 -15px;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .output-pane.is-loading::after { animation-duration: 1.6s; }
}

/* The encode preview is an editable textarea, not a <pre>. */
textarea.output { min-height: 320px; resize: vertical; white-space: pre-wrap; word-break: break-all; }

/* Stack the two columns on narrow screens. */
@media (max-width: 760px) {
  .split { grid-template-columns: 1fr; }
  .output-head { margin-top: 8px; }
}
.output .key-head {
  color: var(--accent-hover);
  font-weight: 700;
  display: block;
  margin: 14px 0 4px;
}
.output .key-head:first-child { margin-top: 0; }
.output .raw-note { color: var(--err); }

.app-footer { margin-top: 32px; color: var(--text-dim); font-size: 0.8rem; text-align: center; }
.footer-sep { margin: 0 8px; opacity: 0.6; }
.repo-link { color: var(--accent); text-decoration: none; }
.repo-link:hover { color: var(--accent-hover); text-decoration: underline; }

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb; --bg-panel: #ffffff; --bg-input: #ffffff;
    --border: #d9dee9; --border-strong: #c4ccdb;
    --text: #1a2233; --text-dim: #5f6b83;
    --err-bg: #fdeaea;
  }
}
