* { box-sizing: border-box; margin: 0; padding: 0; }

  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #f5f5f0;
    color: #222;
    min-height: 100vh;
  }

  .app {
    max-width: 1300px;
    margin: 0 auto;
    padding: 1rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
  }

  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0 1rem;
    border-bottom: 1px solid #ddd;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  h1.app-title {
    font-size: 1.5rem;
    font-weight: 700;
    color: #333;
    font-family: 'Linux Libertine', Georgia, serif;
  }

  .controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
  }

  button, select {
    padding: 0.4rem 0.8rem;
    font-size: 0.9rem;
    border: 1px solid #ccc;
    background: white;
    cursor: pointer;
    border-radius: 4px;
    font-family: inherit;
  }

  button:hover { background: #f0f0f0; }

  .lang-toggle {
    display: inline-flex;
    border: 1px solid #ccc;
    border-radius: 4px;
    overflow: hidden;
  }
  .lang-toggle button {
    border: none;
    border-radius: 0;
    padding: 0.4rem 0.7rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: #555;
  }
  .lang-toggle button + button { border-left: 1px solid #ccc; }
  .lang-toggle button.active {
    background: #2c5aa0;
    color: white;
  }

  /* Same control pattern as .lang-toggle for the difficulty preference */
  .diff-toggle {
    display: inline-flex;
    border: 1px solid #ccc;
    border-radius: 4px;
    overflow: hidden;
  }
  .diff-toggle button {
    border: none;
    border-radius: 0;
    padding: 0.4rem 0.55rem;
    font-size: 0.85rem;
    font-weight: 600;
    color: #777;
    letter-spacing: 1px;
  }
  .diff-toggle button + button { border-left: 1px solid #ccc; }
  .diff-toggle button.active {
    background: #6d4c00;
    color: #fff8e1;
  }
  .diff-toggle button[data-pref="any"].active    { background: #555;    color: white; }
  .diff-toggle button[data-pref="easy"].active   { background: #1b5e20; color: white; }
  .diff-toggle button[data-pref="medium"].active { background: #6d4c00; color: white; }
  .diff-toggle button[data-pref="hard"].active   { background: #8b1c1c; color: white; }

  button.primary {
    background: #2c5aa0;
    color: white;
    border-color: #2c5aa0;
  }
  button.primary:hover { background: #234780; }

  button.icon-btn {
    padding: 0.4rem 0.55rem;
    font-size: 1rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  button.icon-btn svg {
    width: 1.1rem;
    height: 1.1rem;
    display: block;
  }

  /* ---- Modal ----------------------------------------------------------- */
  .modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .modal.hidden { display: none; }
  .modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
  }
  .modal-content {
    position: relative;
    background: white;
    padding: 1.5rem 2rem;
    border-radius: 8px;
    max-width: 460px;
    width: calc(100% - 2rem);
    max-height: calc(100vh - 4rem);
    overflow-y: auto;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  }
  .modal-close {
    position: absolute;
    top: 0.4rem;
    right: 0.6rem;
    width: 2rem;
    height: 2rem;
    padding: 0;
    background: none;
    border: none;
    font-size: 1.4rem;
    color: #888;
    cursor: pointer;
    line-height: 1;
  }
  .modal-close:hover { color: #222; background: none; }
  .modal-content h2 {
    font-size: 1.3rem;
    margin-bottom: 1rem;
    padding-right: 2rem;
    font-family: 'Linux Libertine', Georgia, serif;
    font-weight: 600;
  }
  .modal-content h3 {
    font-size: 1rem;
    margin: 1rem 0 0.4rem;
    color: #2c5aa0;
    font-weight: 700;
  }
  .modal-content p { margin: 0.5rem 0; line-height: 1.5; }
  .modal-content ul {
    margin: 0.3rem 0 0.8rem 1.1rem;
    padding: 0;
    line-height: 1.5;
  }
  .modal-content li { margin: 0.25rem 0; }
  .modal-content a { color: #2c5aa0; }

  .setting {
    margin: 1rem 0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
  .setting > label {
    font-weight: 600;
    color: #444;
    font-size: 0.95rem;
  }
  .setting-checkbox {
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
  }
  .setting-checkbox input { width: 1.05rem; height: 1.05rem; cursor: pointer; }

  main {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 1.5rem;
    flex: 1;
  }

  /* The proper responsive overrides are at the bottom of this file. */

  .sidebar {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    position: sticky;
    top: 1rem;
    align-self: start;
    max-height: calc(100vh - 2rem);
  }

  #guess-form input {
    width: 100%;
    padding: 0.6rem;
    font-size: 1rem;
    border: 2px solid #ccc;
    border-radius: 4px;
    font-family: inherit;
  }
  #guess-form input:focus {
    outline: none;
    border-color: #2c5aa0;
  }

  .stats {
    font-size: 0.95rem;
    color: #444;
    padding: 0.5rem 0;
    border-bottom: 1px solid #eee;
  }

  /* Difficulty chip rendered inside .stats */
  .diff {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.15rem 0.55rem;
    margin-right: 0.5rem;
    border-radius: 999px;
    font-size: 0.82em;
    font-weight: 600;
    background: #eef;
    color: #345;
    line-height: 1.5;
    vertical-align: middle;
    cursor: help;
  }
  .diff-stars { letter-spacing: 1px; font-size: 0.9em; }
  .diff-0 { background: #e0e0e0; color: #555; }
  .diff-1 { background: #e8f5e9; color: #1b5e20; }
  .diff-2 { background: #f1f8e9; color: #33691e; }
  .diff-3 { background: #fff8e1; color: #6d4c00; }
  .diff-4 { background: #ffe0b2; color: #b25400; }
  .diff-5 { background: #ffcdd2; color: #8b1c1c; }

  #history {
    list-style: none;
    overflow-y: auto;
    font-size: 0.9rem;
    flex: 1;
    min-height: 100px;
  }

  #history li {
    padding: 0.35rem 0.6rem;
    border-bottom: 1px solid #eee;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
  }
  #history li:hover { background: #eee; }
  #history li.hit { color: #1b5e20; background: #e8f5e9; }
  #history li.hit:hover { background: #d4ead7; }
  #history li.miss { color: #888; }
  #history li.hint-entry { background: #fff8e1; color: #6d4c00; }
  #history li.hint-entry.hit { background: #fff8e1; }
  #history li.hint-entry:hover { background: #fff3c4; }
  #history li.duplicate { animation: shake 0.4s; }

  @keyframes shake {
    0%,100% { transform: translateX(0); }
    25% { transform: translateX(-3px); }
    75% { transform: translateX(3px); }
  }

  #history .count {
    font-weight: bold;
    font-variant-numeric: tabular-nums;
    background: rgba(0,0,0,0.07);
    padding: 0.1rem 0.4rem;
    border-radius: 10px;
    font-size: 0.85em;
  }
  #history .sim {
    font-variant-numeric: tabular-nums;
    font-weight: 600;
    font-size: 0.85em;
    min-width: 2.5em;
    text-align: right;
  }
  #history .sim.pending {
    color: #aaa;
    font-weight: normal;
  }
  #history li.hit .count { background: #1b5e20; color: white; }
  #history .idx {
    color: #999;
    font-size: 0.8em;
    min-width: 1.6em;
    text-align: right;
  }

  #article {
    background: white;
    padding: 1.5rem 2rem;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    line-height: 1.75;
    font-size: 1.05rem;
    overflow-wrap: break-word;
  }

  .article-title {
    font-size: 2rem;
    font-weight: 400;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid #a2a9b1;
    font-family: 'Linux Libertine', Georgia, serif;
    line-height: 1.3;
  }
  .article-title .title-link {
    color: inherit;
    text-decoration: none;
  }
  .article-title .title-link:hover {
    color: #36c;
  }
  .article-title .title-link:hover .word {
    text-decoration: underline;
  }
  .article-h2 {
    font-size: 1.4rem;
    font-weight: 400;
    margin: 1.5rem 0 0.5rem;
    padding-bottom: 0.2rem;
    border-bottom: 1px solid #a2a9b1;
    font-family: 'Linux Libertine', Georgia, serif;
  }
  .article-h3 {
    font-size: 1.15rem;
    font-weight: 600;
    margin: 1rem 0 0.4rem;
    font-family: 'Linux Libertine', Georgia, serif;
  }
  .article-para {
    margin-bottom: 0.7rem;
  }

  .word {
    display: inline-block;
    border-radius: 2px;
    white-space: nowrap;
  }
  .word.hidden {
    background: #333;
    color: transparent;
    user-select: none;
    margin: 0 1px;
    line-height: 1.2;
    vertical-align: baseline;
    cursor: pointer;
    display: inline-grid;
    grid-template-columns: auto;
    grid-template-rows: auto;
    align-items: center;
    justify-items: center;
  }
  .word.hidden > * {
    grid-column: 1;
    grid-row: 1;
  }
  .word.hidden .placeholder {
    white-space: pre;
    visibility: hidden;
    pointer-events: none;
  }
  .word.hidden .overlay-guess,
  .word.hidden .overlay-count {
    pointer-events: none;
    font-weight: 600;
    white-space: nowrap;
    letter-spacing: -0.02em;
    line-height: 1;
  }
  .word.hidden .overlay-guess {
    font-size: 0.82em;
    padding: 0 0.2em;
    text-shadow: 0 1px 0 rgba(0,0,0,0.6);
  }
  .word.hidden .overlay-count {
    color: white;
    font-size: 0.85em;
    opacity: 0;
  }
  .word.hidden.show-count .overlay-count {
    animation: countFade 2.2s forwards;
  }
  .word.hidden.show-count .overlay-guess { opacity: 0; }
  @keyframes countFade {
    0%   { opacity: 0; }
    15%  { opacity: 1; }
    70%  { opacity: 1; }
    100% { opacity: 0; }
  }
  .word.revealed { color: inherit; }
  .word.just-revealed { background: #fff59d; transition: background 1.5s; }
  .word.highlight { background: #ff9800 !important; color: white !important; }
  .word.title-word.hidden { background: #222; }

  .victory, .gameover {
    background: #d4edda;
    color: #155724;
    padding: 0.8rem 1rem;
    border-radius: 4px;
    margin-bottom: 1rem;
    font-weight: 500;
  }
  .gameover { background: #fff3cd; color: #856404; }

  .loading {
    text-align: center;
    padding: 3rem;
    color: #888;
    font-style: italic;
  }
  .error {
    background: #f8d7da;
    color: #721c24;
    padding: 1rem;
    border-radius: 4px;
  }

  .toast {
    position: fixed;
    top: 1rem;
    left: 50%;
    transform: translateX(-50%);
    background: #333;
    color: white;
    padding: 0.6rem 1.2rem;
    border-radius: 4px;
    z-index: 100;
    animation: fade 2s forwards;
  }
  @keyframes fade {
    0% { opacity: 0; }
    20% { opacity: 1; }
    80% { opacity: 1; }
    100% { opacity: 0; }
  }

/* =========================================================================
 *  Responsive overrides
 *  - <=900px : tablette & mobile (single column, sticky input)
 *  - <=480px : très petits écrans
 * ========================================================================= */

@media (max-width: 900px) {
  .app { padding: 0.5rem; }

  /* Header more compact */
  header {
    padding: 0.25rem 0 0.5rem;
    margin-bottom: 0.5rem;
    gap: 0.4rem;
  }
  h1.app-title { font-size: 1.15rem; }

  .controls { gap: 0.35rem; }
  button,
  .lang-toggle button {
    padding: 0.45rem 0.7rem;
    font-size: 0.85rem;
    min-height: 36px;
  }

  /* Single column with re-ordered children. We make .sidebar transparent
   * to layout (display: contents) so its children (form, stats, history)
   * become direct children of <main> and can be reordered via flex order. */
  main {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  .sidebar {
    display: contents;
    position: static;
    max-height: none;
  }
  #guess-form {
    order: 1;
    position: sticky;
    top: 0;
    z-index: 10;
    background: #f5f5f0;
    padding: 0.4rem 0;
    margin: 0 -0.5rem;        /* extend to viewport edges to feel like a bar */
    padding-left: 0.5rem;
    padding-right: 0.5rem;
    box-shadow: 0 2px 6px -2px rgba(0,0,0,0.15);
  }
  #guess-form input {
    font-size: 16px;          /* prevents iOS zoom on focus */
    padding: 0.7rem;
  }
  .stats { order: 2; padding: 0.3rem 0; }
  #article {
    order: 3;
    padding: 1rem 1rem;
    font-size: 1rem;
    line-height: 1.65;
  }
  #history {
    order: 4;
    max-height: 50vh;
    border-top: 1px solid #ddd;
    padding-top: 0.4rem;
  }
  #history li { padding: 0.5rem 0.6rem; }   /* bigger tap targets */

  /* Article text scaled down a notch on small screens */
  .article-title { font-size: 1.5rem; line-height: 1.25; }
  .article-h2    { font-size: 1.2rem; margin-top: 1rem; }
  .article-h3    { font-size: 1.05rem; }
  .article-para  { margin-bottom: 0.6rem; }

  /* Bigger tappable masked words */
  .word.hidden { margin: 0.05rem 1px; padding: 0 0.05em; }
}

@media (max-width: 480px) {
  .app { padding: 0.35rem; }
  h1.app-title { font-size: 1rem; }
  .controls { gap: 0.25rem; }
  button,
  .lang-toggle button {
    padding: 0.4rem 0.55rem;
    font-size: 0.78rem;
  }
  #article { padding: 0.75rem; }
  .article-title { font-size: 1.3rem; }
}

/* Touch devices: remove hover-only feedback that gets sticky after a tap */
@media (hover: none) {
  button:hover,
  #history li:hover { background: inherit; }
  button.primary:hover { background: #2c5aa0; }
  #history li.hit:hover { background: #e8f5e9; }
}

.tmdb-attribution {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 1.2rem;
  padding-top: 1rem;
  border-top: 1px solid #eee;
  font-size: 0.72rem;
  color: #888;
}
.tmdb-attribution img {
  height: 14px;
  width: auto;
  flex-shrink: 0;
}

.article-title .source-icon {
  display: inline-block;
  margin-right: 0.5rem;
  font-size: 0.75em;
  vertical-align: 0.15em;
  opacity: 0.55;
  cursor: help;
}
