/* =========================================================
   Jetons de design
   ========================================================= */
:root {
  --font: "Manrope", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --bg: #eef1f7;
  --surface: #ffffff;
  --surface-2: #f6f7fb;
  --text: #171c2c;
  --muted: #6c7390;
  --border: #e1e5ef;
  --hover: #eceff7;

  --accent: #4356e6;
  --accent-2: #7a4ff0;
  --accent-soft: #e9ebfd;
  --on-accent: #ffffff;

  --e: #4356e6;
  --y: #0ea28a;
  --danger: #e5484d;
  --ok: #0ea28a;

  --theirs: #ffffff;
  --theirs-text: #171c2c;
  --shadow: 0 1px 2px rgba(23,28,44,.06), 0 8px 24px rgba(23,28,44,.08);
  --shadow-lg: 0 20px 50px rgba(23,28,44,.18);
  --glass: rgba(255,255,255,.78);

  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0b0f1a;
  --surface: #131929;
  --surface-2: #1a2134;
  --text: #e8ebf5;
  --muted: #8b93ad;
  --border: #242c42;
  --hover: #1f2740;

  --accent: #6b7cff;
  --accent-2: #9d7bff;
  --accent-soft: #1d2345;
  --on-accent: #ffffff;

  --e: #6b7cff;
  --y: #2fcfae;
  --danger: #ff6369;
  --ok: #2fcfae;

  --theirs: #1a2134;
  --theirs-text: #e8ebf5;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  --shadow-lg: 0 20px 50px rgba(0,0,0,.5);
  --glass: rgba(19,25,41,.78);

  color-scheme: dark;
}

/* =========================================================
   Base
   ========================================================= */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, input, select { font-family: inherit; }
button { color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.screen { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.hidden { display: none !important; }

.link { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 13px; font-weight: 600; }
.link:hover { text-decoration: underline; }

.icon-btn, .theme-btn, .photo-btn {
  width: 38px; height: 38px;
  border: none; border-radius: 50%;
  background: none; color: var(--muted);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.icon-btn:hover, .theme-btn:hover, .photo-btn:hover { background: var(--hover); color: var(--text); }
.theme-btn { font-size: 17px; line-height: 1; }
.theme-btn::before { content: "🌙"; }
:root[data-theme="dark"] .theme-btn::before { content: "☀️"; }

.avatar {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border-radius: 50%;
  color: #fff; font-weight: 800; font-size: 16px;
  background: var(--muted);
  flex-shrink: 0;
}
.avatar.e { background: linear-gradient(135deg, var(--e), var(--accent-2)); }
.avatar.y { background: linear-gradient(135deg, var(--y), #4fd1a3); }

/* =========================================================
   Accueil : convertisseur
   ========================================================= */
#login { align-items: center; padding: 16px; overflow-y: auto; }
.tool {
  margin: auto;
  background: var(--surface);
  padding: 28px;
  border-radius: 20px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 520px;
  display: flex; flex-direction: column; gap: 18px;
}
.tool-head { display: flex; align-items: center; justify-content: space-between; }
.tool-head h1 { margin: 0; font-size: 22px; font-weight: 800; letter-spacing: -.01em; }
.tabs {
  display: flex; gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
  margin: 0 -28px; padding: 0 28px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex-shrink: 0;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: none; color: var(--muted);
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.tab.active { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.convert { display: flex; align-items: center; gap: 10px; }
.field { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.field input, .field select, .access select, .access input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2); color: var(--text);
  font-size: 16px;
  outline: none;
}
.field input:focus, .field select:focus, .access input:focus, .access select:focus { border-color: var(--accent); }
.field input[readonly] { font-weight: 700; }
.swap {
  width: 40px; height: 40px; flex-shrink: 0;
  border: 1px solid var(--border); border-radius: 50%;
  background: none; color: var(--text);
  font-size: 18px; cursor: pointer;
}
.swap:hover { background: var(--hover); }
.formula { margin: 0; font-size: 13px; color: var(--muted); text-align: center; min-height: 1.2em; }
.tool-foot {
  display: flex; justify-content: space-between; align-items: center;
  font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--border);
  padding-top: 12px;
}
.version { color: var(--muted); font-size: 12px; cursor: default; user-select: none; }
.access { display: flex; gap: 8px; flex-wrap: wrap; }
.access select { flex: 1 1 120px; width: auto; }
.access input { flex: 2 1 140px; width: auto; }
#login-btn {
  padding: 12px 18px;
  border: none; border-radius: 12px;
  background: var(--accent); color: var(--on-accent);
  font-size: 15px; font-weight: 700; cursor: pointer;
}
#login-btn:disabled { opacity: .5; }
.error { color: var(--danger) !important; font-size: 14px; flex-basis: 100%; margin: 0; }

/* =========================================================
   Discussion
   ========================================================= */
#chat {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 100%;
  height: 100dvh;
  max-width: 760px; margin: 0 auto;
  background: var(--bg);
}

/* En-tête translucide au-dessus du fil */
header {
  position: relative; z-index: 3;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: var(--glass);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}
.who { display: flex; align-items: center; gap: 12px; min-width: 0; }
.who > div { min-width: 0; }
.title { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title strong { font-weight: 800; }
.status { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.on { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.dot.off { background: var(--danger); }
.actions { display: flex; align-items: center; gap: 2px; }

/* Changement de mot de passe */
.settings {
  position: relative; z-index: 2;
  display: flex; flex-direction: column; gap: 8px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.settings strong { font-weight: 800; }
.settings input {
  padding: 11px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2); color: var(--text);
  font-size: 16px; outline: none;
}
.settings input:focus { border-color: var(--accent); }
.settings-actions { display: flex; justify-content: flex-end; gap: 8px; align-items: center; }
#pw-save {
  padding: 10px 16px;
  border: none; border-radius: 10px;
  background: var(--accent); color: var(--on-accent);
  font-size: 14px; font-weight: 700; cursor: pointer;
}
#pw-save:disabled { opacity: .5; }
.pw-message { margin: 0; font-size: 13px; color: var(--danger); }
.pw-message.ok { color: var(--ok); }

/* Fil de messages */
#messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 6px;
}
#messages > :first-child { margin-top: auto; }
.empty { text-align: center; color: var(--muted); margin: auto; font-size: 14px; }

.day {
  align-self: center;
  margin: 10px 0 4px;
  padding: 4px 12px;
  font-size: 12px; font-weight: 600; color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.msg {
  position: relative;
  max-width: 76%;
  padding: 9px 14px 7px;
  border-radius: 20px;
  background: var(--theirs);
  color: var(--theirs-text);
  align-self: flex-start;
  box-shadow: 0 1px 1px rgba(23,28,44,.05);
  word-wrap: break-word;
  white-space: pre-wrap;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: pan-y;
  transition: opacity .25s, transform .25s, box-shadow .15s;
}
.msg.mine {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--on-accent);
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 30%, transparent);
}
/* Bulles consécutives du même auteur : regroupées, coins intérieurs resserrés */
.msg:not(.mine) + .msg:not(.mine) { border-top-left-radius: 8px; margin-top: -2px; }
.msg:not(.mine):has(+ .msg:not(.mine)) { border-bottom-left-radius: 8px; }
.msg.mine + .msg.mine { border-top-right-radius: 8px; margin-top: -2px; }
.msg.mine:has(+ .msg.mine) { border-bottom-right-radius: 8px; }

.msg.selected { box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 45%, transparent); }
.msg.gone { opacity: 0; transform: scale(.92); }

.msg.photo { padding: 4px; }
.msg.photo img {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 16px;
  cursor: zoom-in;
}
.msg.photo .meta { padding: 4px 10px 4px; }

.meta {
  display: flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 600; opacity: .65; margin-top: 3px;
  justify-content: flex-end;
}
.msg.mine .meta { color: var(--on-accent); }
.ticks { letter-spacing: -2px; }
.ticks.read { opacity: 1; }
.msg.mine .ticks.read { color: #c7f9ec; }

/* Menu de suppression (appui long) */
.msg-menu { position: absolute; top: -42px; right: 0; z-index: 2; }
.msg:not(.mine) .msg-menu { right: auto; left: 0; }
.msg-menu button {
  padding: 8px 14px;
  border: none; border-radius: 999px;
  background: var(--danger); color: #fff;
  font-size: 13px; font-weight: 700; cursor: pointer;
  box-shadow: var(--shadow);
}
.msg-menu button:disabled { opacity: .6; }

/* Saisie : capsule flottante */
form#form {
  position: relative; z-index: 3;
  display: flex; gap: 6px; align-items: center;
  margin: 6px 12px;
  margin-bottom: calc(12px + env(safe-area-inset-bottom));
  padding: 6px 6px 6px 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.editor {
  flex: 1;
  min-width: 0;
  max-height: 120px;
  overflow-y: auto;
  padding: 10px 6px;
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
  -webkit-user-select: text; user-select: text;
}
.editor:empty::before { content: attr(data-placeholder); color: var(--muted); pointer-events: none; }
.editor img { max-height: 60px; border-radius: 8px; vertical-align: middle; }
form#form { border-radius: 26px; }
#send {
  width: 42px; height: 42px; flex-shrink: 0;
  border: none; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: var(--on-accent);
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: transform .12s, opacity .15s;
}
#send:hover { transform: scale(1.06); }
#send:active { transform: scale(.96); }
#send:disabled { opacity: .5; transform: none; cursor: default; }

.photo-wrap { position: relative; flex-shrink: 0; }
.photo-btn { width: 42px; height: 42px; color: var(--accent); }
.photo-menu {
  position: absolute;
  bottom: 54px; left: 0;
  display: flex; flex-direction: column;
  min-width: 230px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  z-index: 5;
}
.photo-menu label { padding: 14px 16px; font-size: 15px; font-weight: 600; cursor: pointer; }
.photo-menu label + label { border-top: 1px solid var(--border); }
.photo-menu label:hover { background: var(--hover); }

/* Aperçu photo */
.lightbox {
  position: fixed; inset: 0;
  background: rgba(5,8,16,.9);
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out; z-index: 10;
}
.lightbox img { max-width: 95vw; max-height: 95vh; border-radius: 12px; }

/* =========================================================
   Mobile
   ========================================================= */
@media (max-width: 600px) {
  #login { padding: 0; align-items: stretch; }
  .tool {
    margin: 0;
    min-height: 100%;
    max-width: none;
    padding: 20px 16px;
    padding-bottom: calc(20px + env(safe-area-inset-bottom));
    border-radius: 0;
    box-shadow: none;
    gap: 16px;
  }
  .tool-head h1 { font-size: 20px; }
  .tabs { margin: 0 -16px; padding: 0 16px; }
  .convert { flex-direction: column; align-items: stretch; }
  .field { width: 100%; }
  .swap { align-self: center; transform: rotate(90deg); }
  .access { flex-direction: column; }
  .access select, .access input { flex: none; width: 100%; }
  #login-btn { width: 100%; }
  .tool-foot { margin-top: auto; }

  header { padding: 8px 10px; }
  .title { font-size: 14px; }
  #logout { font-size: 12px; }
  #messages { padding: 12px 10px 6px; }
  .msg { max-width: 84%; }
  form#form { margin: 4px 8px; margin-bottom: calc(8px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
