:root {
  --ink: #14201c;
  --ink-soft: #2a3a34;
  --paper: #f3efe6;
  --mist: rgba(243, 239, 230, 0.72);
  --accent: #1f7a5c;
  --accent-strong: #0f5a42;
  --english: #0e4d6b;
  --line: rgba(20, 32, 28, 0.12);
  --glow: rgba(31, 122, 92, 0.28);
  --shadow: 0 24px 60px rgba(12, 24, 20, 0.18);
  --font-display: "Bricolage Grotesque", "IBM Plex Sans JP", sans-serif;
  --font-body: "IBM Plex Sans JP", "Bricolage Grotesque", sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  color: var(--ink);
  font-family: var(--font-body);
  background: #0d1a16;
}

.atmosphere {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  background:
    radial-gradient(ellipse 80% 55% at 15% 10%, rgba(56, 140, 110, 0.35), transparent 55%),
    radial-gradient(ellipse 70% 50% at 90% 20%, rgba(30, 90, 120, 0.28), transparent 50%),
    linear-gradient(165deg, #163028 0%, #0d1a16 42%, #12242c 100%);
  animation: drift 18s ease-in-out infinite alternate;
}

@keyframes drift {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(-1.5%);
  }
}

.topbar {
  display: flex;
  justify-content: flex-end;
  align-items: baseline;
  gap: 1rem;
  padding: 1.25rem 1.5rem 0;
  max-width: 960px;
  margin: 0 auto;
  min-height: 1.5rem;
}

.lang-direction {
  --lang-pad-x: 0.8rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  justify-self: center;
  align-self: center;
  gap: 0.2rem;
  padding: 0.3rem;
  border: 1px solid rgba(243, 239, 230, 0.22);
  background: rgba(8, 18, 15, 0.55);
  backdrop-filter: blur(8px);
  z-index: 1;
}

.lang-side {
  border: 0;
  background: transparent;
  color: rgba(243, 239, 230, 0.7);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.45rem var(--lang-pad-x);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}

.lang-side.is-source {
  background: rgba(243, 239, 230, 0.14);
  color: var(--paper);
}

.lang-side:hover {
  color: var(--paper);
}

.swap-button {
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: rgba(243, 239, 230, 0.85);
  font: inherit;
  font-size: 1.05rem;
  line-height: 1;
  width: 2.1rem;
  height: 2.1rem;
  padding: 0;
  cursor: pointer;
  border-radius: 50%;
  /* Only the source label carries a filled background, so its box edge sits a
     padding width closer to the arrow than the target label's glyphs do.
     Shifting by half that padding makes the two gaps read as equal. */
  transform: translateX(calc(var(--lang-pad-x) / 2));
  transition: background 0.2s ease, transform 0.2s ease;
}

.swap-button svg {
  display: block;
  width: 1.15rem;
  height: 1.15rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.swap-button:hover {
  background: rgba(243, 239, 230, 0.12);
}

.swap-button:active {
  transform: translateX(calc(var(--lang-pad-x) / 2)) rotate(180deg);
}

.swap-button:focus-visible,
.lang-side:focus-visible {
  outline: 2px solid var(--paper);
  outline-offset: 2px;
}

.status {
  margin: 0;
  color: rgba(243, 239, 230, 0.72);
  font-size: 0.9rem;
  text-align: right;
  max-width: min(100%, 28rem);
}

.stage {
  max-width: 960px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem clamp(0.85rem, 4vw, 1.5rem) 3rem;
  box-sizing: border-box;
  overflow-x: clip;
}

.hero {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 3.5rem 0 1.75rem;
  min-height: 0;
}

.brand-hero {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(2.25rem, 7vw, 4.25rem);
  font-weight: 700;
  line-height: 0.9;
  letter-spacing: -0.04em;
  color: var(--paper);
  animation: rise 0.9s ease both;
}

.tagline {
  margin: 1rem 0 0.35rem;
  font-family: var(--font-display);
  font-size: clamp(1.35rem, 3.5vw, 2rem);
  font-weight: 500;
  color: rgba(243, 239, 230, 0.92);
  animation: rise 0.9s ease 0.12s both;
}

.lead {
  margin: 0 0 1.5rem;
  color: rgba(243, 239, 230, 0.68);
  font-size: 1.05rem;
  line-height: 1.6;
  overflow-wrap: anywhere;
  animation: rise 0.9s ease 0.22s both;
}

.mode-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: nowrap;
  width: 100%;
  min-width: 0;
}

.mode-switch {
  display: grid;
  grid-template-columns: repeat(4, auto);
  align-items: center;
  justify-content: space-evenly;
  gap: 0;
  box-sizing: border-box;
  width: 100%;
  margin: 0;
  padding: 0.25rem;
  border: 1px solid rgba(243, 239, 230, 0.18);
  background: rgba(8, 18, 15, 0.35);
  max-width: 100%;
  overflow: hidden;
}

.mode-button {
  border: 0;
  background: transparent;
  color: rgba(243, 239, 230, 0.7);
  font: inherit;
  font-size: clamp(0.66rem, 2.3vw, 0.86rem);
  font-weight: 500;
  min-width: 0;
  padding: 0.5rem clamp(0.1rem, 0.5vw, 0.25rem);
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
  white-space: nowrap;
  text-align: center;
}

.mode-button.mode-action {
  color: rgba(155, 231, 197, 0.9);
}

.mode-button.mode-action:hover {
  color: #c9f0dd;
}

.mode-button.mode-action.has-key {
  color: #9be7c5;
  font-weight: 600;
}

.mode-button:hover {
  color: var(--paper);
}

.mode-button.is-active {
  background: rgba(243, 239, 230, 0.14);
  color: var(--paper);
}

.mode-button:focus-visible {
  outline: 2px solid var(--paper);
  outline-offset: 2px;
}

.settings {
  margin: 0 0 1.25rem;
  padding: 1.2rem 1.3rem 1.3rem;
  border-top: 1px solid rgba(243, 239, 230, 0.18);
  background: rgba(243, 239, 230, 0.1);
  color: var(--paper);
  animation: rise 0.6s ease both;
}

.settings h2 {
  margin: 0 0 0.4rem;
  font-size: 0.95rem;
  font-weight: 600;
}

.settings-note {
  margin: 0 0 0.9rem;
  color: rgba(243, 239, 230, 0.7);
  font-size: 0.9rem;
  line-height: 1.5;
}

.settings-note a {
  color: #9be7c5;
}

.settings-note code {
  font-size: 0.85em;
  color: #c9f0dd;
}

.key-status {
  margin: 0 0 0.75rem;
  font-size: 0.9rem;
  font-weight: 500;
}

.key-status.is-ready {
  color: #9be7c5;
}

.key-status.is-missing {
  color: #f0c9a0;
}

.provider-switch {
  display: inline-flex;
  gap: 0.3rem;
  margin: 0 0 0.85rem;
  padding: 0.25rem;
  border: 1px solid rgba(243, 239, 230, 0.18);
  background: rgba(8, 18, 15, 0.35);
}

.provider-button {
  position: relative;
  border: 0;
  background: transparent;
  color: rgba(243, 239, 230, 0.7);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
}

.provider-tooltip {
  position: absolute;
  z-index: 30;
  left: 50%;
  bottom: calc(100% + 0.55rem);
  width: max-content;
  max-width: min(15rem, 75vw);
  padding: 0.55rem 0.65rem;
  border-radius: 3px;
  background: var(--ink);
  color: var(--paper);
  box-shadow: 0 5px 18px rgba(8, 18, 15, 0.24);
  font-size: 0.76rem;
  font-weight: 500;
  line-height: 1.5;
  text-align: left;
  white-space: normal;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.provider-button:first-child .provider-tooltip {
  left: 0;
  transform: translate(0, 4px);
}

.provider-button:last-child .provider-tooltip {
  right: 0;
  left: auto;
  transform: translate(0, 4px);
}

.provider-button:hover .provider-tooltip,
.provider-button:focus-visible .provider-tooltip {
  opacity: 1;
  transform: translate(-50%, 0);
}

.provider-button:first-child:hover .provider-tooltip,
.provider-button:first-child:focus-visible .provider-tooltip,
.provider-button:last-child:hover .provider-tooltip,
.provider-button:last-child:focus-visible .provider-tooltip {
  transform: translate(0, 0);
}

.provider-button.is-active {
  background: rgba(243, 239, 230, 0.14);
  color: var(--paper);
}

.provider-button.has-key:not(.is-active)::after {
  content: " ✓";
  color: #9be7c5;
}

.provider-button.is-active.has-key::after {
  content: " ✓";
  color: #9be7c5;
}

.settings #apiKeyInput {
  flex: 1;
  min-width: 0;
  border: 1px solid rgba(243, 239, 230, 0.22);
  background: rgba(8, 18, 15, 0.45);
  color: var(--paper);
  padding: 0.7rem 0.85rem;
  font: inherit;
  font-size: 0.95rem;
}

.settings #apiKeyInput:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.settings #apiKeyInput::placeholder {
  color: rgba(243, 239, 230, 0.45);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.exchange {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-top: 0.5rem;
  align-items: start;
  min-width: 0;
  max-width: 100%;
}

.exchange-left {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.55rem;
  min-width: 0;
}

.exchange-left .panel {
  width: 100%;
}

@media (max-width: 799px) {
  .exchange .exchange-left {
    order: 1;
  }

  .exchange .lang-direction {
    order: 2;
  }

  .exchange .panel.english {
    order: 3;
  }

  .exchange .swap-button svg {
    transform: rotate(90deg);
  }
}

@media (min-width: 800px) {
  .exchange {
    grid-template-columns: 1fr auto 1fr;
    grid-template-rows: auto 1fr;
    gap: 0.85rem 0.85rem;
    align-items: start;
  }

  .exchange-left {
    display: contents;
  }

  .mode-bar {
    grid-column: 1;
    grid-row: 1;
    justify-self: stretch;
  }

  .panel.japanese {
    grid-column: 1;
    grid-row: 2;
  }

  .lang-direction {
    grid-column: 2;
    grid-row: 2;
    align-self: center;
    margin-top: 0;
  }

  .panel.english {
    grid-column: 3;
    grid-row: 2;
    margin-top: 0;
  }

  .panel.japanese,
  .panel.english {
    min-height: 14.5rem;
    height: 100%;
  }
}

.panel {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding: 1.35rem 1.4rem 1.4rem;
  border-top: 1px solid var(--line);
  background: #ffffff;
  color: var(--ink);
  animation: rise 0.8s ease both;
  min-height: 14.5rem;
  min-width: 0;
  max-width: 100%;
}

.panel h2 {
  margin: 0 0 0.75rem;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.panel-head h2 {
  margin: 0;
}

.panel-text {
  margin: 0;
  flex: 1;
  min-height: 3.2rem;
  font-size: 1.15rem;
  line-height: 1.65;
  color: var(--ink);
}

.panel-text.answer {
  font-family: var(--font-display);
  font-size: clamp(1.25rem, 2.4vw, 1.55rem);
  font-weight: 500;
  color: var(--english);
  line-height: 1.45;
}

.chat-english-box {
  margin-top: 1rem;
  padding: 0.9rem 1rem;
  border: 1px solid rgba(20, 32, 28, 0.18);
  background: rgba(255, 255, 255, 0.48);
}

.chat-english-label {
  display: block;
  margin-bottom: 0.35rem;
  color: var(--ink-soft);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.chat-english-box p {
  margin: 0;
  color: var(--english);
  font-family: var(--font-display);
  font-size: 1.05rem;
  line-height: 1.5;
}

.vocab-word {
  position: relative;
  display: inline-block;
  border-bottom: 1px dotted var(--accent);
  cursor: help;
  outline: none;
  transition: background 0.15s ease, color 0.15s ease;
}

.vocab-word:hover,
.vocab-word:focus {
  background: rgba(31, 122, 92, 0.2);
  color: var(--accent-strong);
}

.japanese-word-highlight {
  padding: 0 0.08em;
  border-radius: 2px;
  background: rgba(31, 122, 92, 0.22);
  color: var(--accent-strong);
}

.vocab-word.is-speaking {
  background: rgba(31, 122, 92, 0.2);
  color: var(--accent-strong);
}

.vocab-word:focus-visible {
  border-radius: 2px;
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.vocab-tooltip {
  position: absolute;
  z-index: 20;
  display: grid;
  grid-template-columns: 2.5em auto;
  column-gap: 0.65rem;
  row-gap: 0.2rem;
  left: 50%;
  bottom: calc(100% + 0.45rem);
  width: max-content;
  max-width: min(17rem, 75vw);
  padding: 0.45rem 0.6rem;
  border-radius: 3px;
  background: var(--ink);
  color: var(--paper);
  box-shadow: 0 5px 18px rgba(8, 18, 15, 0.2);
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-weight: 500;
  line-height: 1.4;
  text-align: left;
  white-space: normal;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 4px);
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.vocab-tooltip-label {
  color: #9be7c5;
  font-weight: 600;
}

.vocab-word:hover .vocab-tooltip,
.vocab-word:focus .vocab-tooltip {
  opacity: 1;
  transform: translate(-50%, 0);
}

.history {
  margin-top: 1.25rem;
  padding: 1.1rem 1.2rem;
  border-top: 1px solid var(--line);
  background: #ffffff;
  min-width: 0;
  max-width: 100%;
  overflow-x: clip;
}

.history-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.85rem;
}

.history-head h2 {
  margin: 0;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: 0.08em;
}

.history-head span {
  color: var(--ink-soft);
  font-size: 0.78rem;
}

.history-controls {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.history-controls button {
  padding: 0.3rem 0.55rem;
  border: 1px solid rgba(20, 32, 28, 0.16);
  background: rgba(255, 255, 255, 0.45);
  color: var(--ink);
  font: inherit;
  font-size: 0.76rem;
  cursor: pointer;
}

.history-controls button:hover {
  background: rgba(255, 255, 255, 0.75);
}

.history-list {
  display: grid;
  gap: 0.7rem;
  max-height: 32rem;
  overflow-y: auto;
}

.history-more {
  display: block;
  width: 100%;
  margin-top: 0.75rem;
  padding: 0.65rem;
  border: 1px solid rgba(20, 32, 28, 0.16);
  background: rgba(255, 255, 255, 0.45);
  color: var(--ink);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}

.history-more[hidden] {
  display: none;
}

.history-more:hover {
  background: rgba(255, 255, 255, 0.7);
}

.scroll-jump {
  position: fixed;
  z-index: 50;
  right: 1rem;
  bottom: 1rem;
  display: grid;
  gap: 0.4rem;
}

.scroll-jump button {
  display: grid;
  width: 2.65rem;
  height: 2.65rem;
  padding: 0;
  place-items: center;
  border: 1px solid rgba(243, 239, 230, 0.28);
  border-radius: 50%;
  background: rgba(20, 32, 28, 0.9);
  color: var(--paper);
  box-shadow: 0 5px 18px rgba(8, 18, 15, 0.25);
  font: inherit;
  font-size: 1.15rem;
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: background 0.15s ease, transform 0.15s ease;
}

.scroll-jump button:hover {
  background: var(--accent-strong);
  transform: translateY(-1px);
}

.scroll-jump button:focus-visible {
  outline: 2px solid var(--paper);
  outline-offset: 2px;
}

.history-item {
  padding: 0.85rem 0.95rem;
  border: 2px solid #000000;
  background: rgba(255, 255, 255, 0.5);
}

.history-item-head {
  display: grid;
  grid-template-columns: 5.5rem minmax(0, 1fr);
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.65rem;
}

.history-mode {
  justify-self: start;
  padding: 0.16rem 0.5rem;
  background: var(--ink);
  color: var(--paper);
  font-size: 0.7rem;
  font-weight: 600;
}

.history-mode-controls {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.history-play {
  display: grid;
  width: 1.45rem;
  height: 1.45rem;
  padding: 0;
  place-items: center;
  border: 1px solid rgba(20, 32, 28, 0.18);
  border-radius: 50%;
  background: transparent;
  color: var(--accent-strong);
  font: inherit;
  font-size: 0.58rem;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.history-play:hover,
.history-play:focus-visible {
  background: var(--accent-strong);
  color: var(--paper);
}

.history-time {
  color: var(--ink-soft);
  font-size: 0.72rem;
}

.history-content {
  display: grid;
  grid-template-columns: 5.5rem minmax(0, 1fr);
  gap: 0.3rem 0.75rem;
}

.history-label {
  color: var(--ink-soft);
  font-size: 0.76rem;
  font-weight: 600;
}

.history-text {
  margin: 0;
  color: var(--ink);
  font-size: 0.92rem;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.history-text .vocab-tooltip {
  top: calc(100% + 0.45rem);
  bottom: auto;
}

@media (max-width: 540px) {
  .topbar {
    padding-left: 0.85rem;
    padding-right: 0.85rem;
  }

  .history-content {
    grid-template-columns: 1fr;
    gap: 0.15rem;
  }

  .history-item-head {
    grid-template-columns: minmax(0, auto) minmax(0, 1fr);
    gap: 0.45rem;
  }

  .history-text + .history-label {
    margin-top: 0.45rem;
  }

  .scroll-jump {
    right: 0.65rem;
    bottom: 0.65rem;
  }
}

.panel-text.updating {
  animation: flicker 0.45s ease;
}

@keyframes flicker {
  from {
    opacity: 0.35;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.text-row {
  display: flex;
  gap: 0.5rem;
  margin-top: auto;
  padding-top: 1.1rem;
}

#textInput {
  flex: 1;
  min-width: 0;
  border: 2px solid #000000;
  background: rgba(255, 255, 255, 0.55);
  color: var(--ink);
  padding: 0.7rem 0.85rem;
  font: inherit;
  font-size: 0.95rem;
}

#textInput:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.send-button,
.icon-button {
  border: 0;
  background: var(--ink);
  color: var(--paper);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  padding: 0.7rem 1rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s ease, transform 0.15s ease;
}

.send-button:hover,
.icon-button:hover {
  background: var(--accent-strong);
}

.clear-button {
  background: #6b736f;
}

.clear-button:hover {
  background: #5a615e;
}

.send-button:active,
.icon-button:active {
  transform: translateY(1px);
}

.speak-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1rem;
  background: #9aa39e;
  color: rgba(255, 255, 255, 0.92);
}

.speak-toggle:hover {
  background: #8a938e;
}

.speak-toggle.is-on {
  background: var(--accent);
  color: var(--paper);
}

.speak-toggle.is-on:hover {
  background: var(--accent-strong);
}

.speak-toggle:not(.is-on) .speak-wave {
  opacity: 0.35;
}

button.icon-button.mic-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1rem;
  background: #9aa39e;
  color: rgba(255, 255, 255, 0.92);
}

button.icon-button.mic-toggle:hover:not(:disabled),
button.icon-button.mic-toggle:focus-visible:not(:disabled) {
  background: #8a938e;
  color: rgba(255, 255, 255, 0.92);
}

button.icon-button.mic-toggle[aria-pressed="true"] {
  background: var(--accent);
  color: var(--paper);
  box-shadow: 0 0 0 3px var(--glow);
}

button.icon-button.mic-toggle[aria-pressed="true"]:hover:not(:disabled) {
  background: var(--accent-strong);
}

button.icon-button.mic-toggle:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  background: #9aa39e;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
