@layer reset, base, components;

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  body, h1, h2, h3, p { margin: 0; }
  button, input, select { font: inherit; }
  button { cursor: pointer; }
}

@layer base {
  :root {
    color: #15212b;
    background: #f5f7f8;
    font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
    font-synthesis: none;
  }

  body { min-width: 320px; min-height: 100dvh; }
  button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid #3b82f6; outline-offset: 2px; }
}

@layer components {
  .auth-page {
    display: grid;
    min-height: 100dvh;
    place-items: center;
    padding: 24px;
    background: radial-gradient(circle at top, #d8edf1, #f5f7f8 50%);

    .auth-card {
      width: min(100%, 420px);
      padding: 32px 24px;
      border: 1px solid #dbe2e7;
      border-radius: 20px;
      background: #fff;
      box-shadow: 0 16px 48px rgb(21 33 43 / 12%);

      h1 { margin-bottom: 8px; font-size: 1.45rem; }
      p { color: #52616d; line-height: 1.7; }
    }
  }

  .form-stack {
    display: grid;
    gap: 16px;
    margin-top: 28px;

    label { display: grid; gap: 7px; color: #35434e; font-size: .9rem; font-weight: 650; }
    input, select, textarea {
      width: 100%;
      min-height: 48px;
      padding: 10px 12px;
      border: 1px solid #bac7d1;
      border-radius: 10px;
      background: #fff;
      color: inherit;
    }
    .code-input { letter-spacing: .5em; font-size: 1.4rem; text-align: center; }
  }

  .button {
    min-height: 46px;
    padding: 10px 16px;
    border: 0;
    border-radius: 10px;
    background: #123047;
    color: #fff;
    font-weight: 700;

    &:disabled { cursor: wait; opacity: .55; }
    &.secondary { border: 1px solid #b8c5ce; background: #fff; color: #22313d; }
    &.danger { background: #b91c1c; }
    &.text { min-height: auto; padding: 4px; background: transparent; color: #125f7c; }
  }

  .notice {
    margin-top: 22px;
    padding: 12px;
    border-left: 3px solid #9aabb7;
    background: #f2f5f7;
    color: #53626d;
    font-size: .83rem;
    line-height: 1.6;
  }

  .shell { min-height: 100dvh; padding-top: 56px; }
  .header {
    position: fixed;
    z-index: 10;
    inset: 0 0 auto;
    display: flex;
    height: 56px;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 0 12px;
    border-bottom: 1px solid #dce3e8;
    background: rgb(255 255 255 / 94%);
    backdrop-filter: blur(12px);

    .brand { color: #123047; font-size: 1rem; font-weight: 800; white-space: nowrap; }
    .header-actions { display: flex; align-items: center; gap: 5px; }
    .update-button { border: 0; background: transparent; color: #5d6b75; font-size: .72rem; }
    .icon-button { display: grid; width: 38px; height: 38px; place-items: center; border: 0; border-radius: 50%; background: transparent; color: #123047; font-size: 1.15rem; }
  }

  .main { width: min(100%, 760px); margin: 0 auto; padding: 20px 14px 100px; }
  .page-title { display: flex; align-items: start; justify-content: space-between; gap: 12px; margin-bottom: 18px;
    h1 { font-size: 1.35rem; }
    p { margin-top: 4px; color: #62717b; font-size: .82rem; }
  }

  .week-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px;
    .week-label { font-size: .92rem; font-weight: 700; }
    button { border: 0; background: transparent; color: #155e75; font-weight: 700; }
  }

  .timetable {
    display: grid;
    grid-template-columns: 31px repeat(7, minmax(0, 1fr));
    overflow: hidden;
    border: 1px solid #d5dee5;
    border-radius: 12px;
    background: #d5dee5;
    gap: 1px;

    .day, .slot-label { display: grid; min-height: 30px; place-items: center; background: #eef2f4; color: #53616c; font-size: .68rem; font-weight: 700; }
    .slot-label { min-height: 46px; font-size: .65rem; writing-mode: vertical-rl; }
    .cell {
      min-height: 46px;
      border: 0;
      background: #fff;
      transition: transform .12s ease, box-shadow .12s ease;

      &[data-state="reserved"] { background: var(--reservation-color, #64748b); }
      &[data-state="unavailable"] { background: repeating-linear-gradient(135deg, #aeb8c0 0 5px, #d6dce0 5px 10px); }
      &[data-mine="true"] { box-shadow: inset 0 0 0 2.5px #111; }
      &[data-selected="true"] { box-shadow: inset 0 0 0 3px #2563eb; transform: scale(.94); }
    }
  }

  .legend { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 12px; color: #63717a; font-size: .75rem;
    span { display: inline-flex; align-items: center; gap: 5px; }
    i { display: inline-block; width: 12px; height: 12px; border: 1px solid #aeb9c0; border-radius: 3px; }
    .mine { border: 2px solid #111; background: #fff; }
    .busy { background: #4f8e9f; }
  }

  .fab {
    position: fixed;
    right: max(18px, env(safe-area-inset-right));
    bottom: max(22px, env(safe-area-inset-bottom));
    width: 58px;
    height: 58px;
    border: 0;
    border-radius: 50%;
    background: #123047;
    box-shadow: 0 8px 18px rgb(18 48 71 / 28%);
    color: #fff;
    font-size: 2rem;
  }

  .drawer-backdrop, .modal-backdrop { position: fixed; z-index: 20; inset: 0; background: rgb(12 26 36 / 45%); }
  .drawer {
    position: fixed;
    z-index: 21;
    inset: 0 0 0 auto;
    width: min(84vw, 340px);
    padding: 24px 18px;
    background: #fff;
    box-shadow: -12px 0 40px rgb(10 30 42 / 18%);

    h2 { font-size: 1.1rem; }
    .service-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 20px; }
    .service { min-height: 90px; padding: 12px; border: 1px solid #d5e0e5; border-radius: 12px; background: #f9fbfc; color: #183244; text-align: left;
      span { display: block; margin-bottom: 6px; font-size: 1.45rem; }
      small { color: #66757f; }
    }
    .user-box { margin-top: 28px; padding-top: 18px; border-top: 1px solid #e0e7eb;
      p { font-weight: 700; }
      small { color: #6a7881; }
    }
  }

  .modal-backdrop { display: grid; place-items: end center; padding: 12px; }
  .modal {
    width: min(100%, 520px);
    max-height: calc(100dvh - 24px);
    overflow: auto;
    padding: 22px;
    border-radius: 18px;
    background: #fff;

    h2 { font-size: 1.2rem; }
    .modal-copy { margin: 10px 0; color: #60707a; line-height: 1.65; }
    .detail-list { display: grid; gap: 11px; margin: 18px 0;
      div { display: flex; justify-content: space-between; gap: 12px; }
      dt { color: #66757f; }
      dd { margin: 0; font-weight: 650; text-align: right; }
    }
    .modal-actions { display: flex; justify-content: end; gap: 9px; margin-top: 22px; }
  }

  .department-badge { display: inline-flex; align-items: center; gap: 6px;
    &::before { width: 9px; height: 9px; border-radius: 50%; background: var(--color); content: ""; }
  }
  .selection-summary { margin-top: 16px; color: #576773; font-size: .86rem; line-height: 1.55; }
  .admin-only { margin-top: 12px; color: #45545e; font-size: .9rem; }
  .admin-section { margin-top: 18px; padding: 18px; border: 1px solid #d9e2e7; border-radius: 14px; background: #fff;
    h2 { font-size: 1rem; }
    > p { margin-top: 7px; color: #5d6c76; font-size: .85rem; line-height: 1.5; }
    .form-stack { margin-top: 14px; }
  }
  .inline-form { grid-template-columns: 1fr 74px auto; align-items: end;
    .button { margin-bottom: 0; }
  }
  .admin-list { display: grid; gap: 8px; margin-top: 16px;
    > div { display: flex; min-height: 42px; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid #e7edef; }
    .user-row {
      > span:first-child { display: grid; gap: 2px; min-width: 0; font-weight: 650;
        small { overflow: hidden; color: #71808a; font-weight: 400; text-overflow: ellipsis; }
      }
      select { max-width: 130px; min-height: 34px; padding: 4px; border: 1px solid #c6d2d9; border-radius: 6px; }
    }
  }
  .toast-stack { position: fixed; z-index: 30; top: 67px; left: 50%; display: grid; width: min(calc(100% - 28px), 480px); gap: 8px; transform: translateX(-50%); }
  .toast { padding: 13px 15px; border-radius: 10px; box-shadow: 0 8px 20px rgb(11 31 44 / 18%); color: #fff; font-size: .9rem;
    &[data-type="success"] { background: #15803d; }
    &[data-type="error"] { background: #b91c1c; }
    &[data-type="warning"] { background: #a16207; }
    &[data-type="info"] { background: #0369a1; }
  }

  @media (min-width: 640px) {
    .main { padding-inline: 24px; }
    .timetable { grid-template-columns: 50px repeat(7, minmax(0, 1fr));
      .slot-label, .cell { min-height: 58px; }
    }
    .modal-backdrop { place-items: center; }
  }
  @media (max-width: 430px) {
    .inline-form { grid-template-columns: 1fr 64px;
      .button { grid-column: 1 / -1; }
    }
  }
}
