:root {
  --bg: #f7f3ea;
  --bg-soft: #efe7d8;
  --ink: #1e2c2d;
  --ink-soft: #53676a;
  --brand: #0e7c86;
  --brand-dark: #07545b;
  --accent: #d06f2f;
  --card: #fffdf8;
  --line: #d9d0bf;
  --danger: #b43f3f;
  --ok: #2f7b4b;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: "Avenir Next", "Gill Sans", "Trebuchet MS", sans-serif;
  color: var(--ink);
  background: radial-gradient(circle at 12% 18%, #fff7e5 0%, var(--bg) 38%, #dfd2bb 100%);
}

h1,
h2,
h3 {
  font-family: "Palatino Linotype", "Book Antiqua", Georgia, serif;
  margin: 0 0 0.5rem;
  color: #112021;
}

p {
  margin: 0 0 1rem;
  color: var(--ink-soft);
}

a {
  color: var(--brand-dark);
}

.app-bg {
  position: fixed;
  inset: 0;
  background:
    linear-gradient(140deg, rgba(255, 248, 235, 0.7), rgba(242, 235, 219, 0.7)),
    repeating-linear-gradient(
      -45deg,
      rgba(208, 111, 47, 0.03),
      rgba(208, 111, 47, 0.03) 8px,
      rgba(14, 124, 134, 0.03) 8px,
      rgba(14, 124, 134, 0.03) 16px
    );
  z-index: -1;
}

.app-shell {
  display: grid;
  grid-template-columns: 270px 1fr;
  min-height: 100vh;
}

.sidebar {
  padding: 2rem 1.2rem;
  background: linear-gradient(165deg, #183337 0%, #204b4f 45%, #2f6d71 100%);
  color: #f8efe0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.sidebar-top {
  display: block;
}

.brand-block {
  min-width: 0;
}

.brand-title {
  margin: 0;
  color: #fff8eb;
  letter-spacing: 0.4px;
}

.brand-subtitle {
  margin-top: -0.3rem;
  color: #d3e7e8;
  font-size: 0.92rem;
}

.menu {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-top: 0.8rem;
}

.menu-link {
  color: #ecf9f8;
  text-decoration: none;
  font-weight: 600;
  padding: 0.65rem 0.8rem;
  border-radius: 10px;
  transition: transform 120ms ease, background-color 120ms ease;
}

.menu-link:hover {
  background: rgba(255, 255, 255, 0.12);
  transform: translateX(2px);
}

.menu-link.is-active {
  background: linear-gradient(120deg, #f9c76a 0%, #ea9f4a 100%);
  color: #3c240a;
}

.menu-toggle {
  display: none;
  background: rgba(255, 255, 255, 0.14);
  color: #fff6e7;
  border: 1px solid rgba(255, 255, 255, 0.28);
  min-height: 44px;
  padding: 0.48rem 0.72rem;
  border-radius: 10px;
}

.menu-toggle:hover {
  background: rgba(255, 255, 255, 0.2);
}

.sidebar-footer {
  margin-top: auto;
  display: grid;
  gap: 0.6rem;
}

.user-pill {
  display: inline-block;
  padding: 0.4rem 0.6rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.18);
  font-size: 0.84rem;
  line-height: 1.35;
  max-width: 100%;
  white-space: normal;
}

.ghost-link {
  color: #ffe8bd;
  text-decoration: none;
  font-weight: 700;
}

.bottom-nav {
  display: none;
}

.main-content {
  padding: 1.8rem clamp(1rem, 2.2vw, 2.4rem);
}

.page-head {
  margin-bottom: 1rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 10px 25px rgba(17, 32, 33, 0.07);
  padding: 1rem;
  margin-bottom: 1rem;
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.8rem;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.9rem;
  margin-bottom: 1rem;
}

.metric-card {
  background: linear-gradient(155deg, #fff8ec 0%, #f3ebda 100%);
  border: 1px solid #e7dcc9;
  border-radius: 14px;
  padding: 1rem;
}

.metric-card h3 {
  font-size: 0.95rem;
  margin-bottom: 0.7rem;
  color: #5f584d;
}

.metric-card p {
  color: #1f383a;
  font-size: 2rem;
  margin: 0;
  line-height: 1;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  align-items: center;
  margin-bottom: 0.8rem;
}

.period-switch {
  margin-top: 0.9rem;
}

.inline-submit {
  margin-top: 0.2rem;
}

input,
select,
textarea,
button {
  font: inherit;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid #d0c4af;
  border-radius: 10px;
  background: #fff;
  padding: 0.62rem 0.74rem;
}

textarea {
  resize: vertical;
}

button,
.button-secondary,
.button-danger {
  border: 0;
  border-radius: 10px;
  padding: 0.62rem 0.9rem;
  text-decoration: none;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease;
}

button {
  background: linear-gradient(120deg, var(--brand), var(--brand-dark));
  color: #fff;
}

button:hover,
.button-secondary:hover,
.button-danger:hover {
  transform: translateY(-1px);
  opacity: 0.95;
}

.button-secondary {
  background: #ece2cf;
  color: #4f4638;
}

.button-danger {
  background: linear-gradient(120deg, #c7514b, #9a2d2d);
  color: #fff;
}

button:disabled,
.button-secondary:disabled,
.button-danger:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none !important;
}

.table-wrap {
  position: relative;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

th,
td {
  border: 1px solid #dfd6c5;
  padding: 0.56rem 0.6rem;
  text-align: left;
  font-size: 0.92rem;
  vertical-align: top;
}

th {
  background: #efe6d4;
  color: #3d443f;
}

.num {
  text-align: right;
  white-space: nowrap;
}

.table-link {
  font-weight: 700;
}

.mobile-only-col,
.mobile-toggle-cell {
  display: none;
}

.mobile-detail-btn {
  border: 1px solid #d9cdb6;
  background: #f5ebd8;
  color: #514733;
  border-radius: 9px;
  font-weight: 700;
  padding: 0.46rem 0.6rem;
}

.bottom-nav-link {
  text-decoration: none;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.9rem;
}

.form-grid.compact {
  gap: 0.7rem;
}

.form-grid label {
  display: grid;
  gap: 0.35rem;
  color: #314041;
  font-weight: 600;
}

.span-2 {
  grid-column: 1 / -1;
}

.actions {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  flex-wrap: wrap;
}

.formula {
  background: #f6efdf;
  border: 1px dashed #d8c9a8;
  padding: 0.7rem;
  border-radius: 10px;
}

.flash {
  border-radius: 12px;
  padding: 0.7rem 0.85rem;
  margin-bottom: 0.85rem;
  border: 1px solid transparent;
}

.flash.success {
  background: #e4f4e9;
  border-color: #b4d8bf;
  color: #205534;
}

.flash.danger {
  background: #fce8e8;
  border-color: #e9c1c1;
  color: #8a2f2f;
}

.hint {
  margin-top: 0.5rem;
  font-size: 0.87rem;
  color: #6c6e67;
}

.users-mobile-list {
  display: none;
}

.users-create-form {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  max-width: 1020px;
}

.users-create-actions {
  grid-column: 1 / -1;
  justify-content: flex-end;
}

.users-list-head {
  flex-wrap: wrap;
  gap: 0.35rem 0.8rem;
}

.users-list-head .hint {
  margin: 0;
}

.users-table-wrap table {
  min-width: 940px;
}

.users-table-wrap th,
.users-table-wrap td {
  vertical-align: middle;
}

.users-actions {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.users-actions button,
.user-delete-form button {
  min-width: 90px;
}

.user-delete-form {
  margin: 0;
}

.user-mobile-card {
  border: 1px solid #ddd3c1;
  border-radius: 12px;
  background: #fff;
  padding: 0.72rem;
}

.user-mobile-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.user-role-tag {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  border: 1px solid #d8ccb7;
  background: #f5ecdb;
  color: #4f483a;
  font-size: 0.76rem;
  font-weight: 700;
  padding: 0.18rem 0.5rem;
}

.user-modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.user-modal[hidden] {
  display: none;
}

.user-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(17, 33, 35, 0.62);
}

.user-modal-panel {
  position: relative;
  width: min(760px, 96vw);
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 18px 42px rgba(13, 26, 28, 0.26);
  padding: 1rem;
}

.user-modal-panel .card-head {
  margin-bottom: 0.85rem;
}

.user-modal-panel .card-head .hint {
  margin: 0;
}

.user-edit-form input[readonly] {
  background: #f4eee2;
  color: #5f635b;
}

.user-modal-actions {
  justify-content: flex-end;
}

body.modal-open {
  overflow: hidden;
}

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 8% 12%, #fff4dc 0%, #eadfc8 55%, #d0bf9f 100%);
}

.auth-shell {
  width: min(460px, 95vw);
}

.auth-card {
  background: #fffaf1;
  border: 1px solid #d7c7ac;
  box-shadow: 0 16px 36px rgba(35, 29, 13, 0.14);
  border-radius: 16px;
  padding: 1.5rem;
}

.auth-sub {
  margin-bottom: 1.1rem;
}

.auth-error {
  color: var(--danger);
  font-weight: 700;
}

@media (max-width: 980px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    padding: 1rem 0.9rem 0.85rem;
    z-index: 9;
    gap: 0.55rem;
    box-shadow: 0 8px 18px rgba(0, 0, 0, 0.12);
  }

  .brand-title {
    font-size: 1.45rem;
    margin-bottom: 0.1rem;
  }

  .brand-subtitle {
    font-size: 0.82rem;
    margin-top: 0;
    margin-bottom: 0.25rem;
  }

  .menu {
    flex-direction: row;
    gap: 0.35rem;
    margin-top: 0.2rem;
    padding-bottom: 0.2rem;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
  }

  .menu-link {
    white-space: nowrap;
    font-size: 0.9rem;
    padding: 0.52rem 0.66rem;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    scroll-snap-align: start;
  }

  .menu::-webkit-scrollbar {
    height: 6px;
  }

  .menu::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.28);
    border-radius: 999px;
  }

  .table-wrap::before,
  .table-wrap::after {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    width: 12px;
    pointer-events: none;
    z-index: 2;
  }

  .table-wrap::before {
    left: 0;
    background: linear-gradient(to right, rgba(255, 253, 248, 0.95), rgba(255, 253, 248, 0));
  }

  .table-wrap::after {
    right: 0;
    background: linear-gradient(to left, rgba(255, 253, 248, 0.95), rgba(255, 253, 248, 0));
  }

  .sidebar-footer {
    margin-top: 0.2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  .user-pill {
    font-size: 0.76rem;
    padding: 0.34rem 0.54rem;
  }

  .ghost-link {
    font-size: 0.86rem;
  }

  .main-content {
    padding: 1rem 0.72rem 1.35rem;
  }

  .page-head {
    margin-bottom: 0.7rem;
  }

  .page-head h2 {
    font-size: 1.4rem;
  }

  .card {
    padding: 0.75rem;
    border-radius: 12px;
    margin-bottom: 0.75rem;
  }

  .cards-grid {
    grid-template-columns: 1fr;
    gap: 0.65rem;
    margin-bottom: 0.75rem;
  }

  .metric-card {
    padding: 0.8rem;
  }

  .metric-card p {
    font-size: 1.7rem;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 0.65rem;
  }

  .span-2 {
    grid-column: auto;
  }

  .toolbar > * {
    flex: 1 1 100%;
  }

  .toolbar button,
  .toolbar input,
  .toolbar select,
  .toolbar .button-secondary {
    width: 100%;
    min-height: 44px;
  }

  .actions {
    width: 100%;
  }

  .actions button,
  .actions .button-secondary {
    width: 100%;
    text-align: center;
  }

  .users-actions {
    flex-direction: column;
    align-items: stretch;
  }

  .users-actions button,
  .user-delete-form,
  .user-delete-form button {
    width: 100%;
  }

  th,
  td {
    font-size: 0.85rem;
    padding: 0.48rem 0.5rem;
  }

  table {
    min-width: 660px;
  }

  .auth-shell {
    width: min(440px, 94vw);
  }

  .auth-card {
    padding: 1.15rem 1rem;
  }

  .auth-card h1 {
    font-size: 2rem;
    line-height: 1.06;
  }
}

@media (max-width: 640px) {
  .sidebar {
    position: sticky;
    top: 0;
    padding: 0.8rem 0.66rem 0.72rem;
    gap: 0.3rem;
    z-index: 12;
  }

  .sidebar-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.45rem;
  }

  .brand-block {
    max-width: calc(100% - 110px);
  }

  .brand-title {
    font-size: 1.25rem;
  }

  .brand-subtitle {
    display: none;
  }

  .menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 90px;
    font-size: 0.84rem;
  }

  .sidebar .menu,
  .sidebar .sidebar-footer {
    display: none;
  }

  .sidebar.is-open .menu {
    display: flex;
    margin-top: 0.35rem;
  }

  .sidebar.is-open .sidebar-footer {
    display: flex;
    margin-top: 0.25rem;
  }

  .menu-link {
    padding: 0.48rem 0.58rem;
    font-size: 0.84rem;
    min-height: 44px;
  }

  .main-content {
    padding: 0.78rem 0.5rem 5.2rem;
  }

  .card {
    padding: 0.62rem;
  }

  .page-head h2 {
    font-size: 1.18rem;
  }

  .metric-card h3 {
    font-size: 0.86rem;
  }

  .metric-card p {
    font-size: 1.45rem;
  }

  .table-wrap::before,
  .table-wrap::after {
    display: none;
  }

  .table-wrap table {
    min-width: 0;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0 0.55rem;
  }

  .table-wrap thead {
    display: none;
  }

  .table-wrap tbody {
    display: block;
  }

  .table-wrap tr {
    display: block;
    border: 1px solid #ddd3c1;
    border-radius: 10px;
    background: #fff;
    overflow: hidden;
  }

  .table-wrap td {
    display: flex;
    justify-content: space-between;
    gap: 0.55rem;
    align-items: flex-start;
    text-align: left;
    border: 0;
    border-bottom: 1px dashed #e7dcc8;
    padding: 0.48rem 0.52rem;
    font-size: 0.8rem;
    word-break: break-word;
  }

  .table-wrap td:last-child {
    border-bottom: 0;
  }

  .table-wrap td::before {
    content: attr(data-label);
    display: inline-block;
    color: #5f5b4f;
    font-weight: 700;
    min-width: 42%;
    max-width: 42%;
    padding-right: 0.22rem;
  }

  .table-wrap td[colspan] {
    display: block;
    text-align: center;
  }

  .table-wrap td[colspan]::before {
    content: none;
  }

  .table-wrap .table-collapsible-mobile tr.mobile-collapsible td.mobile-extra {
    display: none;
  }

  .table-wrap .table-collapsible-mobile tr.mobile-collapsible.is-expanded td.mobile-extra {
    display: flex;
  }

  .table-wrap td.mobile-toggle-cell {
    display: block;
    border-bottom: 0;
    padding: 0.52rem;
  }

  .table-wrap td.mobile-toggle-cell::before {
    content: none;
  }

  .mobile-detail-btn {
    width: 100%;
    min-height: 42px;
    font-size: 0.82rem;
  }

  .bottom-nav {
    display: grid;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.45rem;
    padding: 0.56rem max(0.5rem, env(safe-area-inset-left))
      calc(0.56rem + env(safe-area-inset-bottom))
      max(0.5rem, env(safe-area-inset-right));
    background: rgba(18, 43, 46, 0.96);
    border-top: 1px solid rgba(255, 255, 255, 0.14);
    backdrop-filter: blur(8px);
    z-index: 30;
  }

  .bottom-nav-link {
    min-height: 44px;
    border-radius: 10px;
    color: #d7ecee;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    font-size: 0.86rem;
    font-weight: 700;
    text-decoration: none;
    background: rgba(255, 255, 255, 0.08);
  }

  .bottom-nav-link.is-active {
    color: #2f210f;
    background: linear-gradient(120deg, #f9c76a 0%, #ea9f4a 100%);
  }

  .users-table-wrap {
    display: none;
  }

  .users-mobile-list {
    display: grid;
    gap: 0.55rem;
  }

  .users-actions {
    display: grid;
    gap: 0.45rem;
  }

  .users-actions button,
  .user-delete-form button,
  .auth-card button {
    min-height: 44px;
    width: 100%;
  }

  .user-modal {
    padding: 0.72rem;
  }

  .user-modal-panel {
    width: 96vw;
    max-height: calc(100vh - 1.44rem);
    padding: 0.8rem;
  }

  .user-modal-actions {
    width: 100%;
  }

  .auth-shell {
    width: 96vw;
  }

  .auth-card {
    padding: 0.95rem 0.8rem;
    border-radius: 13px;
  }

  .auth-card h1 {
    font-size: 1.72rem;
    margin-bottom: 0.35rem;
  }

  .auth-sub {
    margin-bottom: 0.8rem;
    font-size: 0.95rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .menu-link:hover,
  .button-secondary:hover,
  button:hover {
    transform: none !important;
  }
}
