:root {
  --bg: #f2f3f5;
  --panel: #ffffff;
  --text: #111111;
  --sub: #8a8f99;
  --line: #e3e5e8;
  --accent: #06c755;
  --talk-bg: #8cabd9;
  --mine: #86e174;
  --mine-text: #111111;
  --theirs: #ffffff;
  --theirs-text: #111111;
  --pill: rgba(0, 0, 0, 0.18);
  --pill-text: #ffffff;
  --meta-text: rgba(0, 0, 0, 0.55);
  --card: #ffffff;
  --hover: #f5f6f8;
  --selected: #e9f8ef;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111214;
    --panel: #1b1c1f;
    --text: #eceef1;
    --sub: #8d929c;
    --line: #2b2d31;
    --talk-bg: #22262e;
    --mine: #3b9b3a;
    --mine-text: #ffffff;
    --theirs: #34373d;
    --theirs-text: #eceef1;
    --pill: rgba(255, 255, 255, 0.12);
    --pill-text: #d6d9de;
    --meta-text: rgba(255, 255, 255, 0.5);
    --card: #2a2c31;
    --hover: #222428;
    --selected: #1f3326;
  }
}
:root[data-theme="dark"] {
  --bg: #111214;
  --panel: #1b1c1f;
  --text: #eceef1;
  --sub: #8d929c;
  --line: #2b2d31;
  --talk-bg: #22262e;
  --mine: #3b9b3a;
  --mine-text: #ffffff;
  --theirs: #34373d;
  --theirs-text: #eceef1;
  --pill: rgba(255, 255, 255, 0.12);
  --pill-text: #d6d9de;
  --meta-text: rgba(255, 255, 255, 0.5);
  --card: #2a2c31;
  --hover: #222428;
  --selected: #1f3326;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }

/* ---------- 開始画面 ---------- */
.start { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.start-card {
  width: 100%; max-width: 520px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 16px; padding: 28px 24px;
}
.start-card h1 { margin: 0 0 8px; font-size: 22px; }
.lead { margin: 0 0 20px; color: var(--sub); line-height: 1.6; }
.lead code { font-size: 13px; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 36px 16px; border: 2px dashed var(--line); border-radius: 12px;
  cursor: pointer; text-align: center; transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.over { border-color: var(--accent); background: var(--selected); outline: none; }
.dz-main { font-weight: 600; font-size: 16px; }
.dz-sub { color: var(--sub); font-size: 13px; }
.start-actions { display: flex; justify-content: center; margin-top: 12px; }
.btn-secondary {
  cursor: pointer; font-size: 13px; color: var(--accent); padding: 6px 10px; border-radius: 8px;
}
.btn-secondary:hover { background: var(--hover); }
.status { min-height: 1.4em; margin: 14px 0 0; font-size: 14px; text-align: center; }
.status.error { color: #e5484d; }
.note { margin: 12px 0 0; font-size: 12px; color: var(--sub); line-height: 1.6; }

/* ---------- アプリ本体 ---------- */
.app { display: grid; grid-template-columns: 320px minmax(0, 1fr); height: 100%; }
.sidebar { min-width: 0; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.sidebar-head h1 { margin: 0; font-size: 20px; }
.icon-btn { background: none; border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; font-size: 12px; cursor: pointer; }
.icon-btn:hover { background: var(--hover); }
.setting { display: flex; gap: 6px; align-items: center; padding: 4px 16px 10px; font-size: 12px; color: var(--sub); cursor: pointer; }
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; gap: 12px; align-items: center; padding: 10px 16px; cursor: pointer; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--selected); }
.chat-body { flex: 1; min-width: 0; }
.chat-row { display: flex; justify-content: space-between; gap: 8px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-date { color: var(--sub); font-size: 11px; flex-shrink: 0; }
.chat-preview { color: var(--sub); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 17px;
}
.avatar.small { width: 36px; height: 36px; font-size: 14px; }

.talk { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--talk-bg); }
.talk-head {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px;
  background: var(--talk-bg); border-bottom: 1px solid rgba(0,0,0,.06);
}
.talk-head h2 { margin: 0; font-size: 17px; color: var(--pill-text); text-shadow: 0 1px 1px rgba(0,0,0,.08); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back { display: none; background: none; border: none; font-size: 28px; line-height: 1; color: var(--pill-text); cursor: pointer; padding: 0 6px 0 0; }
/* 広い画面では相手と自分の吹き出しが離れすぎないよう、表示幅を 720px までにして中央に寄せる */
.messages { flex: 1; overflow-y: auto; padding: 12px max(16px, calc((100% - 720px) / 2)) 24px; }

.date-sep { display: flex; justify-content: center; margin: 14px 0 10px; }
.date-sep span, .system span {
  background: var(--pill); color: var(--pill-text); font-size: 12px; border-radius: 999px; padding: 3px 12px;
}
.system { display: flex; justify-content: center; margin: 8px 0; text-align: center; }
.system span { border-radius: 12px; max-width: 80%; line-height: 1.5; padding: 6px 12px; }

.msg { display: flex; gap: 8px; margin-top: 4px; align-items: flex-start; }
.msg.first { margin-top: 12px; }
.msg.mine { flex-direction: row-reverse; }
.msg .avatar-slot { width: 36px; flex-shrink: 0; }
.msg-main { display: flex; flex-direction: column; max-width: min(72%, 520px); min-width: 0; }
.msg.mine .msg-main { align-items: flex-end; }
.sender { font-size: 12px; color: var(--pill-text); margin: 0 0 3px 2px; }
.line { display: flex; align-items: flex-end; gap: 6px; }
.msg.mine .line { flex-direction: row-reverse; }
.meta { display: flex; flex-direction: column; font-size: 10px; color: var(--meta-text); line-height: 1.3; flex-shrink: 0; }
.msg.mine .meta { align-items: flex-end; }
.talk .meta { color: var(--pill-text); opacity: .85; }

.bubble {
  position: relative; padding: 8px 12px; border-radius: 18px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
  background: var(--theirs); color: var(--theirs-text);
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); }
.msg.first .bubble::before {
  content: ""; position: absolute; top: 6px; left: -6px; width: 12px; height: 12px;
  background: inherit; clip-path: polygon(100% 0, 0 0, 100% 100%);
}
.msg.mine.first .bubble::before { left: auto; right: -6px; clip-path: polygon(0 0, 100% 0, 0 100%); }
.bubble a { color: inherit; text-decoration: underline; }

.sticker img { width: 130px; height: 130px; object-fit: contain; display: block; }
.sticker-fallback { color: var(--pill-text); font-size: 13px; padding: 8px 0; }

.photo {
  width: 170px; max-width: 100%; max-height: 300px; border-radius: 14px; overflow: hidden;
  background: repeating-linear-gradient(45deg, rgba(0,0,0,.06) 0 10px, rgba(0,0,0,.02) 10px 20px), var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  color: var(--sub); font-size: 12px; text-align: center; padding: 8px;
}
.photo .icon { font-size: 28px; }

.card { background: var(--card); color: var(--theirs-text); border-radius: 14px; overflow: hidden; width: 240px; max-width: 100%; }
.card-body { padding: 10px 12px; display: flex; gap: 10px; align-items: center; }
.card-title { font-weight: 600; font-size: 14px; }
.card-sub { font-size: 12px; color: var(--sub); line-height: 1.5; margin-top: 2px; }
.card-foot { border-top: 1px solid var(--line); padding: 8px 12px; font-size: 13px; text-align: center; }
.card-foot a { color: var(--accent); text-decoration: none; }
.map-thumb { height: 90px; display: grid; place-items: center; font-size: 34px; background: linear-gradient(135deg, #cfe8d4, #bcd6f2); }

.empty { color: var(--pill-text); text-align: center; margin-top: 40px; }

@media (max-width: 720px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .app.show-talk .sidebar { display: none; }
  .app:not(.show-talk) .talk { display: none; }
  .back { display: block; }
  .msg-main { max-width: 78%; }
  .messages { padding: 10px 12px 20px; }
}

/* ---------- プロフィール ---------- */
.clickable { cursor: pointer; }
.clickable:hover { filter: brightness(1.05); }
.talk-head h2.clickable:hover, .sender.clickable:hover { text-decoration: underline; }
.clickable:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.profile {
  width: min(440px, calc(100% - 32px)); max-height: calc(100% - 32px);
  border: none; border-radius: 16px; padding: 0;
  background: var(--panel); color: var(--text);
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
.profile::backdrop { background: rgba(0, 0, 0, .45); }
.profile-head { display: flex; align-items: center; gap: 12px; padding: 18px 18px 12px; }
.profile-head h2 { margin: 0; font-size: 18px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.profile-close { background: none; border: none; font-size: 24px; line-height: 1; color: var(--sub); cursor: pointer; padding: 4px 6px; }
.profile-fields { margin: 0; padding: 0 18px 6px; display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 10px 14px; font-size: 14px; }
.profile-fields dt { color: var(--sub); }
.profile-fields dd { margin: 0; overflow-wrap: anywhere; line-height: 1.5; }
.profile-fields .src { color: var(--sub); font-size: 11px; margin-left: 4px; }
.mid-row { display: flex; gap: 8px; align-items: flex-start; }
.mid-row code { font-size: 13px; overflow-wrap: anywhere; user-select: all; }
.copy-btn { flex-shrink: 0; background: none; border: 1px solid var(--line); border-radius: 6px; font-size: 11px; padding: 2px 8px; cursor: pointer; }
.copy-btn:hover { background: var(--hover); }
.profile-note { margin: 8px 18px 18px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 12px; color: var(--sub); line-height: 1.6; }
.info-btn {
  flex-shrink: 0; width: 30px; height: 30px; border-radius: 50%;
  background: none; border: none; color: var(--sub); font-size: 18px; line-height: 1; cursor: pointer;
}
.info-btn:hover, .info-btn:focus-visible { background: var(--line); color: var(--text); outline: none; }

.saved-actions { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px 16px; margin-top: 4px; font-size: 13px; }
.saved-actions:empty { display: none; }
#saved-info { display: inline-flex; align-items: center; gap: 6px; color: var(--sub); }
.btn-secondary { background: none; border: none; }
.btn-secondary.danger { color: #e5484d; }

/* LINE のサーバーから取れたアイコン画像は頭文字の上に重ねる(取れなければ頭文字が見える) */
.avatar { position: relative; overflow: hidden; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.setting + .setting { margin-top: -6px; }

.load-more { display: flex; justify-content: center; margin: 4px 0 8px; }
.load-more button {
  background: var(--pill); color: var(--pill-text); border: none; border-radius: 999px;
  font-size: 12px; padding: 5px 14px; cursor: pointer;
}
.load-more button:hover { filter: brightness(1.1); }
