:root {
  --bg: #fff;
  --accent: #f15a29;
  --text: #1a2233;
  --muted: #64748b;
  --panel: #fff;
  --border: 6.5px solid;
  --panel-border: #e5e5e5;
  --panel2: color-mix(in srgb, var(--accent) 12%, white);
  --radius: 14px;
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1420;
    --text: #e8ebf1;
    --muted: #8b93a3;
    --panel: #151c2b;
    --panel-border: #1b2334;
    --panel2: color-mix(in srgb, var(--accent) 11%, #151c2b);
    color-scheme: dark;
  }
}

@font-face {
  font-family: 'fredoka';
  src: url('font/FredokaOne-Regular.ttf') format('truetype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

* { box-sizing: border-box; }

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

main {
  flex: 1;
  width: min(1400px, 96vw);
  margin: 0 auto;
  padding: 1rem 0 2rem;
}

footer {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}
footer a { color: var(--accent); }

.ad-footer {
  margin: 1.5rem auto 0;
  max-width: 728px;
  min-height: 90px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel2);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  overflow: hidden;
}
@media (max-width: 640px) {
  .ad-footer { min-height: 50px; }
}

header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  padding: 4rem 1rem 8rem;
  color: var(--accent);
  font-family: 'fredoka', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1 { margin: 0; font-size: 6rem; letter-spacing: 0.5px; }

.logo-wrap {
  position: relative;
  height: 6rem;
  flex-shrink: 0;
}

.logo { height: 100%; width: 100%; display: block; }

.eye-pupil {
  position: absolute;
  width: 15px;
  height: auto;
  pointer-events: none;
  will-change: transform;
}

#eye-left { left: 30%; top: 47.5%; }
#eye-right { right: 30%; top: 47.5%; }

.translator {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 0.75rem;
  align-items: start;
}

.translator[aria-busy="true"] { opacity: 0.5; pointer-events: none; }

.lang-section {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.lang-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  min-width: 0;
}

.lang-header > div { min-width: 0; }

.flag {
  width: 60px;
  height: 40px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
}

.lang-name {
  font-size: 1.8em;
  font-weight: 600;
  color: var(--text);
  line-height: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lang-native {
  font-size: 1rem;
  color: var(--muted);
  font-weight: 400;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lang-abbr { display: none; }

.panel {
  background: var(--panel);
  border: var(--border) var(--panel-border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: border-color 150ms ease;
}

.panel:focus-within {
  border-color: color-mix(in srgb, var(--accent) 55%, var(--panel-border));
}

.panel-output {
  border: 0;
  background: var(--panel2);
}

textarea {
  flex: 1 1 auto;
  min-height: 140px;
  resize: none;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 2rem;
  line-height: 1.5;
  padding: 1rem;
  font-family: inherit;
}

textarea::placeholder { color: var(--muted); }
#output { color: var(--accent); }

.panel-action {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem 0.5rem;
}

.char-count {
  margin-right: auto;
  padding-left: 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}
.char-count.over-limit { color: #e88; font-weight: 600; }

.panel-action button {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  background: transparent;
  border: 0;
  border-radius: 10px;
  color: var(--muted);
  font-size: 1.5rem;
  cursor: pointer;
  touch-action: manipulation;
}
.panel-action button:active { color: var(--accent); background: var(--panel2); }
.panel-action button:disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }

/* Collapsed instead of display:none: the iconify-icon custom element inside
   needs a real box to measure itself against on first render, or its icon
   stays blank forever even after being un-hidden. */
.panel-action button.is-collapsed {
  min-width: 0;
  width: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
  opacity: 0;
  pointer-events: none;
}

@media (hover: hover) {
  .panel-action button:hover { color: var(--accent); }
}

.swap {
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--accent);
  border: 0;
  border-radius: 50%;
  width: 48px;
  height: 48px;
  font-size: 2.2rem;
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
}
.swap:active { background: var(--panel2); transform: scale(0.94); }

@media (hover: hover) {
  .swap:hover { background: var(--panel2); }
}

@keyframes slide-out-right {
  from { transform: translateX(0);    opacity: 1; }
  to   { transform: translateX(40px); opacity: 0; }
}
@keyframes slide-out-left {
  from { transform: translateX(0);     opacity: 1; }
  to   { transform: translateX(-40px); opacity: 0; }
}
@keyframes slide-in-from-left {
  from { transform: translateX(-40px); opacity: 0; }
  to   { transform: translateX(0);     opacity: 1; }
}
@keyframes slide-in-from-right {
  from { transform: translateX(40px); opacity: 0; }
  to   { transform: translateX(0);    opacity: 1; }
}

.slide-out-right  { animation: slide-out-right      160ms ease forwards; }
.slide-out-left   { animation: slide-out-left        160ms ease forwards; }
.slide-in-left    { animation: slide-in-from-left    160ms ease forwards; }
.slide-in-right   { animation: slide-in-from-right   160ms ease forwards; }

@media (prefers-reduced-motion: reduce) {
  .slide-out-right,
  .slide-out-left,
  .slide-in-left,
  .slide-in-right { animation: none; }
  .panel, .swap { transition: none; }
}

@media (max-width: 640px) {
  header { padding-bottom: 1rem; gap: 0.6rem; }
  .logo-wrap { display: none; }
  h1 { font-size: 2.618rem; } /* φ² */

  main {
    width: 100%;
    padding: max(1rem, env(safe-area-inset-top))
             1rem
             max(1.5rem, env(safe-area-inset-bottom));
  }

  .translator :focus { outline: none; }
  .panel:focus-within { border-color: var(--panel-border); }

  /* .lang-section becomes display:contents so its panel and chip become
     direct items of .translator, free to reorder independently */
  .translator {
    display: flex;
    flex-wrap: wrap;
    gap: 0;
  }
  .lang-section { display: contents; }

  /* source + target merged into one card, split by a centered 90% divider */
  .panel {
    order: 4;
    flex: 0 0 100%;
    background: var(--panel);
    border: 1px solid var(--panel-border);
  }
  #section-src .panel {
    border-bottom: 0;
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .panel-output {
    order: 5;
    background: var(--panel);
    border-top: 0;
    border-radius: 0 0 var(--radius) var(--radius);
    position: relative;
  }
  .panel-output::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    height: 1px;
    background: var(--panel-border);
  }

  /* top bar: source chip · switch · target chip, above the card */
  #section-src .lang-header { order: 1; }
  .swap { order: 2; }
  #section-tgt .lang-header { order: 3; }

  /* pill: circular flag + language code — name/native stay desktop-only */
  .flag { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; }
  .lang-name, .lang-native { display: none; }
  .lang-abbr {
    display: block;
    font-size: 1.3em;
    font-weight: 700;
    color: var(--text);
    line-height: 1;
  }

  .lang-header {
    flex: 1 1 0;
    flex-direction: row;
    min-width: 0;
    justify-content: flex-start;
    align-items: center;
    gap: 0.618rem;           /* 1/φ */
    margin-bottom: 0.618rem; /* 1/φ */
    padding: 0.618rem 1rem;  /* 1/φ · 1 */
    background: transparent;
    border: 1px solid var(--panel-border);
    border-radius: 999px;
    text-align: left;
    overflow: hidden;
  }

  .lang-header > div { min-width: 0; width: auto; }

  .swap {
    flex: 0 0 auto;
    margin: 0 0.382rem 0.618rem; /* 1/φ² · 1/φ */
    background: var(--panel2);
  }

  textarea { font-size: 1.05rem; min-height: 100px; padding: 0.85rem; }

  /* .lang-section is display:contents (no box to animate) — redirect the
     swap transition to the visible .lang-header child instead */
  .lang-section.slide-out-right .lang-header { animation: slide-out-right      160ms ease forwards; }
  .lang-section.slide-out-left  .lang-header { animation: slide-out-left       160ms ease forwards; }
  .lang-section.slide-in-left   .lang-header { animation: slide-in-from-left   160ms ease forwards; }
  .lang-section.slide-in-right  .lang-header { animation: slide-in-from-right  160ms ease forwards; }
}
