/* 合作社進銷存系統 - 響應式樣式 */
:root {
  --brand: #2d6a4f;
  --brand-light: #52b788;
  --brand-dark: #1b4332;
  --bg: #f5f7f6;
  --card: #ffffff;
  --border: #d8e0dc;
  --text: #1e2a26;
  --muted: #6b7d76;
  --ok: #2f855a;
  --warn: #b45309;
  --err: #c0392b;
  --accent: #e8f2ec;
  --row-alt: #fafbfa;
  --shadow: 0 1px 3px rgba(27, 67, 50, 0.08);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
               "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.55;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Layout ---------- */
.topbar {
  background: var(--brand-dark);
  color: #fff;
  padding: 0.75rem 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow);
  position: sticky; top: 0; z-index: 100;
}
.topbar .brand { font-weight: 700; font-size: 1.1rem; }
.topbar .brand small { opacity: 0.8; font-weight: 400; margin-left: 0.5rem; }
.topbar .user-info { display: flex; align-items: center; gap: 0.75rem; font-size: 0.9rem; }
.topbar .user-info a { color: #cdeadb; }
.topbar .user-info a:hover { color: #fff; }
.topbar .menu-toggle {
  display: none;
  background: transparent; color: #fff; border: 0;
  font-size: 1.4rem; cursor: pointer; padding: 0.25rem 0.5rem;
}

.layout { display: flex; min-height: calc(100vh - 56px); }
.sidebar {
  width: 230px;
  background: var(--card);
  border-right: 1px solid var(--border);
  padding: 1rem 0;
  flex-shrink: 0;
}
.sidebar h3 {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0.75rem 1.25rem 0.4rem;
  letter-spacing: 0.1em;
}
.sidebar a.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.55rem 1.25rem;
  color: var(--text);
  border-left: 3px solid transparent;
  font-size: 0.95rem;
}
.sidebar a.nav-item:hover { background: var(--accent); text-decoration: none; }
.sidebar a.nav-item.active {
  background: var(--accent);
  border-left-color: var(--brand);
  color: var(--brand-dark);
  font-weight: 600;
}
.sidebar a.nav-item .tag {
  font-size: 0.7rem;
  padding: 0.1rem 0.45rem;
  border-radius: 10px;
  background: #e3e9e6;
  color: var(--muted);
}
.sidebar a.nav-item .tag.ok { background: #d5eadd; color: var(--ok); }

.main {
  flex: 1;
  padding: 1.5rem 2rem;
  min-width: 0;
}

/* ---------- Headings ---------- */
h1, h2, h3 { color: var(--brand-dark); }
h1 { font-size: 1.5rem; margin: 0 0 1rem; }
h2 { font-size: 1.2rem; margin: 1.5rem 0 0.75rem; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.25rem;
  flex-wrap: wrap;
}
.page-header h1 { margin: 0; }
.breadcrumb { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.5rem; }

/* ---------- Card ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  box-shadow: var(--shadow);
}
.card-title { font-weight: 700; color: var(--brand-dark); margin: 0 0 0.75rem; }

/* Dashboard stats */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  box-shadow: var(--shadow);
}
.stat .num { font-size: 1.7rem; font-weight: 700; color: var(--brand-dark); }
.stat .lbl { font-size: 0.85rem; color: var(--muted); margin-top: 0.2rem; }

.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.85rem;
}
.module-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  transition: all 0.2s;
}
.module-card:hover {
  border-color: var(--brand-light);
  transform: translateY(-1px);
  text-decoration: none;
  box-shadow: 0 4px 10px rgba(27,67,50,0.1);
}
.module-card .code {
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.05em;
}
.module-card .name { font-weight: 700; color: var(--brand-dark); font-size: 1.05rem; }
.module-card .status {
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 10px;
  display: inline-block;
  margin-top: 0.25rem;
  align-self: flex-start;
}
.module-card .status.ok { background: #d5eadd; color: var(--ok); }
.module-card .status.soon { background: #fbefd4; color: var(--warn); }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; margin: 0 -0.25rem; }
table.data {
  width: 100%;
  border-collapse: collapse;
  background: var(--card);
  border-radius: 8px;
  overflow: hidden;
  min-width: 600px;
}
table.data thead th {
  background: var(--accent);
  color: var(--brand-dark);
  font-weight: 600;
  font-size: 0.85rem;
  padding: 0.6rem 0.75rem;
  text-align: left;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}
table.data tbody td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
table.data tbody tr:nth-child(even) { background: var(--row-alt); }
table.data tbody tr:hover { background: #f1f6f3; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.ctr, table.data th.ctr { text-align: center; }

/* 列可點擊：整列視為可按進入編輯 */
table.data tbody tr.clickable {
  cursor: pointer;
  transition: background 0.15s;
}
table.data tbody tr.clickable:hover {
  background: #e8f2ec;
  box-shadow: inset 3px 0 0 var(--brand);
}
table.data tbody tr.clickable td { position: relative; }
/* 操作欄內的按鈕/表單點擊不觸發列跳轉 */
table.data tbody tr.clickable td.ctr { cursor: default; }

/* ---------- Form ---------- */
.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.9rem;
  margin-bottom: 0.8rem;
}
.form-group { display: flex; flex-direction: column; gap: 0.3rem; }
.form-group label {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 500;
}
.form-group label .req { color: var(--err); }

input[type=text], input[type=password], input[type=number],
input[type=date], input[type=email], select, textarea {
  font-family: inherit;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.95rem;
  background: #fff;
  color: var(--text);
  min-height: 40px;  /* 觸控友善 */
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(45, 106, 79, 0.15);
}
textarea { resize: vertical; min-height: 80px; }

.checkbox-inline {
  display: inline-flex; align-items: center; gap: 0.35rem;
  padding: 0.5rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  background: #fff;
  user-select: none;
}
.checkbox-inline input { accent-color: var(--brand); }
.checkbox-inline:hover { border-color: var(--brand-light); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  padding: 0.55rem 1rem;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 500;
  text-decoration: none;
  min-height: 40px;
  transition: all 0.15s;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-primary:hover { background: var(--brand-dark); color: #fff; }
.btn-secondary { background: #fff; color: var(--brand); border-color: var(--brand); }
.btn-secondary:hover { background: var(--accent); }
.btn-danger { background: var(--err); color: #fff; }
.btn-danger:hover { background: #8e2a20; color: #fff; }
.btn-ghost { background: transparent; color: var(--muted); border-color: var(--border); }
.btn-ghost:hover { color: var(--text); }
.btn-sm { padding: 0.3rem 0.65rem; font-size: 0.85rem; min-height: 32px; }

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1rem 0 0;
}

/* ---------- Badge & Flash ---------- */
.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
}
.badge-customer { background: #dbeafe; color: #1d4ed8; }
.badge-supplier { background: #fef3c7; color: #92400e; }
.badge-active   { background: #d5eadd; color: var(--ok); }
.badge-inactive { background: #e5e7eb; color: #4b5563; }
.badge-admin    { background: #fce7f3; color: #be185d; }

.flash {
  padding: 0.75rem 1rem;
  border-radius: 6px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  border-left: 4px solid;
}
.flash-ok    { background: #e8f5ec; color: var(--ok); border-color: var(--ok); }
.flash-error { background: #fde7e4; color: var(--err); border-color: var(--err); }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand-dark) 0%, var(--brand) 100%);
  padding: 1rem;
}
.login-box {
  width: 100%;
  max-width: 380px;
  background: #fff;
  border-radius: 14px;
  padding: 2rem;
  box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}
.login-box h1 {
  text-align: center;
  font-size: 1.3rem;
  margin: 0 0 0.25rem;
}
.login-box .subtitle {
  text-align: center;
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 1.5rem;
}
.login-box .btn-primary { width: 100%; }
.login-box .hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px dashed var(--border);
}

/* ---------- Dynamic row table (收購磅單) ---------- */
.dyn-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.5rem;
  min-width: 1080px;  /* 欄位加寬後需水平捲動的最小寬度 */
}
.dyn-table th, .dyn-table td {
  padding: 0.5rem 0.55rem;
  border: 1px solid var(--border);
  vertical-align: middle;
}
.dyn-table th {
  background: var(--accent);
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
}
.dyn-table input, .dyn-table select {
  width: 100%;
  padding: 0.55rem 0.6rem;
  min-height: 44px;
  font-size: 0.95rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font-family: inherit;
  /* 避免下拉選項選完後被格子遮住 */
  text-overflow: ellipsis;
}
.dyn-table select {
  min-width: 110px;
  appearance: auto;
}
.dyn-table input:focus, .dyn-table select:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 2px rgba(45, 106, 79, 0.15);
}
.dyn-table .net {
  background: var(--row-alt);
  text-align: right;
  font-weight: 600;
  color: var(--brand-dark);
}
.dyn-table .gross-auto {
  background: #f4faf7;
  text-align: right;
  font-weight: 600;
  color: var(--brand-dark);
  font-variant-numeric: tabular-nums;
}

/* ---------- Grid price editor ---------- */
.price-grid {
  border-collapse: collapse;
  width: 100%;
}
.price-grid th, .price-grid td {
  border: 1px solid var(--border);
  padding: 0.4rem;
  text-align: center;
}
.price-grid thead th { background: var(--accent); color: var(--brand-dark); }
.price-grid input[type=number] {
  width: 100%;
  text-align: right;
  padding: 0.35rem;
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .topbar .menu-toggle { display: inline-block; }
  .layout { flex-direction: column; }
  .sidebar {
    width: 100%;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    display: none;
  }
  .sidebar.open { display: block; }
  .main { padding: 1rem; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .module-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }
  h1 { font-size: 1.25rem; }
}
@media (max-width: 480px) {
  .stat-grid { grid-template-columns: 1fr; }
  .topbar { padding: 0.6rem 1rem; }
  .topbar .brand { font-size: 1rem; }
  .topbar .user-info { font-size: 0.8rem; }
  .main { padding: 0.8rem; }
  .btn { font-size: 0.9rem; padding: 0.5rem 0.8rem; }
}

/* Print */
@media print {
  .topbar, .sidebar, .btn, .btn-row, .no-print { display: none !important; }
  .main { padding: 0; }
  .card { box-shadow: none; border: 1px solid #888; }
}
