/* Reset & base */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, "Segoe UI", "Hiragino Sans", "Meiryo", sans-serif; font-size: 14px; color: #222; background: #f5f7fa; }
a { color: #2a6cb0; text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0 0 12px 0; }
h2 { font-size: 16px; margin: 0 0 8px 0; }
h3 { font-size: 14px; margin: 0 0 6px 0; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 100; height: 48px; background: #2c3e50; color: #fff; display: flex; align-items: center; padding: 0 12px; gap: 12px; }
.topbar .brand { color: #fff; font-weight: bold; font-size: 19px; letter-spacing: .3px; }
.topbar .spacer { flex: 1; }
.topbar .user-menu { display: flex; gap: 12px; align-items: center; font-size: 13px; }
.topbar .user-menu a { color: #cde4ff; }
.topbar .user-label { color: #bbb; }
.burger { display: none; background: none; border: none; color: #fff; font-size: 24px; cursor: pointer; }

/* Layout */
.layout { display: flex; min-height: calc(100vh - 48px); }
.sidebar { width: 180px; background: #34495e; padding: 8px 0; flex-shrink: 0; display: flex; flex-direction: column; }
.sidebar a { display: block; padding: 10px 16px; color: #dfe6ed; font-size: 13px; border-left: 3px solid transparent; }
.sidebar a:hover { background: #2c3e50; color: #fff; border-left-color: #4a90e2; text-decoration: none; }
.sidebar-footer { margin-top: auto; padding: 12px 16px 8px; border-top: 1px solid #2c3e50; }
.sidebar-footer a { display: inline; padding: 0; font-size: 11px; color: #8899aa; border: none; }
.sidebar-footer a:hover { background: none; color: #ccc; border: none; }
.content { flex: 1; padding: 16px; max-width: 1400px; min-width: 0; /* 子のwide tableで親が伸びないように */ }

/* Mobile */
@media (max-width: 768px) {
  .burger { display: block; }
  .sidebar {
    position: fixed; top: 48px; left: 0;
    height: calc(100vh - 48px);
    width: 220px;
    transform: translateX(-100%);
    transition: transform 0.2s;
    z-index: 110;
    box-shadow: 2px 0 12px rgba(0,0,0,0.2);
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }
  /* サイドバー展開時の背景オーバーレイ */
  .sidebar-backdrop {
    display: none;
    position: fixed; top: 48px; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.4); z-index: 100;
  }
  .sidebar.open + .sidebar-backdrop,
  body.sb-open .sidebar-backdrop { display: block; }
  .topbar .user-label { display: none; }
  .content { padding: 10px; }
  h1 { font-size: 18px; }
}

/* Flash messages */
.flash { padding: 10px 14px; border-radius: 4px; margin-bottom: 10px; }
.flash-success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.flash-error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
.flash-info { background: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }

/* Auth page */
.auth-main { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: #2c3e50; padding: 20px; }
.auth-card { background: #fff; padding: 28px; border-radius: 8px; min-width: 320px; max-width: 400px; box-shadow: 0 4px 20px rgba(0,0,0,0.2); }
.auth-card h1 { text-align: center; margin-bottom: 18px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form label { display: flex; flex-direction: column; gap: 4px; }
.auth-form input { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; }

/* Page header */
.page-header { display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; }
.page-header .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; flex-wrap: wrap; gap: 8px; }
.section-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; flex-wrap: wrap; gap: 8px; }
.section-head h2 { margin: 0; }

/* Buttons */
.btn { display: inline-block; padding: 7px 14px; border: 1px solid #bbb; background: #fff; color: #333; border-radius: 4px; cursor: pointer; font-size: 13px; text-decoration: none; line-height: 1.3; }
.btn:hover { background: #f0f0f0; text-decoration: none; }
.btn-primary { background: #2c7be5; color: #fff; border-color: #2c7be5; }
.btn-primary:hover { background: #1a62c7; color: #fff; }
.btn-danger { background: #e0534f; color: #fff; border-color: #e0534f; }
.btn-danger:hover { background: #c63431; color: #fff; }
.btn-small { padding: 4px 8px; font-size: 12px; }

/* Toolbar */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; padding: 10px; background: #fff; border-radius: 4px; border: 1px solid #e1e5eb; }
.toolbar input[type=text], .toolbar select, .toolbar input[type=date], .toolbar input[type=number] { padding: 6px 10px; border: 1px solid #ccc; border-radius: 3px; }
.toolbar .inline { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; }
/* JS 有効時は冗長な「検索」ボタンを隠す（自動送信のため） */
.js-on .toolbar > button[type=submit].btn:not(.btn-primary):not(.btn-danger) { display: none; }

/* Cards */
.card { background: #fff; padding: 16px; border-radius: 6px; border: 1px solid #e1e5eb; margin-bottom: 12px; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.card.link { transition: box-shadow 0.2s; }
.card.link:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.08); text-decoration: none; color: inherit; }

/* KPI grid */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; margin-bottom: 12px; }
.kpi { background: #fff; padding: 12px; border-radius: 6px; border: 1px solid #e1e5eb; }
.kpi-label { font-size: 11px; color: #666; margin-bottom: 4px; }
.kpi-value { font-size: 18px; font-weight: bold; color: #2c3e50; }

/* Tables */
.table-wrap { overflow-x: auto; background: #fff; border: 1px solid #e1e5eb; border-radius: 4px; -webkit-overflow-scrolling: touch; }
table.data {
  /* セル内で折り返さず、必要に応じて横スクロール（width:100% は使わない）*/
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th, table.data td {
  padding: 7px 10px;
  border-bottom: 1px solid #eaecef;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;     /* デフォルトは改行禁止 */
}
table.data thead th { background: #f7f9fc; font-weight: 600; color: #555; position: sticky; top: 0; }
table.data tfoot th { background: #f7f9fc; font-weight: bold; }
table.data tr:hover { background: #f9fbfd; }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; }
table.data .small { font-size: 11px; color: #666; }
table.data .neg { color: #c0392b; }
table.data .total { background: #f0f5fb; font-weight: bold; }
table.data.pivot th:first-child, table.data.pivot td:first-child { position: sticky; left: 0; background: inherit; }
/* 折り返しを許可するセル（住所・メモなど）*/
table.data td.wrap, table.data td.address, table.data .pre {
  white-space: normal;
  word-break: break-word;
  max-width: 280px;
}

/* ソート可能ヘッダ */
table.data thead th.sortable {
  cursor: pointer;
  user-select: none;
  position: relative;
  padding-right: 18px;
}
table.data thead th.sortable:hover { background: #eaeff5; }
table.data thead th.sortable::after {
  content: '⇅';
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  color: #aaa;
}
table.data thead th.sort-asc::after { content: '▲'; color: #2c7be5; font-size: 9px; }
table.data thead th.sort-desc::after { content: '▼'; color: #2c7be5; font-size: 9px; }

/* KV */
dl.kv { display: grid; grid-template-columns: 180px 1fr; gap: 6px 12px; margin: 0; font-size: 13px; }
dl.kv dt { color: #666; font-weight: normal; }
dl.kv dd { margin: 0; }
dl.kv dd.pre { white-space: pre-wrap; }

@media (max-width: 640px) {
  dl.kv { grid-template-columns: 110px 1fr; font-size: 12px; }
}

/* Forms */
.form-card { background: #fff; padding: 16px; border-radius: 6px; border: 1px solid #e1e5eb; }
.form-card fieldset { border: 1px solid #e1e5eb; padding: 10px; margin: 0 0 12px 0; border-radius: 4px; }
.form-card legend { padding: 0 6px; font-size: 13px; font-weight: bold; color: #2c3e50; }
.form-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; margin-bottom: 10px; }
.form-card label > span { color: #555; font-size: 12px; }
.form-card input, .form-card select, .form-card textarea { padding: 7px 10px; border: 1px solid #ccc; border-radius: 3px; font-size: 13px; font-family: inherit; }
.form-card label.inline { flex-direction: row; align-items: center; gap: 6px; }
.form-card label.inline input[type=checkbox] { width: auto; margin: 0; }
.grid-form fieldset { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px 14px; }
.grid-form label.full { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 8px; margin-top: 12px; }

.inline-form { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; align-items: center; }
.inline-form input, .inline-form select { padding: 5px 8px; border: 1px solid #ccc; border-radius: 3px; font-size: 12px; }

/* Tags */
.tag { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; background: #eee; color: #555; margin-right: 3px; }
.tag.active, .tag.success { background: #d4edda; color: #155724; }
.tag.vacant, .tag.warning { background: #fff3cd; color: #856404; }
.tag.sold, .tag.failed, .tag.expired { background: #f8d7da; color: #721c24; }
.tag.partial { background: #d1ecf1; color: #0c5460; }
.tag.admin { background: #2c3e50; color: #fff; }
.tag.owner { background: #2c7be5; color: #fff; }
.tag.viewer { background: #95a5a6; color: #fff; }
.tag.ok { background: #d4edda; color: #155724; }
.tag.neg { background: #f8d7da; color: #721c24; }
.tag.muted { background: #eee; color: #888; }

/* 部屋行の入居/空室ハイライト */
table.data tr.row-occ { background: #f3faf4; }
table.data tr.row-vac { background: #fff5f5; }
table.data tr.row-occ:hover { background: #e8f5ea; }
table.data tr.row-vac:hover { background: #fdecec; }

/* Tabs */
.tabs { display: flex; gap: 2px; background: #fff; border-radius: 4px; padding: 4px; margin-bottom: 10px; overflow-x: auto; white-space: nowrap; }
.tabs a { padding: 6px 14px; font-size: 13px; color: #555; border-radius: 3px; }
.tabs a:hover { background: #f0f5fb; text-decoration: none; }

.muted { color: #888; }
.neg { color: #c0392b; }
.pre { white-space: pre-wrap; }

/* Dropzone */
.dropzone { border: 2px dashed #bbb; padding: 24px; text-align: center; border-radius: 6px; cursor: pointer; background: #fafbfc; color: #666; transition: all 0.15s; }
.dropzone:hover, .dropzone.drag { border-color: #2c7be5; background: #f0f7ff; color: #2c7be5; }

.upload-row { padding: 8px 12px; margin: 6px 0; border-radius: 4px; background: #f5f7fa; font-size: 13px; }
.upload-row.ok { background: #d4edda; color: #155724; }
.upload-row.err { background: #f8d7da; color: #721c24; }

pre.log { background: #f5f7fa; padding: 10px; border: 1px solid #e1e5eb; border-radius: 4px; overflow: auto; max-height: 400px; font-size: 11px; }

/* Modal */
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 200; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-content { background: #fff; padding: 20px; border-radius: 6px; max-width: 900px; width: 100%; max-height: 85vh; overflow: auto; position: relative; }
.modal-content .close { position: absolute; top: 8px; right: 14px; font-size: 24px; cursor: pointer; color: #999; }
.modal-content .close:hover { color: #333; }

/* ============================================================
   スマホ最適化（640px以下）
   タッチターゲット拡大／グリッド縮約／表は横スクロール
   ============================================================ */
@media (max-width: 640px) {
  body { font-size: 14px; }
  h1 { font-size: 17px; }
  h2 { font-size: 15px; }

  /* タッチ用に上下のヒット領域を確保 */
  .btn { padding: 9px 14px; font-size: 14px; min-height: 38px; }
  .btn-small { padding: 6px 10px; font-size: 12px; min-height: 30px; }

  /* リンク間隔（誤タップ防止） */
  .sidebar a { padding: 12px 16px; font-size: 14px; }

  /* ページヘッダ: 縦積み・全幅ボタン */
  .page-header { flex-direction: column; align-items: stretch; }
  .page-header .actions { width: 100%; }
  .page-header .actions .btn { flex: 1 1 auto; text-align: center; }

  /* カードグリッド: 1列 */
  .card-grid { grid-template-columns: 1fr; }
  .card { padding: 12px; }

  /* KPI: 2列（小さな画面でちょうど見やすい） */
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .kpi { padding: 10px 8px; }
  .kpi-value { font-size: 16px; word-break: break-all; }

  /* 入力ツールバー: 1行ずつ・全幅 */
  .toolbar { flex-direction: column; align-items: stretch; padding: 8px; }
  .toolbar > label, .toolbar > input, .toolbar > select, .toolbar > .btn { width: 100%; }
  .toolbar input[type=text], .toolbar input[type=date],
  .toolbar input[type=number], .toolbar input[type=month],
  .toolbar select { width: 100%; padding: 9px 10px; font-size: 14px; }
  .toolbar .inline { width: auto; }
  .toolbar > label { display: flex; flex-direction: column; gap: 4px; }

  /* フォーム: 1列＋大きめ入力 */
  .form-card { padding: 12px; }
  .grid-form fieldset { grid-template-columns: 1fr; gap: 8px 0; }
  .form-card input, .form-card select, .form-card textarea { padding: 9px 10px; font-size: 14px; min-height: 38px; }
  .form-actions { flex-direction: column; }
  .form-actions .btn { width: 100%; text-align: center; }

  /* 説明用 dl: ブロック表示 */
  dl.kv { display: block; }
  dl.kv dt { color: #888; font-size: 11px; margin-top: 8px; }
  dl.kv dd { margin: 2px 0 0 0; }

  /* テーブル: スマホでは文字を少し小さく */
  table.data { font-size: 12px; }
  table.data th, table.data td { padding: 6px 10px; }
  table.data td.wrap, table.data td.address, table.data .pre { max-width: 200px; }
  /* dl.kv の中の住所など長文 */
  dl.kv dd { word-break: break-word; }

  /* タブ: 横スクロールのまま、タップ対応で大きめ */
  .tabs { padding: 4px 6px; gap: 4px; }
  .tabs a { padding: 8px 14px; font-size: 13px; }

  /* インラインフォーム（金利追加・返済追加など） */
  .inline-form { gap: 6px; }
  .inline-form input, .inline-form select { padding: 8px 10px; font-size: 13px; min-height: 36px; flex: 1 1 calc(50% - 8px); }
  .inline-form .btn { width: 100%; }

  /* モーダル全画面化 */
  .modal { padding: 0; }
  .modal-content { max-height: 100vh; height: 100vh; border-radius: 0; padding: 14px; }

  /* ダッシュボードのチャート高さ */
  #chart { max-height: 280px; }

  /* プロフィールシート: 列幅を縮め、ラベルを折り返す */
  table.matrix col.label-col { width: 110px !important; }
  table.matrix col.total-col { width: 80px !important; }
  table.matrix col.prop-col  { width: 100px !important; }
  table.matrix { font-size: 10.5px; }
  table.matrix th, table.matrix td { padding: 4px 5px; }

  /* ドロップゾーン */
  .dropzone { padding: 16px 12px; font-size: 13px; }

  /* ユーザーメニューの整理 */
  .topbar { padding: 0 8px; gap: 8px; }
  .topbar .brand { font-size: 15px; }
  .topbar .user-menu { gap: 8px; font-size: 12px; }
  .topbar .user-menu a { padding: 4px 0; }

  /* フラッシュ通知 */
  .flash { padding: 10px 12px; font-size: 13px; }
}

/* 超小型（〜380px = iPhone SE 縦） */
@media (max-width: 380px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .kpi-value { font-size: 18px; }
  .topbar .user-menu .user-label { display: none; }
}
