:root {
  --bg: #f3f5fb;
  --card: #ffffff;
  --text: #1b2033;
  --muted: #667089;
  --line: #dde2ee;
  --primary: #2f55d4;
  --primary-press: #2446b5;
  --secondary: #e8edfc;
  --secondary-text: #2446b5;
  --err-spelling: #e0364f;
  --err-grammar: #2f7de0;
  --err-style: #d99a00;
  --ok: #1f9d63;
  --shadow: 0 4px 24px rgba(27, 32, 51, .08);
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11141d;
    --card: #1b1f2c;
    --text: #eef1f8;
    --muted: #9aa3ba;
    --line: #2d3345;
    --primary: #5b7cf0;
    --primary-press: #4a69d8;
    --secondary: #262d44;
    --secondary-text: #b5c4ff;
    --err-spelling: #ff6b80;
    --err-grammar: #6aa8ff;
    --err-style: #f0bd3c;
    --shadow: 0 4px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.5 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  -webkit-user-select: none;
  user-select: none;
}
textarea, .check-view { -webkit-user-select: text; user-select: text; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px; flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; }
.brand img { width: 34px; height: 34px; border-radius: 9px; }
.user { display: flex; align-items: center; gap: 10px; font-size: 15px; color: var(--muted); }
.user img { width: 30px; height: 30px; border-radius: 50%; background: var(--line); }
.link { background: none; border: 0; color: var(--primary); font: inherit; padding: 8px; cursor: pointer; }

main { flex: 1 1 auto; display: flex; min-height: 0; }
.screen { flex: 1; display: flex; flex-direction: column; padding: 8px 20px 20px; min-height: 0; gap: 14px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 36px 28px; }
.center { margin: auto; max-width: 560px; width: 100%; text-align: center; }
h1 { margin: 6px 0 8px; font-size: 30px; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.error { color: var(--err-spelling); font-weight: 600; }
.hero-icon { width: 96px; height: 96px; border-radius: 24px; box-shadow: var(--shadow); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  border: 0; border-radius: 14px; font: inherit; font-weight: 600;
  padding: 12px 18px; min-height: 48px; cursor: pointer; text-decoration: none;
  transition: transform .08s, background .15s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:active { background: var(--primary-press); }
.btn.secondary { background: var(--secondary); color: var(--secondary-text); }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.big { font-size: 19px; padding: 14px 26px; min-height: 58px; }
.btn.huge { font-size: 21px; min-height: 84px; padding: 18px 26px; border-radius: 18px; }
.btn:disabled { opacity: .5; }
.emoji { font-size: 28px; }

#screen-login .btn.big { margin: 22px 0 12px; }
.capture-buttons { display: grid; gap: 14px; margin: 26px 0; }
.lang-row { display: flex; align-items: center; justify-content: center; gap: 12px; color: var(--muted); }
.lang-select {
  font: inherit; color: var(--text); background: var(--secondary);
  border: 0; border-radius: 12px; padding: 10px 14px; min-height: 46px;
}
.lang-select.compact { font-size: 15px; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.step-title { color: var(--muted); font-size: 15px; text-align: center; flex: 1; }
.bottom-actions { display: flex; align-items: center; justify-content: flex-end; gap: 12px; flex: 0 0 auto; }
.bottom-actions .muted { margin-right: auto; }

/* Recadrage */
.crop-stage {
  flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center;
  background: #0d0f16; border-radius: var(--radius); overflow: hidden; padding: 20px;
}
.crop-wrap { position: relative; touch-action: none; line-height: 0; }
.crop-wrap canvas { max-width: 100%; max-height: 100%; display: block; }
.crop-box {
  position: absolute; border: 2px solid #fff;
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, .55);
  touch-action: none; cursor: move;
}
.handle {
  position: absolute; width: 44px; height: 44px; margin: -22px; touch-action: none;
}
.handle::after {
  content: ""; position: absolute; inset: 11px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 6px rgba(0, 0, 0, .5);
}
.handle.tl { left: 0; top: 0; cursor: nwse-resize; }
.handle.tr { right: 0; top: 0; cursor: nesw-resize; }
.handle.bl { left: 0; bottom: 0; cursor: nesw-resize; }
.handle.br { right: 0; bottom: 0; cursor: nwse-resize; }

/* Éditeur */
.segmented { display: inline-flex; background: var(--secondary); border-radius: 14px; padding: 4px; }
.segmented button {
  border: 0; background: transparent; font: inherit; font-size: 15px; font-weight: 600;
  color: var(--secondary-text); padding: 10px 16px; border-radius: 11px; min-height: 44px; cursor: pointer;
}
.segmented button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0, 0, 0, .12); }
.editor-card {
  flex: 1; min-height: 0; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
#text, .check-view {
  flex: 1; width: 100%; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: 20px/1.7 Georgia, "Times New Roman", serif; padding: 22px 26px; overflow-y: auto;
  white-space: pre-wrap; word-wrap: break-word;
}
.check-status {
  flex: 0 0 auto; padding: 10px 26px; border-top: 1px solid var(--line); font-size: 15px; color: var(--muted);
  display: flex; gap: 18px; flex-wrap: wrap; align-items: center;
}
.legend { display: inline-flex; align-items: center; gap: 6px; }
.legend::before { content: ""; width: 14px; height: 3px; border-radius: 2px; background: currentColor; }
.legend.spelling { color: var(--err-spelling); }
.legend.grammar { color: var(--err-grammar); }
.legend.style { color: var(--err-style); }
.check-status .ok { color: var(--ok); font-weight: 600; }

.mark {
  text-decoration: underline wavy; text-decoration-thickness: 2px; text-underline-offset: 5px;
  cursor: pointer; border-radius: 4px;
}
.mark.spelling { text-decoration-color: var(--err-spelling); }
.mark.grammar { text-decoration-color: var(--err-grammar); }
.mark.style { text-decoration-color: var(--err-style); }
.mark.active { background: var(--secondary); }

/* Fiche erreur */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 20; }
.sheet {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  width: min(560px, calc(100% - 32px)); background: var(--card); border-radius: 22px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .3); padding: 22px; z-index: 21;
  display: flex; flex-direction: column; gap: 12px;
}
.sheet-word { font: 700 24px/1.3 Georgia, serif; }
.sheet-msg { margin: 0; color: var(--muted); }
.suggestions { display: flex; flex-wrap: wrap; gap: 10px; }
.suggestions button {
  font: 600 18px Georgia, serif; border: 2px solid var(--primary); color: var(--primary);
  background: transparent; border-radius: 12px; padding: 10px 16px; min-height: 48px; cursor: pointer;
}
.sheet-actions { display: flex; justify-content: space-between; }

/* Chargement & toast */
.loading {
  position: fixed; inset: 0; background: rgba(243, 245, 251, .85); z-index: 30;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  font-weight: 600;
}
@media (prefers-color-scheme: dark) { .loading { background: rgba(17, 20, 29, .85); } }
.spinner {
  width: 54px; height: 54px; border-radius: 50%;
  border: 5px solid var(--secondary); border-top-color: var(--primary); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; top: calc(20px + env(safe-area-inset-top)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 12px 22px; border-radius: 14px; font-weight: 600;
  z-index: 40; box-shadow: var(--shadow); max-width: calc(100% - 32px); text-align: center;
}

@media (max-width: 700px) {
  .screen { padding: 8px 16px 16px; }
  .step-title { order: 3; flex-basis: 100%; }
  .user span { display: none; }
  .toolbar .segmented { order: 3; width: 100%; }
  .segmented button { flex: 1; }
}
