* { box-sizing: border-box; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  margin: 0;
  color: #1a1a1a;
  background: #fafafa;
}

header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #e0e0e0;
  background: #fff;
}

header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
}

header h1 { margin: 0; font-size: 1.25rem; }
header h1 a { color: inherit; text-decoration: none; }

header nav a {
  color: #777;
  text-decoration: none;
  margin-left: 1rem;
  font-size: 0.9rem;
}

header nav a:hover { color: #1a1a1a; }

main {
  max-width: 40rem;
  margin: 0 auto;
  padding: 1rem;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

#search {
  flex: 1;
  /* flex items won't shrink below the input's intrinsic default width,
     which overflows narrow phone viewports */
  min-width: 0;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}

.count { color: #777; font-size: 0.875rem; white-space: nowrap; }

.word-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 0.5rem;
}

.word-list li { border-bottom: 1px solid #eee; }
.word-list li:last-child { border-bottom: none; }

.word-list a {
  display: block;
  padding: 0.6rem 0.9rem;
  color: inherit;
  text-decoration: none;
}

.word-list a:hover { background: #f4f4f4; }

.word-detail .back { margin-top: 0; }
.word-detail .back a { color: #777; text-decoration: none; }
.word-detail h2 { margin: 0.25rem 0 1rem; font-size: 1.75rem; }

.definition {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 0.5rem;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
}

.definition h3 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-style: italic;
  font-weight: 600;
}

.definition .pron {
  font-style: normal;
  font-weight: 400;
  color: #777;
  margin-left: 0.5rem;
}

.play-audio, .speak-word {
  border: none;
  background: none;
  font-size: 1rem;
  cursor: pointer;
  padding: 0 0.25rem;
  vertical-align: baseline;
}

.definition ol { margin: 0; padding-left: 1.5rem; }
.definition li { margin-bottom: 0.35rem; }

.definition .examples {
  list-style: none;
  margin: 0.5rem 0 0;
  padding-left: 1.5rem;
  border-left: 2px solid #e0e0e0;
  font-style: italic;
  font-size: 0.9rem;
  color: #555;
}

.definition .examples li { margin-bottom: 0.25rem; }

.quiz-stats { margin: -0.5rem 0 1rem; color: #777; font-size: 0.875rem; }

/* quiz page */
.quiz-top { display: flex; justify-content: space-between; gap: 1rem; }

#quiz-banner {
  padding: 0.75rem 1rem;
  border: 1px solid #d4a017;
  border-radius: 0.5rem;
  background: #fdf6e3;
}

#quiz-card {
  margin-top: 1rem;
  padding: 1.25rem;
  border: 1px solid #e0e0e0;
  border-radius: 0.75rem;
}

#quiz-card h2 { margin: 0.25rem 0 1rem; font-size: 1.75rem; }

#quiz-senses { margin: 0.25rem 0 1rem; padding-left: 1.5rem; }
#quiz-senses li { margin-bottom: 0.35rem; }

#quiz-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
/* display rules defeat the UA [hidden] rule - be explicit */
#quiz-choices[hidden] { display: none; }

.quiz-choice {
  padding: 0.6rem 1rem;
  font: inherit;
  font-size: 0.95rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  -webkit-appearance: none;
  appearance: none;
  color: #1a1a1a;
  cursor: pointer;
}

.quiz-choice:hover:not(:disabled) { background: #f4f4f4; }
.quiz-choice:disabled { cursor: default; }
.quiz-choice.right { border-color: #2e7d32; color: #2e7d32; background: #f0f7f0; }
.quiz-choice.wrong { border-color: #b00020; color: #b00020; background: #fdf3f4; }

#quiz-form { display: flex; gap: 0.5rem; align-items: flex-end; }
#quiz-form[hidden] { display: none; }

#quiz-answer {
  flex: 1;
  padding: 0.5rem 0.75rem;
  font: inherit;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  resize: vertical;
}

#quiz-submit {
  padding: 0.5rem 1rem;
  font: inherit;
  font-size: 0.9rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  /* iOS renders unstyled-looking buttons with UA appearance and system
     colors, which blanked the label on phones - opt out entirely and
     state the color instead of inheriting it. */
  -webkit-appearance: none;
  appearance: none;
  color: #1a1a1a;
  white-space: nowrap;
  flex-shrink: 0;
  cursor: pointer;
}

#quiz-submit:hover { background: #f4f4f4; }
#quiz-submit:disabled { color: #999; cursor: default; }

#quiz-feedback { margin-top: 1rem; }
#quiz-verdict { margin: 0 0 0.25rem; font-weight: 600; }
#quiz-verdict.right { color: #2e7d32; }
#quiz-verdict.wrong { color: #b00020; }
#quiz-note { margin: 0 0 0.5rem; }
#quiz-error { color: #b00020; }

.etymology-block { margin: 1rem 0; }

#etymology-toggle {
  padding: 0.4rem 0.8rem;
  font-size: 0.875rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  cursor: pointer;
}

#etymology-toggle:hover { background: #f4f4f4; }
#etymology-toggle:disabled { color: #999; cursor: default; }

.etymology {
  margin: 0.5rem 0 0;
  padding-left: 1.5rem;
  border-left: 2px solid #e0e0e0;
  font-size: 0.9rem;
  color: #555;
}

.etymology[hidden] { display: none; }

.meta, .empty { color: #777; font-size: 0.875rem; }

#import-form { display: flex; flex-direction: column; gap: 0.75rem; }

#import-text {
  width: 100%;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  font-family: inherit;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}

.file-label { color: #777; font-size: 0.875rem; }

#import-submit {
  align-self: flex-start;
  padding: 0.5rem 1.25rem;
  font-size: 1rem;
  border: none;
  border-radius: 0.5rem;
  background: #1a1a1a;
  color: #fff;
  cursor: pointer;
}

#import-submit:disabled { opacity: 0.5; cursor: default; }

.report { font-weight: 600; }

#random {
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  cursor: pointer;
}

#random:hover { background: #f4f4f4; }

.star { color: #d4a017; }

.title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.title-row h2 { margin: 0; }

.actions { display: flex; gap: 0.5rem; }

.actions button {
  padding: 0.4rem 0.8rem;
  font-size: 0.875rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  cursor: pointer;
}

.actions button:hover { background: #f4f4f4; }

#star-toggle[data-starred="1"] { border-color: #d4a017; color: #8a6a0f; }

.actions .danger { color: #b00020; }
.actions .danger:hover { background: #fbeaec; }

#login-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-width: 20rem;
  margin: 3rem auto 0;
}

#login-form input {
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}

#login-form button {
  padding: 0.5rem 1.25rem;
  font-size: 1rem;
  border: none;
  border-radius: 0.5rem;
  background: #1a1a1a;
  color: #fff;
  cursor: pointer;
}

.error { color: #b00020; font-weight: 600; }

/* --- word-tutor chatbot --- */
#chat-fab {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: 3.25rem;
  height: 3.25rem;
  border: none;
  border-radius: 50%;
  background: #1a1a1a;
  color: #fff;
  font-size: 1.4rem;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
  z-index: 50;
}

#chat-panel {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  width: min(24rem, calc(100vw - 2rem));
  height: min(32rem, calc(100vh - 2rem));
  display: flex;
  flex-direction: column;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 0.75rem;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
  z-index: 50;
  overflow: hidden;
}

#chat-panel header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 0.9rem;
  background: #1a1a1a;
  color: #fff;
  font-size: 0.9rem;
  margin: 0;
}

#chat-close {
  border: none;
  background: none;
  color: #fff;
  font-size: 1.2rem;
  cursor: pointer;
  line-height: 1;
}

#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.chat-msg {
  max-width: 85%;
  padding: 0.5rem 0.75rem;
  border-radius: 0.75rem;
  font-size: 0.9rem;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.chat-msg.user {
  align-self: flex-end;
  background: #1a1a1a;
  color: #fff;
}

.chat-msg.assistant, .chat-msg.hint, .chat-msg.typing {
  align-self: flex-start;
  background: #f0f0f0;
  color: #1a1a1a;
}

.chat-msg.hint { color: #777; }
.chat-msg.error { align-self: flex-start; background: #fde8ec; color: #b00020; }

#chat-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.6rem;
  border-top: 1px solid #eee;
}

#chat-input {
  flex: 1;
  resize: none;
  padding: 0.45rem 0.6rem;
  font-size: 0.9rem;
  font-family: inherit;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
}

#chat-send {
  border: none;
  border-radius: 0.5rem;
  background: #1a1a1a;
  color: #fff;
  padding: 0 0.9rem;
  cursor: pointer;
}

#chat-send:disabled { opacity: 0.5; cursor: default; }

#chat-resize {
  position: absolute;
  top: 0;
  left: 0;
  width: 1.1rem;
  height: 1.1rem;
  cursor: nwse-resize;
  z-index: 1;
  touch-action: none;
  /* two diagonal ticks, mirroring the native bottom-right grip */
  background:
    linear-gradient(135deg, #999 0 1.5px, transparent 1.5px 4px,
      #999 4px 5.5px, transparent 5.5px) no-repeat 3px 3px / 9px 9px;
}

/* markdown inside assistant bubbles */
.chat-msg.assistant { white-space: normal; }
.chat-msg p, .chat-msg ul, .chat-msg ol, .chat-msg pre { margin: 0 0 0.5rem; }
.chat-msg > :last-child { margin-bottom: 0; }
.chat-msg ul, .chat-msg ol { padding-left: 1.25rem; }
.chat-msg li { margin: 0.15rem 0; }
.chat-msg code {
  background: #e6e6e6;
  padding: 0.05rem 0.3rem;
  border-radius: 0.25rem;
  font-size: 0.85em;
}
.chat-msg pre {
  background: #e6e6e6;
  padding: 0.5rem;
  border-radius: 0.5rem;
  overflow-x: auto;
}
.chat-msg pre code { background: none; padding: 0; }
.chat-msg a { color: inherit; }

/* The tutor panel needs room; on phones it swallows the whole viewport.
   The quiz bot covers mobile - hide the web tutor there. */
@media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) {
  #chat-fab, #chat-panel { display: none; }
}

/* The author display:flex above overrides the UA's [hidden] rule, which
   left the panel visible on load and made the close button a no-op. */
#chat-panel[hidden] { display: none; }

#chat-clear {
  border: none;
  background: none;
  color: #fff;
  font-size: 1.05rem;
  cursor: pointer;
  line-height: 1;
  margin-right: 0.4rem;
}

/* logout confirmation dialog */
#logout-confirm {
  border: none;
  border-radius: 0.75rem;
  padding: 1.25rem 1.5rem;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
  max-width: 20rem;
}
#logout-confirm::backdrop { background: rgba(0, 0, 0, 0.35); }
#logout-confirm p { margin: 0 0 1rem; font-size: 1rem; }
.dialog-actions { display: flex; gap: 0.5rem; justify-content: flex-end; }
.dialog-actions button {
  padding: 0.4rem 1rem;
  font-size: 0.9rem;
  border: 1px solid #ccc;
  border-radius: 0.5rem;
  background: #fff;
  cursor: pointer;
}
#logout-go {
  background: #1a1a1a;
  color: #fff;
  border-color: #1a1a1a;
}

/* back-to-top button; sits above the chat fab where that exists */
#scroll-top {
  position: fixed;
  right: 1rem;
  bottom: 5.25rem;
  width: 2.5rem;
  height: 2.5rem;
  border: 1px solid #ddd;
  border-radius: 50%;
  background: #fff;
  color: #1a1a1a;
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
  z-index: 49;
}
/* author display rules defeat the UA [hidden] rule - be explicit (see
   the chat-panel close-button bug) */
#scroll-top[hidden] { display: none; }
@media (max-width: 640px), (pointer: coarse) and (max-width: 1024px) {
  /* chat fab is hidden here, so take the corner */
  #scroll-top { bottom: 1rem; }
}

/* word card dialog */
#word-dialog {
  border: none;
  border-radius: 0.75rem;
  padding: 0;
  width: min(38rem, calc(100vw - 2rem));
  max-height: calc(100vh - 4rem);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.18);
}
#word-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
#word-dialog-body {
  padding: 1.25rem 1.5rem;
  overflow-y: auto;
  max-height: calc(100vh - 4rem);
}
#word-dialog .word-detail { margin: 0; }
