/* スマホ前提（モバイルファースト）。画面が広いときは、カードを複数列に並べる */

:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #1f2933;
  --muted: #667383;
  --line: #e1e5ea;
  --accent: #1b6ec2;
  --accent-text: #ffffff;
  --warn-bg: #fff1cc;
  --warn-text: #8a5a00;
  --warn-edge: #f0b429;
  --danger-bg: #fde3e3;
  --danger-text: #a12626;
  --danger-edge: #e05252;
  --radius: 12px;
  --tap: 48px;                       /* 指で押しやすい最小の高さ */
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161b;
    --surface: #1c2229;
    --text: #e6eaee;
    --muted: #96a3b0;
    --line: #2d3640;
    --accent: #4a9be8;
    --accent-text: #0b1620;
    --warn-bg: #3a2f10;
    --warn-text: #f1c767;
    --warn-edge: #b98a1f;
    --danger-bg: #3c1c1c;
    --danger-text: #f59a9a;
    --danger-edge: #b34848;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Yu Gothic UI", "Hiragino Sans", "Meiryo", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

/* ---- ヘッダー ---- */
.app-header {
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 8px;
}
h1 { margin: 0; font-size: 20px; line-height: 1.3; }
.badge {
  font-size: 12px; font-weight: normal; padding: 2px 8px; margin-left: 6px;
  border-radius: 999px; background: var(--warn-bg); color: var(--warn-text); vertical-align: middle;
}
.sub { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.operator { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; }
.userbox { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--muted); }
.userbox span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

main { padding: 12px 12px calc(88px + var(--safe-b)); max-width: 1100px; margin: 0 auto; }

/* ---- 入力部品（16px以上：iPhoneで拡大されない） ---- */
input, select, textarea, button {
  font: inherit; color: inherit; font-size: 16px;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface);
}
input, select, textarea { width: 100%; }
textarea { min-height: 88px; resize: vertical; }
button { cursor: pointer; font-weight: 600; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.ghost { background: var(--surface); }
button:active { filter: brightness(.94); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }
.operator input { min-height: 44px; }

/* ---- サマリー（タップで絞り込み） ---- */
.summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.tile {
  min-height: 0; padding: 8px 4px; text-align: center; font-weight: normal;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.tile .label { display: block; color: var(--muted); font-size: 11px; line-height: 1.3; }
.tile .value { display: block; font-size: 22px; font-weight: 700; line-height: 1.3; }
.tile.warn .value { color: var(--warn-text); }
.tile.danger .value { color: var(--danger-text); }
.tile.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
div.tile { cursor: default; }

/* ---- 検索・絞り込み ---- */
.controls { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
details > summary {
  list-style: none; cursor: pointer; min-height: 40px; display: flex; align-items: center;
  color: var(--accent); font-size: 14px; font-weight: 600;
}
details > summary::-webkit-details-marker { display: none; }
details > summary::before { content: "▸"; margin-right: 6px; display: inline-block; transition: transform .15s; }
details[open] > summary::before { transform: rotate(90deg); }
.filter-grid { display: grid; gap: 10px; padding-bottom: 6px; }
.filter-grid label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; }
.list-head { display: flex; justify-content: space-between; align-items: center; position: relative; }
.count { color: var(--muted); font-size: 13px; }
.more { position: relative; }
.more-menu {
  position: absolute; right: 0; top: 100%; z-index: 5; min-width: 180px;
  display: flex; flex-direction: column; gap: 6px; padding: 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}

/* ---- 在庫カード ---- */
.list { display: grid; grid-template-columns: 1fr; gap: 10px; }
.item {
  background: var(--surface); border: 1px solid var(--line); border-left: 6px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
}
.item.soon { border-left-color: var(--warn-edge); }
.item.expired { border-left-color: var(--danger-edge); }
.item-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.item-name { font-size: 17px; font-weight: 700; line-height: 1.35; }
.item-qty { text-align: right; white-space: nowrap; }
.item-qty b { font-size: 26px; line-height: 1.1; }
.item-qty small { color: var(--muted); margin-left: 2px; font-size: 12px; }
.item-exp { margin-top: 4px; font-size: 15px; }
.item-dest { margin-top: 2px; font-size: 15px; }
.item-dest span:not(.sep) { color: var(--muted); font-size: 12px; margin-right: 6px; }
.item-dest .sep { color: var(--muted); margin: 0 8px; }
.item-memo {
  margin: 8px 0 0; padding: 8px 10px; background: var(--bg); border-radius: 8px;
  color: var(--muted); font-size: 14px; white-space: pre-wrap; word-break: break-word;
}
.item-foot { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.item-meta { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; margin-left: 8px; font-weight: 600; }
.tag.warn { background: var(--warn-bg); color: var(--warn-text); }
.tag.danger { background: var(--danger-bg); color: var(--danger-text); }
.empty { padding: 32px 16px; text-align: center; color: var(--muted); }

/* ---- 新規登録ボタン（親指が届く右下に固定） ---- */
.fab {
  position: fixed; right: 16px; bottom: calc(16px + var(--safe-b)); z-index: 10;
  min-height: 56px; padding: 0 22px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text); border-color: var(--accent);
  font-size: 17px; box-shadow: 0 6px 18px rgba(0, 0, 0, .3);
}
.fab span { font-size: 22px; margin-right: 2px; vertical-align: -1px; }

/* ---- 登録・編集（スマホでは、画面下からせり上がるシート） ---- */
dialog {
  border: 0; padding: 0; color: var(--text); background: var(--surface);
  width: 100%; max-width: 100%;
  max-height: 94dvh; margin: auto 0 0;
  border-radius: 18px 18px 0 0;
}
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog form { display: flex; flex-direction: column; max-height: 94dvh; }
dialog h2 { margin: 0; padding: 16px 16px 8px; font-size: 18px; }
.fields { padding: 4px 16px 8px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
dialog label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
dialog label > input, dialog label > select, dialog label > textarea { display: block; margin-top: 4px; color: var(--text); }
.row { display: grid; grid-template-columns: 1fr; gap: 0; }
.meta { color: var(--muted); font-size: 12px; margin: 0 0 8px; }
.err { background: var(--danger-bg); color: var(--danger-text); padding: 10px 12px; border-radius: 10px; margin: 0 0 8px; font-size: 14px; }
.err-text { color: var(--danger-text); min-height: 1.5em; font-size: 14px; }
.actions {
  display: grid; grid-template-columns: 1fr 2fr; gap: 10px;
  padding: 12px 16px calc(12px + var(--safe-b));
  border-top: 1px solid var(--line); background: var(--surface);
}

/* ---- ログイン ---- */
.login { display: flex; justify-content: center; padding: 32px 16px; }
.login-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  padding: 24px 20px; width: min(420px, 100%); text-align: center;
}
.login-card h2 { margin: 0 0 8px; font-size: 18px; }
.login-card p { color: var(--muted); font-size: 14px; }
#gsi-btn { display: flex; justify-content: center; margin: 16px 0 8px; min-height: 44px; }

/* ---- 少し広い画面（タブレット・PC）：カードを複数列に ---- */
@media (min-width: 640px) {
  .app-header { flex-direction: row; justify-content: space-between; align-items: flex-end; }
  .operator input { width: 200px; }
  .filter-grid { grid-template-columns: repeat(3, 1fr); }
  .list { grid-template-columns: repeat(2, 1fr); }
  .item-foot { flex-direction: row; align-items: center; justify-content: space-between; }
  .item-actions { grid-template-columns: auto auto; }
  .item-actions button { min-height: 40px; padding: 4px 16px; }
  dialog { max-width: 560px; margin: auto; border-radius: 16px; max-height: 90dvh; }
  dialog form { max-height: 90dvh; }
  .row { grid-template-columns: 1fr 1fr; gap: 12px; }
}
@media (min-width: 1000px) {
  .list { grid-template-columns: repeat(3, 1fr); }
}
