/* ============== Reset & Base ============== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
  touch-action: manipulation;
}

/* ============== Theme Variables ============== */
:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #1f1f1f;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --word-hover: rgba(0, 0, 0, 0.06);
  --word-active: rgba(255, 220, 50, 0.45);
  --popup-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  --font-base: 18px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --surface: #2a2a2a;
    --text: #e5e7eb;
    --text-muted: #9ca3af;
    --border: #3a3a3a;
    --word-hover: rgba(255, 255, 255, 0.08);
    --word-active: rgba(255, 220, 50, 0.25);
    --popup-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  }
}
[data-theme="light"] {
  --bg: #fafafa; --surface: #ffffff; --text: #1f1f1f; --text-muted: #6b7280;
  --border: #e5e7eb; --word-hover: rgba(0,0,0,0.06); --word-active: rgba(255,220,50,0.45);
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: #1a1a1a; --surface: #2a2a2a; --text: #e5e7eb; --text-muted: #9ca3af;
  --border: #3a3a3a; --word-hover: rgba(255,255,255,0.08); --word-active: rgba(255,220,50,0.25);
  color-scheme: dark;
}

/* ============== Toolbar ============== */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.brand {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  margin-right: auto;
  letter-spacing: -0.01em;
}
.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: background 120ms, border-color 120ms;
  white-space: nowrap;
  min-height: 36px;
}
.btn:hover { background: var(--word-hover); }
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-secondary { background: transparent; }
.btn-danger {
  color: var(--danger);
  border-color: var(--danger);
}
.btn-icon {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px 8px;
  position: relative;
  color: var(--text);
  font-family: inherit;
  border-radius: 6px;
  line-height: 1;
  min-width: 36px;
  min-height: 36px;
}
.btn-icon:hover { background: var(--word-hover); }
.btn-icon:active { transform: scale(0.92); }
.dialog-header .btn-icon {
  width: 44px;
  height: 44px;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.dot {
  position: absolute;
  top: 0;
  right: 0;
  color: var(--danger);
  font-size: 18px;
  line-height: 1;
  pointer-events: none;
  font-weight: bold;
}

.select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  min-height: 36px;
}

.font-size-label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--text-muted);
  user-select: none;
}
#font-size {
  width: 80px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ============== Main ============== */
main {
  max-width: 680px;
  margin: 0 auto;
  padding: 32px 20px 120px;
}

.welcome {
  text-align: center;
  padding: 60px 16px;
  color: var(--text-muted);
}
.welcome h2 {
  font-size: 24px;
  margin: 0 0 12px;
  color: var(--text);
  font-weight: 600;
}
.welcome p {
  margin: 6px 0;
  line-height: 1.6;
}
.welcome .hint {
  margin-top: 24px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  display: inline-block;
  text-align: left;
  font-size: 14px;
}
.welcome .hint code {
  background: var(--word-hover);
  padding: 1px 6px;
  border-radius: 3px;
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  font-size: 13px;
}

.paste-area {
  width: 100%;
  min-height: 280px;
  padding: 14px;
  font-size: 15px;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  resize: vertical;
  line-height: 1.5;
}
.paste-area:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.paste-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 12px;
}

/* ============== Reading Area ============== */
.reading-area {
  font-size: var(--font-base);
  line-height: 2.0;
  letter-spacing: 0.01em;
  word-spacing: 0.04em;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  /* Wrap at word boundaries; only break a single word if it can't fit the line. */
  overflow-wrap: break-word;
  max-width: 100%;
}
.reading-area p {
  margin: 0 0 1.5em 0;
  text-indent: 0;
}
.word {
  cursor: pointer;
  border-radius: 3px;
  padding: 1px 1px;
  transition: background 100ms;
  -webkit-touch-callout: none;
}
.word:hover { background: var(--word-hover); }
.word.active { background: var(--word-active); }

/* ============== Popup ============== */
.popup {
  position: absolute;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--popup-shadow);
  padding: 12px 16px;
  max-width: 360px;
  min-width: 180px;
  font-size: 15px;
  pointer-events: auto;
  line-height: 1.4;
  transition: opacity 120ms;
  opacity: 0;
}
.popup.visible { opacity: 1; }
.popup-word {
  font-size: 17px;
  font-weight: 600;
  margin-bottom: 4px;
  word-break: break-word;
}
.popup-ipa {
  color: var(--text-muted);
  font-family: "Charis SIL", "Doulos SIL", "Lucida Sans Unicode", "Arial Unicode MS", serif;
  margin-bottom: 6px;
  font-size: 15px;
  word-break: break-word;
}
.popup-translation {
  color: var(--text);
  word-break: break-word;
}
.popup-loading, .popup-error {
  color: var(--text-muted);
  font-style: italic;
  font-size: 13px;
}
.popup-error {
  color: var(--danger);
  font-style: normal;
}

/* Mobile: bottom sheet */
@media (max-width: 768px) {
  .popup {
    position: fixed;
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
    max-width: none;
    bottom: 8px;
    top: auto !important;
    border-radius: 14px;
    padding: 14px 18px;
    font-size: 16px;
  }
  .popup-word { font-size: 19px; }
  .popup-ipa { font-size: 16px; }
  main { padding: 20px 14px 120px; }
  .reading-area { line-height: 1.9; }
  .toolbar { padding: 8px 12px; gap: 6px; }
  .brand { font-size: 16px; }
  .btn { padding: 6px 10px; font-size: 13px; min-height: 40px; }
}

/* ============== Settings Dialog ============== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 2000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  cursor: pointer;
}
.overlay.visible {
  display: flex;
  animation: fadein 150ms ease-out;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
.dialog {
  background: var(--surface);
  border-radius: 12px;
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  animation: slideup 200ms ease-out;
  cursor: default;
}
@keyframes slideup { from { transform: translateY(20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.dialog-header h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}
.dialog-body {
  padding: 18px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.dialog-footer {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}
.field-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
}
.input-group {
  display: flex;
  gap: 4px;
  align-items: center;
}
.input-group input {
  flex: 1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", Consolas, monospace;
  min-width: 0;
}
.input-group input:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.link {
  display: inline-block;
  margin-top: 8px;
  font-size: 13px;
  color: var(--accent);
  text-decoration: none;
}
.link:hover { text-decoration: underline; }
.status {
  margin-top: 14px;
  font-size: 13px;
  color: var(--text-muted);
}
.status.saved { color: var(--success); }
.test-result {
  margin-top: 8px;
  font-size: 13px;
  min-height: 20px;
  word-break: break-word;
}
.test-result.success { color: var(--success); }
.test-result.error { color: var(--danger); }
.test-result.loading { color: var(--text-muted); }
.dialog .privacy-note {
  margin-top: 16px;
  padding: 10px 12px;
  background: var(--word-hover);
  border-radius: 6px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

@media (max-width: 480px) {
  .dialog { max-width: 100%; }
  .dialog-footer { flex-wrap: wrap; }
  .dialog-footer .btn { flex: 1; min-width: 70px; min-height: 44px; }
}
