:root {
  color-scheme: light dark;
  --ink: #1d2b24;
  --muted: #737d75;
  --soft: #a4aba4;
  --line: #e8ebe6;
  --paper: #fbfcfa;
  --green: #2f674f;
  --green-dark: #234f3d;
  --mint: #e9f2ec;
  --orange: #e9a363;
  --card: #fff;
  --field: #fcfdfb;
  --chip: #f5f7f4;
  --hover: #f7faf7;
  --line-soft: var(--line-soft);
  --dash: var(--dash);
  --text-2: #5d685f;
  --shadow: rgba(37, 56, 44, .05);
  --glow: var(--glow);
  --err-bg: #fff9f8;
  --err-line: var(--err-line);
  --err-ink: #a4493f;
  --font: "Segoe UI", system-ui, -apple-system, "Noto Sans Khmer", "Khmer UI", "Khmer OS System", "Leelawadee UI", "Khmer Sangam MN", Arial, sans-serif;
  font-family: var(--font);
  font-synthesis: none;
  text-rendering: optimizeLegibility;
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(ellipse at 9% 12%, var(--glow), transparent 30rem),
    var(--paper);
  color: var(--ink);
}

button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { width: min(1120px, calc(100% - 64px)); margin: 0 auto; }

.topbar {
  height: 82px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -.7px;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 31px;
  height: 31px;
  place-items: center;
  border-radius: 10px;
  background: var(--green);
  color: #fff;
  font-family: var(--font);
  font-size: 21px;
  font-weight: 800;
}

.brand-light { margin-left: 4px; color: var(--muted); font-weight: 500; }
.topbar-meta { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #d3a062; }
.status-dot.ready { background: #63a579; box-shadow: 0 0 0 3px #63a5791c; }
.status-dot.error { background: #cf6259; }

.intro { padding: 56px 0 36px; text-align: center; }
.eyebrow { display: inline-flex; align-items: center; gap: 8px; color: var(--green); font-size: 12px; font-weight: 700; letter-spacing: 1.65px; }
.sparkle { color: var(--orange); font-size: 17px; }
h1, h2, p { margin-top: 0; }
.intro h1 { margin: 0 0 14px; font-family: var(--font); font-size: clamp(40px, 5vw, 58px); font-weight: 600; line-height: 1.1; letter-spacing: -3.1px; }
.intro h1 .accent { color: var(--green); }
.intro p { margin-bottom: 0; color: var(--muted); font-size: 15px; }

.workspace { width: min(100%, 920px); margin: 0 auto; }
.language-bar { display: flex; min-height: 67px; align-items: center; gap: 12px; padding: 10px 20px; border: 1px solid var(--line); border-bottom: 0; border-radius: 13px 13px 0 0; background: var(--card); }
.language-field { display: flex; min-width: 160px; flex-direction: column; gap: 3px; }
.language-field > span, .access-field > span { color: var(--soft); font-size: 10.5px; font-weight: 700; letter-spacing: 1.2px; }
.language-field select { width: fit-content; min-width: 145px; padding: 3px 25px 3px 0; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 13px; font-weight: 600; }
.swap-button { display: grid; width: 34px; height: 34px; flex: 0 0 auto; place-items: center; border: 1px solid var(--line); border-radius: 50%; background: var(--card); color: var(--muted); transition: color .15s, border-color .15s; }
.swap-button:hover { border-color: var(--green); color: var(--green); }
.swap-button svg { width: 17px; }
.target-field { padding-left: 7px; }
.access-field { display: flex; max-width: 270px; flex: 1; flex-direction: column; gap: 4px; margin-left: auto; }
.access-field input { width: 100%; padding: 5px 8px; border: 1px solid var(--line); border-radius: 6px; outline: 0; font-size: 12px; }
.access-field input:focus { border-color: var(--green); }

.tool-tabs { display: flex; align-items: stretch; gap: 3px; padding: 12px 19px 0; border: 1px solid var(--line); border-bottom: 0; background: var(--card); }
.tool-tab { display: inline-flex; min-width: 90px; align-items: center; justify-content: center; gap: 8px; padding: 11px 16px 13px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); font-size: 12px; font-weight: 600; transition: color .15s, border-color .15s; }
.tool-tab:hover { color: var(--green); }
.tool-tab.active { border-bottom-color: var(--green); color: var(--green); }
.tab-icon { font-family: var(--font); font-size: 15px; font-weight: 600; }

.editor-card { overflow: hidden; border: 1px solid var(--line); border-radius: 0 0 13px 13px; background: var(--card); box-shadow: 0 15px 40px var(--shadow); }
.tool-panel { padding: 23px 24px 0; }
.panel-heading { display: flex; min-height: 43px; align-items: flex-start; justify-content: space-between; gap: 18px; }
.panel-heading h2 { margin: 0 0 5px; font-family: var(--font); font-size: 15px; font-weight: 700; letter-spacing: -.2px; }
.panel-heading p { margin: 0; color: var(--muted); font-size: 12px; }
.format-label { padding: 6px 8px; border-radius: 5px; background: var(--chip); color: var(--muted); font-size: 10.5px; font-weight: 600; letter-spacing: .45px; white-space: nowrap; }

#text-input { display: block; width: 100%; min-height: 188px; resize: vertical; margin: 17px 0 0; padding: 16px; border: 1px solid var(--line-soft); border-radius: 8px; outline: none; background: var(--field); color: var(--ink); font-size: 14px; line-height: 1.7; transition: border-color .15s, background .15s; }
#text-input:focus { border-color: var(--green); background: var(--card); }
#text-input::placeholder { color: var(--soft); }
.editor-footer { display: flex; min-height: 65px; align-items: center; justify-content: space-between; gap: 16px; color: var(--soft); font-size: 12.5px; }
.translate-button { display: inline-flex; min-height: 38px; align-items: center; gap: 21px; padding: 0 14px; border: 0; border-radius: 7px; background: var(--green); color: white; font-size: 12.5px; font-weight: 600; transition: background .15s, transform .15s; }
.translate-button:hover { transform: translateY(-1px); background: var(--green-dark); }
.translate-button:disabled { cursor: wait; opacity: .63; transform: none; }
.translate-button span:last-child { font-size: 15px; }

.upload-zone { display: flex; min-height: 182px; flex-direction: column; align-items: center; justify-content: center; gap: 10px; margin: 18px 0 0; border: 1px dashed var(--dash); border-radius: 9px; background: var(--field); text-align: center; cursor: pointer; transition: border .15s, background .15s; }
.upload-zone:hover, .upload-zone.dragging { border-color: var(--green); background: var(--hover); }
.upload-zone input { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
.upload-icon { display: grid; width: 38px; height: 38px; place-items: center; border: 1px solid var(--line); border-radius: 11px; background: var(--card); color: var(--green); font-size: 20px; }
.upload-zone strong { color: var(--text-2); font-size: 12px; font-weight: 500; }
.upload-zone strong span { color: var(--green); font-weight: 700; }
.upload-zone small { color: var(--soft); font-size: 12px; }
.editor-card:has(.tool-panel:not([hidden])) .tool-panel[hidden] { display: none; }

.progress-message { display: flex; min-height: 48px; align-items: center; gap: 10px; padding: 0 24px; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--green); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.result-card { margin-top: 17px; padding: 23px 25px 20px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); box-shadow: 0 12px 30px var(--shadow); }
.result-heading { display: flex; align-items: center; justify-content: space-between; gap: 15px; }
.result-kicker, .transcript-block > span { color: var(--soft); font-size: 10.5px; font-weight: 700; letter-spacing: 1.15px; }
.result-heading h2 { margin: 5px 0 0; font-family: var(--font); font-size: 15px; font-weight: 700; }
.copy-button, .text-download-button { padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--text-2); font-size: 12px; font-weight: 600; }
.copy-button:hover, .text-download-button:hover { border-color: var(--green); color: var(--green); }
.transcript-block { margin-top: 19px; padding: 13px 14px; border-radius: 7px; background: var(--chip); }
.transcript-block p { margin: 7px 0 0; color: var(--text-2); font-size: 12px; line-height: 1.7; white-space: pre-wrap; }
.translation-result { margin-top: 17px; color: var(--ink); font-family: var(--font); font-size: 16px; line-height: 1.75; white-space: pre-wrap; overflow-wrap: anywhere; }
.result-actions { display: flex; align-items: center; gap: 8px; margin-top: 17px; padding-top: 14px; border-top: 1px solid var(--line-soft); }
.text-download-button { display: inline-flex; align-items: center; gap: 9px; }
.speak-button { display: inline-flex; align-items: center; gap: 7px; padding: 8px 10px; border: 0; border-radius: 6px; background: var(--mint); color: var(--green); font-size: 12px; font-weight: 700; }
.speak-button:hover { filter: brightness(.96); }
.speak-button:disabled { cursor: wait; opacity: .62; }
.speech-list { display: grid; gap: 10px; margin-top: 15px; }
.speech-item { display: grid; grid-template-columns: 90px 1fr auto; align-items: center; gap: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 7px; }
.speech-item span { color: var(--muted); font-size: 12px; }
.speech-item audio { width: 100%; height: 34px; }
.speech-item a { color: var(--green); font-size: 12px; font-weight: 700; text-decoration: none; }
.speech-item a:hover { text-decoration: underline; }
.error-message { margin: 14px 0 0; padding: 11px 14px; border: 1px solid var(--err-line); border-radius: 7px; background: var(--err-bg); color: var(--err-ink); font-size: 12px; line-height: 1.55; }

.page-footer { display: flex; width: min(920px, 100%); justify-content: space-between; gap: 20px; margin: 23px auto 28px; color: var(--soft); font-size: 12px; }

[hidden] { display: none !important; }

@media (max-width: 680px) {
  .app-shell { width: min(100% - 32px, 540px); }
  .topbar { height: 68px; }
  .intro { padding: 36px 0 26px; }
  .intro h1 { letter-spacing: -2.3px; }
  .intro p { max-width: 340px; margin: 0 auto; font-size: 13px; line-height: 1.6; }
  .language-bar { flex-wrap: wrap; gap: 9px; padding: 12px 14px; }
  .language-field { min-width: 0; flex: 1; }
  .language-field select { min-width: 0; max-width: 100%; font-size: 12px; }
  .swap-button { width: 31px; height: 31px; }
  .target-field { padding-left: 3px; }
  .access-field { min-width: 100%; margin: 3px 0 0; }
  .tool-tabs { justify-content: space-between; gap: 0; padding: 8px 9px 0; }
  .tool-tab { min-width: 0; flex: 1; gap: 5px; padding: 10px 6px 12px; font-size: 12.5px; }
  .tab-icon { font-size: 13px; }
  .tool-panel { padding: 19px 15px 0; }
  .panel-heading { min-height: 50px; }
  .panel-heading h2 { font-size: 14px; }
  .panel-heading p { max-width: 250px; font-size: 12.5px; line-height: 1.5; }
  .format-label { font-size: 10px; }
  #text-input { min-height: 170px; padding: 13px; font-size: 16px; }
  .upload-zone { min-height: 168px; }
  .editor-footer { min-height: 61px; font-size: 10.5px; }
  .translate-button { min-height: 36px; gap: 10px; padding: 0 10px; font-size: 12px; }
  .result-card { padding: 19px 16px 16px; }
  .translation-result { font-size: 15px; }
  .speech-item { grid-template-columns: 1fr auto; gap: 5px 10px; }
  .speech-item audio { grid-column: 1 / -1; grid-row: 2; }
  .page-footer { flex-direction: column; gap: 7px; margin-top: 17px; font-size: 10.5px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* ---- Dark mode ---- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
  --ink: #e6ece8;
  --muted: #9aa69e;
  --soft: #74807a;
  --line: #27322c;
  --paper: #111714;
  --green: #6fbf93;
  --green-dark: #8fd1ad;
  --mint: #1d3329;
  --card: #18201c;
  --field: #141b17;
  --chip: #1e2722;
  --hover: #1b2620;
  --line-soft: #222c27;
  --dash: #34423a;
  --text-2: #b4beb8;
  --shadow: rgba(0, 0, 0, .3);
  --glow: rgba(47, 103, 79, .18);
  --err-bg: #2a1b19;
  --err-line: #4b2b27;
  --err-ink: #f0a59c;
  }
}
:root[data-theme="dark"] {
  --ink: #e6ece8;
  --muted: #9aa69e;
  --soft: #74807a;
  --line: #27322c;
  --paper: #111714;
  --green: #6fbf93;
  --green-dark: #8fd1ad;
  --mint: #1d3329;
  --card: #18201c;
  --field: #141b17;
  --chip: #1e2722;
  --hover: #1b2620;
  --line-soft: #222c27;
  --dash: #34423a;
  --text-2: #b4beb8;
  --shadow: rgba(0, 0, 0, .3);
  --glow: rgba(47, 103, 79, .18);
  --err-bg: #2a1b19;
  --err-line: #4b2b27;
  --err-ink: #f0a59c;
}
:root[data-theme="dark"], :root[data-theme="dark"] body { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }
.translate-button { color: #fff; }
:root[data-theme="dark"] .translate-button { background: #2f674f; }
:root[data-theme="dark"] .translate-button:hover { background: #3a7a5e; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .translate-button { background: #2f674f; }
  :root:not([data-theme="light"]) .translate-button:hover { background: #3a7a5e; }
}
select option { background: var(--card); color: var(--ink); }

/* ---- Topbar controls ---- */
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.ui-lang { display: inline-flex; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--card); }
.ui-lang button { min-width: 40px; padding: 5px 9px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 700; }
.ui-lang button[aria-pressed="true"] { background: var(--mint); color: var(--green); }
.icon-button { display: grid; width: 32px; height: 32px; place-items: center; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--muted); }
.icon-button:hover { color: var(--green); border-color: var(--green); }
.icon-button svg { width: 17px; height: 17px; }
.icon-button .moon { display: none; }
:root[data-theme="dark"] .icon-button .sun { display: none; }
:root[data-theme="dark"] .icon-button .moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) .icon-button .sun { display: none; }
  :root:not([data-theme]) .icon-button .moon { display: block; }
}

/* ---- Text editor extras ---- */
.textarea-wrap { position: relative; }
.clear-button { position: absolute; top: 26px; right: 10px; display: grid; width: 26px; height: 26px; place-items: center; border: 0; border-radius: 50%; background: var(--chip); color: var(--muted); font-size: 17px; line-height: 1; }
.clear-button:hover { color: var(--ink); }
.shortcut-hint { margin-left: 12px; color: var(--soft); }
.shortcut-hint::before { content: "·"; margin-right: 12px; }
@media (hover: none) { .shortcut-hint { display: none; } }

/* ---- Image preview ---- */
.upload-zone { position: relative; }
.image-preview { max-width: min(100%, 320px); max-height: 150px; border-radius: 8px; border: 1px solid var(--line); object-fit: contain; }
.upload-zone:has(.image-preview:not([hidden])) .upload-icon { display: none; }

/* ---- Results ---- */
.result-heading h2:empty { display: none; }
.translation-result { font-size: 17px; }
.copy-button.copied { border-color: var(--green); color: var(--green); }

/* ---- History ---- */
.history { margin-top: 22px; }
.history-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.history-heading h2 { margin: 0; color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: .6px; text-transform: uppercase; }
.link-button { padding: 4px 6px; border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 600; }
.link-button:hover { color: var(--err-ink); }
.history-list { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.history-item { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--ink); text-align: left; transition: border-color .15s; }
.history-item:hover { border-color: var(--green); }
.history-item small { display: block; margin-bottom: 4px; color: var(--soft); font-size: 10.5px; font-weight: 700; letter-spacing: .8px; text-transform: uppercase; }
.history-item p { display: -webkit-box; margin: 0; overflow: hidden; color: var(--text-2); font-size: 13px; line-height: 1.55; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow-wrap: anywhere; }
.history-item p.out { color: var(--ink); }

/* ---- Khmer script needs more room ---- */
:lang(km) { line-height: 1.7; }
html[lang="km"] .eyebrow, html[lang="km"] .language-field > span, html[lang="km"] .access-field > span, html[lang="km"] .result-kicker, html[lang="km"] .format-label, html[lang="km"] .history-heading h2 { letter-spacing: 0; }
html[lang="km"] .intro h1 { letter-spacing: 0; line-height: 1.35; font-size: clamp(32px, 4.4vw, 50px); }
.translation-result:lang(km), #text-input:lang(km) { font-size: 18px; line-height: 1.9; }

@media (max-width: 680px) {
  .topbar-actions { gap: 8px; }
  .topbar-meta #service-status { display: none; }
  .history-item { grid-template-columns: 1fr; gap: 6px; }
  .clear-button { top: 22px; }
}
.brand-mark { background: #2f674f; }
.translate-button { white-space: nowrap; }
.editor-footer > span:first-child { min-width: 0; }
html[lang="km"] .eyebrow, html[lang="km"] .language-field > span, html[lang="km"] .access-field > span, html[lang="km"] .result-kicker, html[lang="km"] .transcript-block > span, html[lang="km"] .format-label, html[lang="km"] .history-item small, html[lang="km"] .history-heading h2 { font-size: 12.5px; }
