:root {
  --sidebar-bg: #1e293b;
  --sidebar-hover: #334155;
  --sidebar-active: #2563eb;
  --sidebar-width: 240px;
  --header-height: 60px;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --bg: #f1f5f9;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --shadow-lg: 0 4px 16px rgba(0,0,0,.12);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Segoe UI', 'Hiragino Sans', 'Meiryo', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; }

/* ===== LOGIN ===== */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
}
.login-card {
  background: white;
  border-radius: 16px;
  padding: 48px 40px;
  width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.login-logo { text-align: center; margin-bottom: 32px; }
.login-logo .icon { font-size: 48px; display: block; margin-bottom: 8px; }
.login-logo h1 { font-size: 20px; color: var(--text); font-weight: 700; }
.login-logo p { color: var(--text-muted); font-size: 12px; margin-top: 4px; }
.login-card .form-group { margin-bottom: 16px; }
.login-card label { display: block; font-size: 12px; font-weight: 600; color: var(--text-muted); margin-bottom: 6px; }
.login-card input { width: 100%; padding: 10px 14px; border: 1.5px solid var(--border); border-radius: var(--radius); font-size: 14px; outline: none; transition: border-color .2s; }
.login-card input:focus { border-color: var(--primary); }
.login-card .btn-login { width: 100%; padding: 12px; background: var(--primary); color: white; border: none; border-radius: var(--radius); font-size: 15px; font-weight: 600; cursor: pointer; margin-top: 8px; transition: background .2s; }
.login-card .btn-login:hover { background: var(--primary-dark); }
.login-error { color: var(--danger); font-size: 12px; margin-top: 8px; text-align: center; min-height: 18px; }

/* ===== MAIN LAYOUT ===== */
#main-app { display: flex; height: 100vh; overflow: hidden; }
#main-app.hidden { display: none; }

/* ===== SIDEBAR ===== */
#sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}
.sidebar-header {
  padding: 20px 16px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-header .app-title { color: white; font-size: 14px; font-weight: 700; }
.sidebar-header .app-sub { color: rgba(255,255,255,.5); font-size: 11px; margin-top: 2px; }
.sidebar-user {
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  display: flex;
  align-items: center;
  gap: 10px;
}
.sidebar-user .avatar {
  width: 32px; height: 32px;
  background: var(--primary);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: white; font-size: 14px; font-weight: 600; flex-shrink: 0;
}
.sidebar-user .user-info { min-width: 0; }
.sidebar-user .user-name { color: white; font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-user .user-role { color: rgba(255,255,255,.5); font-size: 11px; }

.sidebar-section { padding: 8px 0; }
.sidebar-section-label {
  padding: 8px 16px 4px;
  color: rgba(255,255,255,.35);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  color: rgba(255,255,255,.75);
  cursor: pointer;
  transition: background .15s, color .15s;
  border-left: 3px solid transparent;
  user-select: none;
}
.nav-item:hover { background: var(--sidebar-hover); color: white; }
.nav-item.active { background: rgba(37,99,235,.25); color: white; border-left-color: var(--primary); }
.nav-item .nav-icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }
.nav-item .nav-label { font-size: 13px; }
.sidebar-footer {
  margin-top: auto;
  padding: 12px;
  border-top: 1px solid rgba(255,255,255,.08);
}
.btn-logout {
  width: 100%;
  padding: 9px;
  background: transparent;
  border: 1px solid rgba(255,255,255,.2);
  color: rgba(255,255,255,.7);
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
  transition: all .2s;
}
.btn-logout:hover { background: rgba(255,255,255,.1); color: white; }

/* ===== CONTENT AREA ===== */
#content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.content-header {
  height: var(--header-height);
  background: white;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  flex-shrink: 0;
}
.content-header h2 { font-size: 18px; font-weight: 700; color: var(--text); }
.content-header .breadcrumb { color: var(--text-muted); font-size: 13px; }
.content-body { flex: 1; overflow-y: auto; padding: 24px; }

/* ===== CARDS ===== */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.card-header h3 { font-size: 15px; font-weight: 700; }
.card-body { padding: 20px; }

/* ===== TOOLBAR ===== */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.toolbar-spacer { flex: 1; }
.search-box {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 12px;
  min-width: 200px;
}
.search-box input {
  border: none;
  background: transparent;
  outline: none;
  font-size: 13px;
  width: 100%;
}

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: all .15s;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #15803d; }
.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: #b91c1c; }
.btn-warning { background: var(--warning); color: white; }
.btn-warning:hover { background: #b45309; }
.btn-outline { background: white; color: var(--text); border: 1.5px solid var(--border); }
.btn-outline:hover { background: var(--bg); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }

/* ===== TABLE ===== */
.table-wrapper { overflow-x: auto; }
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  table-layout: auto;
}
thead th {
  background: var(--bg);
  padding: 10px 16px;
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }
tbody td { padding: 10px 16px; vertical-align: middle; white-space: nowrap; }
/* アクション列はコンテンツ幅に収める */
th.col-actions { width: 1%; white-space: nowrap; text-align: right; }
td.col-actions { width: 1%; white-space: nowrap; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.actions-cell { display: flex; gap: 6px; justify-content: flex-end; }

/* ===== BADGES ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge-success { background: #dcfce7; color: #166534; }
.badge-danger { background: #fee2e2; color: #991b1b; }
.badge-warning { background: #fef3c7; color: #92400e; }
.badge-info { background: #dbeafe; color: #1e40af; }
.badge-gray { background: #f1f5f9; color: #475569; }

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}
.modal {
  background: white;
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.modal-lg { max-width: 760px; }
.modal-wide { max-width: 1050px; }
.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.modal-header h3 { font-size: 16px; font-weight: 700; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text-muted); padding: 4px; line-height: 1; border-radius: 4px; }
.modal-close:hover { background: var(--bg); }
.modal-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-shrink: 0;
}

/* ===== FORM ===== */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid.cols-1 { grid-template-columns: 1fr; }
.form-grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group.col-span-2 { grid-column: span 2; }
.form-group.col-span-3 { grid-column: span 3; }
.form-group label { font-size: 12px; font-weight: 600; color: var(--text-muted); }
.form-group input,
.form-group select,
.form-group textarea {
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color .2s;
  font-family: inherit;
  width: 100%;
}
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus { border-color: var(--primary); }
.form-group textarea { resize: vertical; min-height: 80px; }
.form-group input[type="number"] { -moz-appearance: textfield; }
.required::after { content: ' *'; color: var(--danger); }

/* ===== DASHBOARD ===== */
.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.stat-card {
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.stat-card .stat-label { font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat-card .stat-value { font-size: 28px; font-weight: 700; margin-top: 6px; color: var(--text); }
.stat-card .stat-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.stat-card.blue .stat-value { color: var(--primary); }
.stat-card.green .stat-value { color: var(--success); }
.stat-card.orange .stat-value { color: var(--warning); }
.stat-card.red .stat-value { color: var(--danger); }

.charts-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 16px; margin-bottom: 20px; }
.chart-container { position: relative; height: 280px; }

/* ===== BARCODE SCANNER ===== */
.scanner-area {
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
  margin-bottom: 16px;
}
.scanner-area video {
  width: 100%;
  height: 240px;
  object-fit: cover;
  display: block;
}
.scanner-line {
  position: absolute;
  top: 50%;
  left: 10%;
  right: 10%;
  height: 2px;
  background: rgba(37,99,235,.8);
  box-shadow: 0 0 8px rgba(37,99,235,.8);
  animation: scan-line 2s linear infinite;
}
@keyframes scan-line {
  0% { top: 20%; }
  50% { top: 80%; }
  100% { top: 20%; }
}
.scanner-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  color: rgba(255,255,255,.7);
}
.scanner-tabs { display: flex; gap: 4px; margin-bottom: 16px; }
.scanner-tab {
  flex: 1;
  padding: 8px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: white;
  cursor: pointer;
  font-size: 13px;
  text-align: center;
  transition: all .15s;
}
.scanner-tab.active { background: var(--primary); color: white; border-color: var(--primary); }

/* ===== FILTER BAR ===== */
.filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
}
.filter-bar .form-group { margin: 0; }
.filter-bar select,
.filter-bar input {
  padding: 7px 10px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  outline: none;
}
.filter-bar select:focus,
.filter-bar input:focus { border-color: var(--primary); }

/* ===== IN/OUT TYPE ===== */
.type-in { color: var(--success); font-weight: 600; }
.type-out { color: var(--danger); font-weight: 600; }
.qty-positive { color: var(--success); }
.qty-negative { color: var(--danger); }

/* ===== CONFIRM TOGGLE ===== */
.confirm-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: none;
  transition: all .2s;
}
.confirm-toggle.confirmed { background: #dcfce7; color: #166534; }
.confirm-toggle.pending { background: #f1f5f9; color: #64748b; }

/* ===== EMPTY STATE ===== */
.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}
.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 14px; }

/* ===== MOBILE RESPONSIVE ===== */
@media (max-width: 768px) {
  /* サイドバーをオーバーレイ式に */
  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform .25s ease;
    width: 260px;
  }
  #sidebar.open { transform: translateX(0); }

  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 199;
  }
  #sidebar-overlay.open { display: block; }

  /* コンテンツエリアを全幅 */
  #content-area { width: 100%; }

  /* ヘッダーにハンバーガーボタン */
  .content-header { padding: 0 12px; gap: 10px; }
  .content-header h2 { font-size: 15px; }
  #hamburger-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px; height: 36px;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 20px;
    flex-shrink: 0;
    border-radius: 6px;
    color: var(--text);
  }
  #hamburger-btn:hover { background: var(--bg); }

  .content-body { padding: 12px; }

  /* カードの余白を縮小 */
  .card-header { padding: 12px 14px; flex-wrap: wrap; gap: 8px; }
  .card-body { padding: 12px 14px; }

  /* ツールバーを折り返し */
  .toolbar { flex-wrap: wrap; }
  .search-box { min-width: 0; flex: 1; }

  /* テーブルをスクロール可能に */
  .table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  table { min-width: 500px; font-size: 12px; }
  tbody td, thead th { padding: 8px 8px; }

  /* ボタン */
  .btn { padding: 8px 12px; font-size: 13px; }
  .btn-sm { padding: 6px 10px; font-size: 12px; }

  /* フィルターバー縦並び */
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar select, .filter-bar input { width: 100%; }

  /* フォームグリッドを1列 */
  .form-grid { grid-template-columns: 1fr; }
  .form-grid.cols-3 { grid-template-columns: 1fr; }
  .form-group.col-span-2 { grid-column: span 1; }
  .form-group.col-span-3 { grid-column: span 1; }

  /* ダッシュボード */
  .dashboard-grid { grid-template-columns: 1fr 1fr; }
  .charts-grid { grid-template-columns: 1fr; }
  .stat-card .stat-value { font-size: 22px; }

  /* モーダル */
  .modal { max-height: 95vh; border-radius: 12px 12px 0 0; }
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal-lg { max-width: 100%; }
  .modal-header { padding: 16px 16px 12px; }
  .modal-body { padding: 12px 16px; }
  .modal-footer { padding: 12px 16px; }

  /* スキャナー */
  .scanner-tabs { gap: 6px; }

  /* アクション列 */
  .actions-cell { flex-wrap: wrap; gap: 4px; }
}

@media (max-width: 400px) {
  .dashboard-grid { grid-template-columns: 1fr; }
}

/* PCではハンバーガーを非表示 */
@media (min-width: 769px) {
  #hamburger-btn { display: none; }
}

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ===== UTILS ===== */
.hidden { display: none !important; }
.mt-4 { margin-top: 16px; }
.flex { display: flex; }
.gap-2 { gap: 8px; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.font-bold { font-weight: 700; }
.text-sm { font-size: 12px; }
.text-muted { color: var(--text-muted); }

.alert {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: 16px;
  font-size: 13px;
}
.alert-info { background: #dbeafe; color: #1e40af; border-left: 4px solid var(--primary); }
.alert-warning { background: #fef3c7; color: #92400e; border-left: 4px solid var(--warning); }
.alert-success { background: #dcfce7; color: #166534; border-left: 4px solid var(--success); }
